Android to determine whether the server is turned on, try a lot of methods do not (if the server does not open will be stuck in HttpResponse that), some people say that the higher version of the Android program does not allow access to the main thread in the network (the main thread can read and write network streams) to be verified.
Method One:
Added in Xxxactivity's OnCreate () method
Strictmode.threadpolicy policy = new StrictMode.ThreadPolicy.Builder (). Permitall (). build (); Strictmode.setthreadpolicy (Policy);
These two lines of code were added to the activity I wrote, and this method did not solve my problem, go on ...
Solution 2:
Package Siat.hpc.ngb.utils;import Java.io.ioexception;import Org.apache.http.httpresponse;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.methods.httpget;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.params.basichttpparams;import Org.apache.http.params.httpconnectionparams;import Org.apache.http.params.httpparams;import Android.os.asynctask;import Android.util.log;public class ConnServer Extends asynctask<string,string,string>{/** * * To determine if the server is on * @param path network server address * @return * Server on * Server is not turned on */@o verrideprotected string Doinbackground (String ... params) {//TODO auto-generated method Stubtry {HttpGet get = new HttpGet (Params[0]); LOG.I ("DoInBack1 ...", params[0]); HttpResponse response = new Defaulthttpclient (). Execute (GET); LOG.I ("DoInBack2 ...", params[0]); if (Response.getstatusline (). Getstatuscode () = = () {return "Success";}} catch (Clientprotocolexception e) {//TODO auto-Generated catch blocke.printstacktrace (); return "Clienterror";} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace (); return "Servererror";} Return "Success";}}
Call directly where needed:
。。。 <pre name= "code" class= "java" style= "FONT-SIZE:18PX;" >.. Appconstant.base_url is a string of http: such as http://172.21.6.233:8080/
}
Refer:http://stackoverflow.com/questions/19162272/httpclient-executehttppost-on-android-4-2-error
The Android client determines whether the server is open httphostexception solution