onPostExecute从未调用过?

时间:2012-07-01 12:49:53

标签: android

public static class TestTask extends AsyncTask<Void, Integer, Integer> {

    private String stacktrace;

    public TestTask (String stacktrace){

        this.stacktrace = stacktrace;
    }

    @Override
    protected Integer doInBackground(Void... params) {

        try {
            Log.i("async", "doInBackground 1"); //this gets logged
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://xx.xx:8080/android/service.php");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("action", "logexception"));
            nameValuePairs.add(new BasicNameValuePair("stacktrace", stacktrace));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            Log.i("async", "doInBackground 2"); //this gets logged
            return 1;

        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

    protected void onPreExecute(){
        Log.i("async", "onPreExecute"); //this gets logged
    }

    @Override
    protected void onPostExecute(Integer result) {
        Log.i("async", "onPostExecute"); //this doenst get logged!


    }
}

我一直在检查其他SO线程,但据他们说,我的代码看起来是正确的,据我所知。那为什么我永远不会到达Log.i("async", "onPostExecute");?感谢

2 个答案:

答案 0 :(得分:1)

您是否在AsyncTask主题上创建了UI? 其他人似乎很好。泛型和注释都很好。

因此问题可能是您的doInBackground方法永远不会返回,因为onPostExecute会在doInBackground返回时自动调用{。}}。

答案 1 :(得分:-1)

您必须拨打super

中的onPostExecute()

所以你的代码应该是这样的:

@Override
public void onPostExecute(Integer result) {
    super.onPostExecute(result);
    Log.i("async", "onPostExecute");
}

这应该有效