2
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");?谢谢

4

2 回答 2

1

AsyncTaskUIThread 上创建了吗?其他看起来不错。泛型和注释都很好。

所以可能的问题是你的doInBackground方法永远不会返回,因为onPostExecute当某些东西返回时会自动调用doInBackground

于 2012-07-01T13:27:45.700 回答
-1

你必须打电话superonPostExecute()

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

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

这应该有效

于 2012-07-01T13:06:39.043 回答