Android is a perfect json request format, androidjson
Public static String getHttpText (String url ){
If (MyApplication. FOR_DEBUG ){
Log. I (TAG, "[getHttpText1]" + url );
}
Log. I (TAG, "[getHttpText2]" + url );
If (url = null | url. equals (""))
Return null;
StringBuilder builder = new StringBuilder ();
InputStreamReader isReader = null;
HttpURLConnection conn = null;
Try {
URL u = new URL (url );
Conn = (HttpURLConnection) u. openConnection ();
Conn. setConnectTimeout (TIMEOUT );
If (conn = null | conn. getResponseCode ()! = HttpURLConnection. HTTP_ OK)
Return null;
Conn. connect ();
IsReader = new InputStreamReader (conn. getInputStream ());
BufferedReader reader = new BufferedReader (isReader );
String buffer;
While (buffer = reader. readLine ())! = Null ){
Builder. append (buffer );
}
Reader. close ();
Return builder. toString ();
} Catch (Exception e ){
Log. e (TAG, "getHttpText error:" + e. getMessage ());
} Finally {
If (isReader! = Null ){
Try {
IsReader. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
If (conn! = Null ){
Conn. disconnect ();
}
}
Return null;
}
The above is the json request method, the parameter only needs to pass a corresponding server address, the following is the resolution format
String json = HttpUtils. getHttpText (
Configs. getServerAddress (context)
+ "/Api. php/Message/getMessage"
+ Configs. getRoomInfo (context ));
If (json = null | json. equals (""))
Continue;
Try {
JSONObject root = new JSONObject (json );
If (root. getInt ("status ")! = 0)
Return;
Boolean gotMessage = false;
JSONArray ja = root. getJSONArray ("messageList ");
Int len = ja. length ();
Long now = System. currentTimeMillis ();
For (int I = 0; I <len; I ++ ){
JSONObject jo = ja. getJSONObject (I );
Long deadline = jo. getLong ("finish_time") * 1000;
If (deadline <= now) continue;
MarqueeContent content = new MarqueeContent ();
Content. id = mMarqueeId ++;
Content. deadline = deadline;
Content. text = jo. getString ("content ");
MMarqueeList. add (content );
GotMessage = true;
}
If (gotMessage ){
Context. sendBroadcast (new Intent (MarqueeView. GOT_MARQUEE_ACTION ));
}
Return;
} Catch (JSONException e ){
E. printStackTrace ();
}