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.