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