The Android client accesses its own established server and returns JSON data for parsing and learning

Source: Internet
Author: User
Tags server website
Recently looking for a client Access server development use cases always go to visit other people's website can not modify the data inside and do not know how to achieve, oneself in the online application of a free server website uploaded a php file, now you can pass URLSTR===HTTP://1. hellowes.sinaapp.com/access to the information on the server, and the server will return a data, because the PHP point does not understand that the server returned is not the real JSON data, So we had to parse it through the jsonobject by combining the client string into a JSON statement.

The implementation code is posted below, it is finally possible to get information from the server

Public Jsonobject Getweb (String urlstr) throws exception{

StringBuffer sb = new StringBuffer ();
try {
URL url = new URL (urlstr);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Conn.setrequestmethod ("GET");
Conn.setconnecttimeout (5000);
Conn.setdoinput (TRUE);
Conn.setdooutput (TRUE);
if (conn.getresponsecode () = = 200) {
InputStream is = Conn.getinputstream ();
int len = 0;
byte[] buf = new byte[1024];
while (len = Is.read (BUF))! =-1) {
Sb.append (New String (buf, 0, Len, "UTF-8"));
}
Is.close ();
}else{
throw new Exception ("Access network failed 00");
}

} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
throw new Exception ("Access network failed 11");
}
System.out.println ("---------" +sb.tostring ());
String htmlstr = sb.tostring ();
Htmlstr = Htmlstr.replaceall ("\" "," \ ");
Htmlstr = "{' Singer ':" +htmlstr+ "}";
System.out.println ("htmlstr====" +htmlstr);
Jsonobject jsonobj = null;
try {
Jsonobj = new Jsonobject (HTMLSTR). Getjsonobject ("singer");
System.out.println ("jsonobj====" +jsonobj);
} catch (Jsonexception E1) {
TODO auto-generated Catch block

E1.printstacktrace ();
}

return jsonobj;

}

The above describes the Android client access to its own established server and return JSON data for parsing learning, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.