1

So I'm at lost how to do this: the goal is simply to make a call to the openweather api in java and have the result be returned on the console. I can't find any tutorials on how to do this, only on how to parse JSON data from another file...

Erm is this going in the right direction? No idea. Modified as per suggestion to attempt to use Gson

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;


public class ApiJSONRead {

    URL apiURL = new URL("http://api.openweathermap.org/data/2.5/find?q=London&APPID=(idhere)");

    public static void main(String[] args) throws IOException {



        JsonObject jobj = new Gson().fromJson(apiURL, JsonObject.class);
        var Jsonresponse = jobj.get("weather").getAsString();


        System.out.println(Jsonresponse);

    }

}
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
Mehmet
  • 95
  • 9
  • *No idea*: you would get an idea by executing your code, and reading error messages. A FileInputStream, as its name (and its javadoc) indicate, is used to read from files. Not from URLs. How could you get an InputStream from a URL? The javadoc of URL could maybe tell you about that. – JB Nizet Feb 18 '17 at 17:28
  • See: http://stackoverflow.com/questions/2793150/using-java-net-urlconnection-to-fire-and-handle-http-requests – Jorn Vernee Feb 18 '17 at 18:05

4 Answers4

1

Sort of, although as one of the commenters points out, run your code. It won't work. Once you get the stream, what you have should work. But the apiURL isn't a file, so FileInputStream won't understand how to read it. Check out Apache HttpComponents. Specifically, here's an example that shows how to make a GET request, which is exactly what you're trying to do.

Chris Thompson
  • 35,167
  • 12
  • 80
  • 109
1

To parse JSON data use can use google API gson

e.g.

    JsonObject jobj = new Gson().fromJson(response, JsonObject.class);
    jsonresponse = jobj.get("message").getAsString();
  • Thanks Vikrant, would you have any more complete usage examples? I can only find examples using files, but no examples anywhere on how to read the data from the api url... – Mehmet Feb 18 '17 at 17:55
  • You can hit the url by using apache http client,HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet("http://ip.jsontest.com/"); org.apache.http.HttpResponse httpResponse = client.execute(request); String response = EntityUtils.toString(httpResponse.getEntity()); System.out.println(response); – Vikrant verma Feb 18 '17 at 18:15
1

check the below code and import necessary jars

import java.io.IOException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;
import com.google.gson.JsonObject;


public class ApiJSONRead {



    public static void main(String[] args) throws IOException {


        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://ip.jsontest.com/");
        org.apache.http.HttpResponse httpResponse = client.execute(request);
        String response = EntityUtils.toString(httpResponse.getEntity());
        System.out.println(response);
        JsonObject jobj = new Gson().fromJson(response, JsonObject.class);
        String Jsonresponse = jobj.get("ip").getAsString();
        System.out.println(Jsonresponse);
    }

}
0

I am committer of the Sprinkler opensource project. See http://wiki.bitplan.com/index.php/Sprinkler As part of the project I am implementing the necessary open weather API calls. You might want to have a look at

https://github.com/BITPlan/com.bitplan.sprinkler/blob/master/src/test/java/com/bitplan/sprinkler/TestWeatherReport.java

and

https://github.com/BITPlan/com.bitplan.sprinkler/blob/master/src/main/java/org/openweathermap/weather/WeatherReport.java

There is also a JsonUtil helper class that hides the details of how to get the json string from an url.

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186