The reasons for the network collection are as follows, as well as the workaround:
Let me add a summary:
Solution One:
Add the following code after the activity's protected void OnCreate (Bundle savedinstancestate) function that operates the network class (socket connection). In Super.oncreate (savedinstancestate), front Plus.
Strictmode.setthreadpolicy (new StrictMode.ThreadPolicy.Builder () . Detectdiskreads (). Detectdiskwrites (). Detectnetwork (). Penaltylog (). build ()); Strictmode.setvmpolicy(new StrictMode.VmPolicy.Builder () . Detectleakedsqlliteobjects (). Detectleakedclosableobjects () . Penaltylog (). Penaltydeath (). build ());
This method is not recommended. But it is also one of the solutions.
Solution Two:
Opens a new thread. Is the method in the picture above.
New Thread () { @Override publicvoid run () { // The network operation Connection Code mclientmanage.connect (); }
Android about "Networkonmainthreadexception"