After Android3.0, it will be found that the runtime will make an error if the HTTP request is written in the main thread (that is, activity), because Android after 3.0 is in order to prevent the application of the ANR (aplication not Response) exception.
There are two ways to solve this problem:
1. You can add such a piece of code to the OnCreate () method of the activity, as follows:
1 if (Build.VERSION.SDK_INT >=) { 2 strictmode.setthreadpolicy (new StrictMode.ThreadPolicy.Builder (). Detectdiskreads (). Detectdiskwrites (). Detectnetwork (). Penaltylog (). build ()); 3 Strictmode.setvmpolicy (new StrictMode.VmPolicy.Builder (). Detectleakedsqlliteobjects (). Detectleakedclosableobjects (). Penaltylog (). Penaltydeath (). build ()); 4 }
This allows you to perform network operations in the main thread.
2. The general situation is that we should do this
Start a child thread to make your network request.
Of course, if your application executes a small amount of network request data, you can use the first scenario.
HTTP requests cannot be made in the main thread after resolving Android3.0