Parsing httpurlconnection to get response
•
Android
I have a method that uses the httpurlconnection get method to call the API,
Please find my way to write:
private String sendGet(CallWebserviceActivity context) throws Exception {
String url = "http://myurl/";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setDoOutput(true);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'Get' request to URL : " + url+"--"+responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("Response : -- " + response.toString());
return response.toString();
}
Here, the result I get is a string in JSON object format. I want to be able to take result as the value of the key so that it can be displayed in Android activities
resolvent:
Build jsonobject from string. Then get value from key
JSONObject jsonResponse = new JSONObject(response.toString());
String value = jsonResponse.getString("Key");
Note that the string should be JSON. XML in the correct format
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码