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