【问题标题】:AsyncTask not executing onPostExecuteAsyncTask 未执行 onPostExecute
【发布时间】: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


【解决方案1】:

您在 doInbackground 中显示 toast 消息,这违反了异步任务,所有与 ui 相关的东西都应该放在 postExecute 中。

请删除这些行。

【讨论】:

  • 确实如此,我也在评论中指出了这一点,但如果没有抛出异常,这不可能是 onPostExecute() 没有被执行的原因。
  • 我已经注释掉了 doInBackground 中的所有 Toast 行。但是问题仍然存在 onPostExecute 仍然没有被调用。我已经调用了 new GetServerData(this, data, adapter, CPATH).execute(URL, ""+current_limit); 这一行来自 Activity 的 onCreate() 方法
  • 什么是 current_limit,你是从主界面调用这个任务
  • 是的,我从 Activity 的 onCreate 调用它。并且当前限制在这里只是一个字符串。
【解决方案2】:
}catch (Exception e){
   Toast.makeText(con, "Unable to fetch data", Toast.LENGTH_SHORT).show();
}

最后一个 catch 块中的 Toast.makeText 正在引发异常。我认为这清楚地表明您不应该尝试从后台线程显示 toast。

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

【讨论】:

  • 我已经删除了代码中的所有祝酒词。但是异常不是问题,因为方法 doInBackground 成功执行且没有错误。但仍然没有调用 onPostExecute。
【解决方案3】:

问题以一种非常意想不到的方式解决了。重新启动 eclipse 解决了我在 Eclipse 中查看的问题没有正确构建项目。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2013-04-07
    相关资源
    最近更新 更多