Android is a perfect json request format, androidjson

Source: Internet
Author: User

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 ();
}

Related Article

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.