The Android client determines whether the server is open httphostexception solution

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.