The android client accesses a self-built server and returns JSON data for parsing and learning.

Source: Internet
Author: User
Tags server website
: This article describes how to use the android client to access a self-built server and return JSON data for parsing. For more information about PHP tutorials, see. Recently, I was looking for a client to access the server for development. I always visit other websites and cannot modify or implement the data, I have applied for a free server website on the Internet and uploaded a php file. now I can use urlStr ==== login,

The following is the implementation code to obtain 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 [1, 1024];
While (len = is. read (buf ))! =-1 ){
Sb. append (new String (buf, 0, len, "UTF-8 "));
}
Is. close ();
} Else {
Throw new Exception ("Network access failed 00 ");
}

} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
Throw new Exception ("Network access 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 introduces the android client to access the self-built server and return JSON data for parsing and learning, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.