gpt4 book ai didi

android - onPostExecute 从未调用过?

转载 作者:行者123 更新时间:2023-11-29 13:57:19 26 4
gpt4 key购买 nike

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

最佳答案

您是否在 UI 线程上创建了您的 AsyncTask?其他好像不错泛型和注解都很好。

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

关于android - onPostExecute 从未调用过?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11282085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com