Android more complete JSON request format

Source: Internet
Author: User

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

Above is the JSON request method, the parameter only need to pass a corresponding server address on the line, the following is the parsing 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 ();
}

Android more complete JSON request format

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.