Skip to content Skip to sidebar Skip to footer

Networkonmainthreadexception With Asynctask

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 extend

Solution 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?

Post a Comment for "Networkonmainthreadexception With Asynctask"