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.