To get the response from the HTTP response you would need to do this:
public void getHTTPResponse() {
try {
URL url = validateAddress(
"http://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=XXXxxxxx");
//validate url using some method to escape any reserved characters
BufferedReader reader = null;
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
//create the connection and open it
reader = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream()));
// start reading the response given by the HTTP response
StringBuilder jsonString = new StringBuilder();
// using string builder as it is more efficient for append operations
String line;
// append the string response to our jsonString variable
while ((line = reader.readLine()) != null) {
jsonString.append(line);
}
// dont forget to close the reader
reader.close();
// close the http connection
httpsURLConnection.disconnect();
// start parsing
parseJSON();
} catch (IOException e1) {
System.out.println("Malformed URL or issues with the reader.");
e1.printStackTrace();
}
}
Parsing the JSON using GSON
private void parseJSON() {
Gson gson = new Gson();
// let GSON do the work
SomeObject[] fromJSON = gson.fromJson(jsonString, SomeObject[].class);
// add to our list or do whatever else you want from here onwards.
ArrayList<SomeObject> o= new ArrayList<SomeObject>(Arrays.asList(fromJSON));
}
How to get GSON
Include the following in your maven pom.xml file
<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
Or if you are using gradle do this
dependencies {
compile 'com.google.code.gson:gson:2.8.1'
}
Don't forget to use mavenCentral() as the repository.
If you're not using maven or gradle - I recommend you do. It will make managing your APIs much much easier than downloading JARs etc.
(Just noticed OP is using maven, but I'll leave it in for others)
Hope this helped.