Home  >  Article  >  Backend Development  >  The android client accesses the server it created and returns JSON data for parsing and learning.

The android client accesses the server it created and returns JSON data for parsing and learning.

WBOY
WBOYOriginal
2016-08-08 09:26:32772browse

Recently I am looking for use cases about client access server development. I always visit other people’s websites and cannot modify the data inside. I don’t know how to achieve it. I applied for a free server website online and uploaded a php file. , now you can access the information on the server through urlStr===http://1.hellowes.sinaapp.com/, and the server will return a data. Since I don’t know PHP at all, what is returned on the server is not the real data. JSON data, so I had to combine the client string into a JSON statement and parse it through JSONObject.

The implementation code is posted below, and finally I can get the 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.setConnectTime out (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") ; .printStackTrace();
throw new Exception("Failed to access network 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 how the android client accesses the server established by itself and returns JSON data for parsing and learning, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP basic constantsNext article:PHP basic constants