AsyncTask异步交互的用法简介
2016-05-31 09:35
381 查看
首先定义一个类,继承AsyncTask类,并实现相关方法:
在UI主线程调用(注:创建一个MyAsyncTask对象,只能调用一次excute方法)
<span style="font-size:18px;">/** * @author WJL <span style="color:#ff0000;">第一个泛型:规定doInBackground方法的参数类型,规定AsyncTask.execute方法传递的参数类型 * 第二个泛型:规定onProgressUpdate方法参数的类型,publishProgress参数类型 * 第三个泛型:规定doInBackground方法的返回值类型</span> */ public class MyAsyncTask extends AsyncTask<String, Double, String> { TextView textView; ProgressBar bar; /** * @param textView * @param bar */ public MyAsyncTask(TextView textView, ProgressBar bar) { this.textView=textView; this.bar=bar; } // Runs on the UI thread before doInBackground. @Override // 运行在主线程,在doInBackground方法之前执行 protected void onPreExecute() { System.out.println("准备联网"); } @Override // 运行在子线程,做耗时操作 // 返回值被onPostExecute接收 <span style="color:#ff0000;">protected String doInBackground(String... params)</span> { String string = "100"; HttpClient httpClient = new DefaultHttpClient(); // 做联网请求,得到传递的参数,因为是可变长度的数组,想取第一个参数,所以去角标为0的元素 HttpGet httpGet = new HttpGet(params[0]); try { HttpResponse httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200) { HttpEntity httpEntity = httpResponse.getEntity(); //得到实体里的json string = EntityUtils.toString(httpEntity); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return string; } // Runs on the UI thread after doInBackground. // 运行在主线程,在doInBackground方法之后执行,接收doInBackground方法的返回值</span>
<span style="font-size:18px;">在此方法内更新UI线程中的控件 <span style="color:#ff0000;">protected void onPostExecute(String result)</span> { System.out.println(result); //更新进度 // publishProgress(100d); }; // 更新进度,运行在主线程上,在publishProgress运行之后 protected void onProgressUpdate(Double[] values) { // System.out.println("执行进度:" + values[0]); double r = values[0]; //向上取整 double floor = Math.ceil(r ); textView.setText(floor+""); // bar.setProgress(double.); }; //获取进度 public void progress(){ /* // 获取数据总长度 long contentLength = httpEntity.getContentLength(); System.out.println(contentLength); // 获取网络数据 InputStream inputStream = httpEntity.getContent(); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream, "utf-8")); char[] buf = new char[1]; //总读取进度 int sun = 0; //本次读取的进度 int i; StringBuilder buffer=new StringBuilder(); while ((i = bufferedReader.read(buf)) != -1) { sun=sun+i; double count = (((double)sun / (double)contentLength))*100; //实时更新进度 publishProgress(count); buffer.append(new String(buf)); } bufferedReader.close(); inputStream.close(); string = buffer.toString();*/ //////////////////////////////////////////////// } }</span>
在UI主线程调用(注:创建一个MyAsyncTask对象,只能调用一次excute方法)
<span style="font-size:18px;">public class MainActivity extends Activity { // String path="http://169.254.214.171:8080/bwie/city.json"; String key = "3ac9f31ff66b9746539472887b3799c3"; // 通过get请求时的接口地址 String get_path = "http://web.juhe.cn:8080/constellation/getAll?consName=狮子座&type=today&key=" + key; private TextView textView; private ProgressBar bar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView); ProgressBar bar = (ProgressBar) findViewById(R.id.pro); MyAsyncTask myAsyncTask=new MyAsyncTask(textView,bar); try { String string = myAsyncTask.execute(get_path).get(); textView.setText(string); System.out.println(string); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }</span>
相关文章推荐
- NSString
- opkg软件源设置
- iOS 【iOS iPhone屏幕尺寸、分辨率及屏幕适配】
- javascript事件处理模型实例说明
- 树边,前向边,后向边,横叉边
- 《创业维艰分享之五》所有得,有所乐,日事日清,循序渐进。
- 第二次冲刺第三天
- 找小水王
- 流程
- jQuery基础知识点总结(必看)
- Lens Distortion Correction
- google chrome开发者工具项(二)
- java开发人员必去的知名国外网站
- 关于Eclipse 自动重写某父类或者接口的方法@override
- 多屏幕显示
- ACM书籍推荐
- 原生js实现的星级评分效果
- String 、StringBuffer、 StringBuilder的区别
- Android常用Manager
- 在Macbook Pro上使用CornerStone(SVN)