First of all, I am Android 4.0 above version, this time directly with the online code will be error, first to assign the Internet on the universal code
1String uri = "Http://url";2HttpGet HttpGet =NewHttpGet (URI);3LOG.E ("Zllmsg", "New HttpGet");4 Try {5HttpResponse hr =Newdefaulthttpclient (). Execute (httpget);6 intReturnCode =hr.getstatusline (). Getstatuscode ();7 if(returncode==200{//200 is back to normal.8LOG.E ("Zllmsg", "Woshi 200");9String Fanhuijieguo =entityutils.tostring (Hr.getentity ());//get something back from the serverTenLOG.E ("Zllmsg", Fanhuijieguo); One A}Else{ -LOG.E ("Zllmsg", "Woshi No"); - } the}Catch(clientprotocolexception e) { - //TODO auto-generated Catch block -LOG.E ("Zllmsg", E.getmessage ()); -}Catch(IOException e) { + //TODO auto-generated Catch block -LOG.E ("Zllmsg", E.getmessage ()); +}
This time if it is 4.0 before, should be no problem, if it is 4.0 will error, online said is because 4.0 has not allowed to access the network in the main thread, is to prevent the program suspended animation or what, anyway, it means to open a thread to engage, so the above code before adding the following code is good, It's not quite clear, it's probably just another thread or something.
Strictmode.setthreadpolicy (new StrictMode.ThreadPolicy.Builder (). Detectdiskreads (). Detectdiskwrites (). Detectnetwork (). Penaltylog (). build ()); Strictmode. Setvmpolicy (new StrictMode.VmPolicy.Builder (). Detectleakedsqlliteobjects (). Penaltylog (). Penaltydeath (). build ());
Android, Android Get request submission and exceptions I encountered