NetworkOnMainThreadException with AsyncTask

前端 未结 1 1321
栀梦
栀梦 2021-01-28 07:23

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 :

publi         


        
相关标签:
1条回答
  • 2021-01-28 07:59

    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?

    0 讨论(0)
提交回复
热议问题