Problem: when android 4.4 is used to access http, connection. getResponseCode () is not executed and no error is reported. See the red font below:
Public static String getJsonContent (String url_path, String encode) {String jsonString = ""; try {URL url = new URL (url_path); HttpURLConnection connection = (HttpURLConnection) url. openConnection (); connection. setConnectTimeout (3000); connection. setRequestMethod ("GET"); connection. setDoInput (true); // get data from the server int responseCode = connection. getResponseCode (); if (200 = responseCode) {jsonString = changeInp UtStream (connection. getInputStream (), encode);} catch (Exception e) {// TODO: handle exception} // return jsonString;} private static String changeInputStream (InputStream inputStream, String encode) throws IOException {// TODO Auto-generated method stubString jsonString = null; ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); byte [] data = new byte [1024]; int len = 0; while (len = inputSt Ream. read (data ))! =-1) {outputStream. write (data, 0, len);} jsonString = new String (outputStream. toByteArray (), encode); inputStream. close (); return jsonString ;}
This code can directly access http in a java project, and directly access urp_path = http: // 192.168.0.102: 8080/json_Project/servlet/JsonAction in a browser? Action_flag = person also corresponds to prove that the url address is correct.
Solution:
Cause: the HTTP access request is not put in a separate thread but in the main thread.
Solution: Put the http request in a new thread, or add closeStrictMode () to the onCreat () method of the Activity that calls the Http access ().
Public static void closeStrictMode (){
StrictMode. setThreadPolicy (new StrictMode. ThreadPolicy. Builder ()
. DetectAll (). penaltyLog (). build ());
}
Reference: http://hb.qq.com/a/20110914/000054.htm
Zookeeper