//
Parsing JSON from URL on Android/Java
This example shows how to Parse JSON from URL in Android/Java:
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL("http://www.example.com/json.php");
HttpURLConnection con =
(HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
DataOutputStream wr =
new DataOutputStream(con.getOutputStream());
String query = "start=10&end=20";
wr.writeBytes(query);
wr.flush();
wr.close();
if (con.getResponseCode() == 200) {
BufferedReader br = new BufferedReader(
new InputStreamReader(
con.getInputStream()
)
);
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
try {
JSONObject json =
new JSONObject(sb.toString());
System.out.println("Success!");
// json.getString("key");
// json.getInt("key");
} catch (Exception je) {
System.out.println("JSON parsing error");
}
} else {
System.out.println("Server connection error");
}
} catch (Exception e) {
System.out.println("Server connection error");
}
}
});
thread.start();