* ,第二个为显示进度的参数,第第三个为doInBackground返回和onPostExecute传入的参数。
总结来说 就是OnPreExecute()函数中 在doInBackground()前 执行 通常可以用来显示一个对话框
OnpostExecute() 在 doInBackground()之后执行 可以用来更新UI 以及关闭对话框
doInbackground()用来 执行耗时任务 这样 即可
其中 各个函数的参数 protected void onPostExecute(String result) {} 这个函数的参数 是doInbackground函数的返回值
示例代码:
package com.example.asyn; import java.io.IOException; import java.io.InputStream; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.os.AsyncTask; import android.os.Message; import android.widget.TextView; public class MyAsynTask extends AsyncTask<String, Void, String> { String jsonData; private String path; MyApplication myApplication; public MyAsynTask(String urlPath,MyApplication myApplication) { this.myApplication = myApplication; this.path = urlPath; } @Override protected void onPreExecute() { } @Override protected void onPostExecute(String result) { myApplication.setResult(result); } @Override protected String doInBackground(String... params) { HttpClient httpClient = new DefaultHttpClient(); HttpGet myGet = new HttpGet(constant.urlArticleString); try { HttpResponse httpResponse = httpClient.execute(myGet); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { System.out.println("------------->请求成功" + httpResponse.getStatusLine().getStatusCode()); HttpEntity entity = httpResponse.getEntity(); if (entity != null) { InputStream inputStream = entity.getContent(); jsonData = constant .convertStreamToString(inputStream); return jsonData; } } else { System.out.println("--------->请求失败" + httpResponse.getStatusLine().getStatusCode()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } }