Connection.getresponsecode () is not executed when HTTP is accessed in Android, and an error org.json.JSONException:End of input at character 0.

Source: Internet
Author: User

Problem: When using Android 4.4 to write Android access to HTTP, Connection.getresponsecode () will not be executed, nor error: But thrown org.json.JSONException:End of input at Character 0 of. Exceptions:

Connection code:

 Public Staticstring Getjsoncontent (String url_path) {Try{System.out.println ("Url_path---->>:" +Url_path); URL URL=NewURL (Url_path); HttpURLConnection connetion=(HttpURLConnection) url.openconnection (); Connetion.setconnecttimeout (3000); Connetion.setrequestmethod ("GET"); Connetion.setdoinput (true); System.out.println ("Connetion---->>:" +connetion); intCode=Connetion.getresponsecode (); System.out.println ("Code---->>>:" +code); if(code==200){                        returnChangeinputstream (Connetion.getinputstream ()); }Else{System.out.println ("---->> request code is not correct!!! "); return""; }    } Catch(Exception e) {e.printstacktrace (); }    return"";}
View Code

This code is directly accessible to HTTP in the Java project and is accessed directly by the browser url_path=http://192.168.111.102:8080/jsonproject/servlet/jsonaaction?action _flag=person also has the corresponding, proof URL address is right.


Workaround:
Cause: The request to access HTTP is not placed on a separate thread but on the main thread.
Workaround, place the HTTP request separately in a new thread, or Oncreat () method Nega: Closestrictmode () on the activity that invokes this HTTP access.

public static void Closestrictmode () {
Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder ()
. Detectall (). Penaltylog (). build ());
}
Reference article: http://hb.qq.com/a/20110914/000054.htm

Connection.getresponsecode () is not executed when HTTP is accessed in Android, and an error org.json.JSONException:End of input at character 0.

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.