Read JSON object using java


import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import org.json.JSONArray;
import org.json.JSONObject;
import org.testng.Reporter;
public class ReadJsonObject {

private String URL;

public ReadJsonObject(String url) {
this.URL = url;
}

public void aptTesting() throws Exception {
try {
URL url = new URL(this.URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");

if (conn.getResponseCode() != 200) {
throw new RuntimeException(" HTTP error code : "
+ conn.getResponseCode());
}

Scanner scan = new Scanner(url.openStream());
String entireResponse = new String();
while (scan.hasNext())
entireResponse += scan.nextLine();

System.out.println("Response : " + entireResponse);
scan.close();

JSONObject obj = new JSONObject(entireResponse);
String responseCode = obj.getString("status");
System.out.println("status : " + responseCode);

JSONArray arr = obj.getJSONArray("results");
for (int i = 0; i < arr.length(); i++) {
String placeid = arr.getJSONObject(i).getString("place_id");
System.out.println("Place id : " + placeid);
String formatAddress = arr.getJSONObject(i).getString("formatted_address");
System.out.println("Address : " + formatAddress);

//validating Address as per the requirement
if(formatAddress.equalsIgnoreCase("Chicago, IL, USA"))
{
System.out.println("Address is as Expected");
} else {
System.out.println("Address is not as Expected");
}
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Reading HTTP server response using java


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class ReadServerResponse {

private static final String USER_AGENT = "Mozilla/5.0";
private String URL;

public ReadServerResponse(String url){
this.URL = url;
}

public static void main(String[] args) {
ReadServerResponse readServerResponse =
new ReadServerResponse("<Your UR>");
StringBuffer response = readServerResponse.getServerResponse();
System.out.println(response);
}

public StringBuffer getServerResponse() {
StringBuffer buffer = new StringBuffer();
try {
URL unifromResourceIdentifier = new URL(this.URL);
HttpURLConnection httpConnection = (HttpURLConnection) unifromResourceIdentifier.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setRequestProperty("User-Agent", USER_AGENT);
int statusCode = httpConnection.getResponseCode();
if(statusCode == HttpURLConnection.HTTP_OK) {
InputStream is = httpConnection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));

String inputLine;

while ((inputLine = in.readLine()) != null) {
               buffer.append(inputLine);
           }
           in.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return buffer;
}
}