AsyncTask没有执行onPostExecute

时间:2014-08-22 16:22:08

标签: android android-activity android-asynctask

我正在处理一个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中处理数据。但它没有被调用。我在这做错了什么?

3 个答案:

答案 0 :(得分:1)

您正在doInbackground中显示toast消息,这违反了异步任务,所有与ui相关的内容应该在postExecute中进行。

请删除这些行。

答案 1 :(得分:1)

}catch (Exception e){
   Toast.makeText(con, "Unable to fetch data", Toast.LENGTH_SHORT).show();
}

最后一个catch块中的Toast.makeText正在抛出异常。我认为这很清楚你不应该试图从后台线程中显示祝酒词。

您应该能够在logcat中看到堆栈跟踪。

答案 2 :(得分:0)

问题以一种非常意外的方式解决了。重新启动eclipse修复了问题,因为我在Eclipse中查看并没有正确构建项目。