【发布时间】:2014-08-22 16:22:07
【问题描述】:
我正在处理 AsyncTask,但在 doInBackground 完成后,onPostExecute 不会被调用。
这是我的 AsyncTask 类
public class GetServerData extends AsyncTask<Object, String, String>{
Context con;
ArrayList<Items> data;
ListViewAdapter adapter;
String IMAGE_PREFIX, cat_id;
public GetServerData(Context con, ArrayList<Items> data, ListViewAdapter adapter, String cat_id){
this.con = con;
this.data = data;
this.adapter = adapter;
IMAGE_PREFIX = con.getResources().getString(R.string.web_image_prefix);
this.cat_id = cat_id;
}
@Override
protected String doInBackground(Object... arg0) {
// TODO Auto-generated method stub
String url = (String) arg0[0];
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> nameValuePairs = null;
if (!(((Activity) con) instanceof MainActivity)){
nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("limit", (String) arg0[1]));
nameValuePairs.add(new BasicNameValuePair("cat_id", cat_id));
}else if (((Activity) con) instanceof MainActivity){
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("limit", (String) arg0[1]));
}
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Execute HTTP Post Request
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}catch (Exception e){;
}
String result = null;
HttpEntity entity = response.getEntity();
try {
result = EntityUtils.toString(entity);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Toast.makeText(con, "Success", Toast.LENGTH_SHORT).show();
}
}
这就是我执行任务的方式
new GetServerData(this, data, adapter, CPATH).execute(URL, ""+current_limit);
doInBackground 方法运行完美,并按应有的方式获取数据。但我需要在 onPostExecute 中发布处理数据。但它不会被调用。我在这里做错了什么?
【问题讨论】:
-
ots 没有显示 toats?把调试点放在那里
-
我认为您在某些地方遇到了一些异常,因为您正在捕获每个异常,所以应用程序没有崩溃..
-
除了你的 issue,不要显示来自 doInBackground 的 toast 消息。
-
知道了,谢谢 :) 但是请在这个问题上提供任何帮助
标签: android android-activity android-asynctask