0

I am new to WebAPIs, and I was asked to acquire the longitude and latitude of a specific location with the help of a certain website's APIs. (website deleted), and I was provided with an asset key as well. I think my question here is, how do I import this API into my program in Java?

user21478621
  • 71
  • 1
  • 9

1 Answers1

0

Here's a simple example of Assets query from that page using the provided demo key:

EXAMPLE QUERY

https://api.nasa.gov/planetary/earth/assets?lon=100.75&lat=1.5&begin=2014-02-01&api_key=DEMO_KEY

[this is in Java 6, modify to suit your Java version]

import java.io.BufferedReader;
import java.lang.StringBuilder;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.net.URL;
import java.net.URLConnection;
import java.io.IOException;

public class NasaRestAPIExample {

    public static void main(String[] args) throws IOException {
        String query = "lon=100.75&lat=1.5&begin=2014-02-01&api_key=DEMO_KEY";

        URLConnection connection = new URL("https://api.nasa.gov/planetary/earth/assets?" + query).openConnection();
        connection.connect();

        BufferedReader r  = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));

        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            sb.append(line);
        }
        System.out.println(sb.toString());
    }
}

output:

{"count": 61, "results": [{"date": "2014-02-04T03:30:01", "id": "LC8_L1T_TOA/LC81270592014035LGN00"}, ...