我试图创建一个 AsyncTask 来从服务器端获取一些数据。但它在“IOUtils.copy((response_.getEntity().getContent()), writer);”行的 onPostExecute() 中报告了 Null 指针异常。我使用一些日志来查看发生了什么,发现日志 1 和 3 已打印但日志 2 没有。为什么 onPostExecute 在 client.executed() 完成之前执行?任何人都可以提供一些建议吗?
private class GetForm extends AsyncTask<String, Integer, Object> {
private HttpResponse response_;
private Exception exception_;
private String url_;
public GetForm(String url) {
super();
url_ = url;
}
protected Object doInBackground(String... params) {
try {
Log.i("mylog","inside doInbackground 1.");
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url_);
response_ = client.execute(request);
Log.i("mylog","inside doInbackground 2.");
} catch (Exception e) {
exception_ = e;
}
return response_;
}
@Override
protected void onPostExecute(Object o) {
// Your current UI stuff here.
Log.i("mylog","inside onPostExecute 3.");
StringWriter writer = new StringWriter();
try{
IOUtils.copy((response_.getEntity().getContent()), writer);
......
}catch(IOException e) {
e.printStackTrace();
}
}
}