问题
I need to do some HTTP requests from my android app. I use AsyncTask as needed, with the network part in doInBackground.
Here is my request class :
public class AsyncRequest extends AsyncTask<Void, Integer, HttpResponse>{
private ProgressDialog dialog;
private Activity activity;
private HttpUriRequest query;
private HttpClient client;
private HttpResponse response;
private String dialogMsg;
private String uri;
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public AsyncRequest(Activity a, HttpUriRequest q, HttpClient c, String m) {
query = q;
client = c;
activity = a;
dialog = new ProgressDialog(a);
dialogMsg = m;
}
@Override
protected void onPostExecute(HttpResponse res) {
if (dialog.isShowing())
dialog.dismiss();
}
protected void onPreExecute() {
this.dialog.setMessage(dialogMsg);
this.dialog.show();
}
@Override
protected HttpResponse doInBackground(Void... arg0) {
try {
response = client.execute(query);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
protected HttpResponse getResponse() {
return response;
}
}
And, in my UI thread, I use it this way :
AsyncRequest request;
HttpClient httpClient = new DefaultHttpClient();
HttpGet getQuery = new HttpGet("Here goes my url");
HttpResponse response = null;
String rt = "Null";
getQuery.setHeader("Authorization", getIntent().getExtras().getString("token"));
request = new AsyncRequest(this, getQuery, httpClient, "Loading events...");
try {
response = request.execute().get(3, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
try {
rt = EntityUtils.toString(entity);
} catch (Exception e) {
e.printStackTrace();
return ;
}
So, I'm an android beginner, but I guess this exception is caught when network related code is executed in main thread. But I run client.execute() in my doInBackground() method. Is it the call to execute().get() (in ui thread) that causes the problem ?
回答1:
You have this
response = request.execute().get(3, TimeUnit.SECONDS);
Calling get()
makes AsyncTask
no more Asynchronous. It blocks the ui thread waiting for the response. This leads to NetworkOnMainThreadException
.
Get rid of get(3, TimeUnit.SECONDS);
Just use request.execute()
. You can update ui in onPostExecute
. You can also use interface as a callback to the Activity.
Check the answer by blackbelt in the below link
How do I return a boolean from AsyncTask?
来源:https://stackoverflow.com/questions/22757607/networkonmainthreadexception-with-asynctask