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?
Asked
Active
Viewed 2,135 times
0
-
1I just quickly glanced at the link, but this looks like a RESTful web service. The question here addresses how to do it: http://stackoverflow.com/questions/3913502/restful-call-in-java – Christopher Schneider Feb 04 '17 at 00:54
-
@ChristopherSchneider Thank you very much, the link is very helpful :) – user21478621 Feb 04 '17 at 02:34
1 Answers
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"}, ...

chickity china chinese chicken
- 7,709
- 2
- 20
- 49
-
This is not a solution. It's hard-coding a single query and getting a single result. Well established and robust REST Java libraries exist that should be preferred. – Christopher Schneider Feb 04 '17 at 03:43
-
I just provided an example of what the OP asked. If it is not considered a solution, why did the OP accept it as an answer? – chickity china chinese chicken Feb 04 '17 at 03:46
-
Fair point. I don't think the OP knew what they wanted, though. – Christopher Schneider Feb 04 '17 at 03:49
-
If this answer didn't serve their purpose, they shouldn't have accepted it. If you can provide a *real* solution, I will gladly delete my answer, if possible. – chickity china chinese chicken Feb 04 '17 at 03:51
-
I could write a _real_ solution easily, but I won't. I won't do someone else's work/homework on a trivial problem, but have no problem helping/pointing in the right direction. You or I may have to work with this person in the future, so it's best they try to figure it out themselves. I apologize if you were offended; that wasn't my intent. – Christopher Schneider Feb 04 '17 at 04:00
-
I respect and appreciate you sharing that. no offense taken, I learn too, thank you. – chickity china chinese chicken Feb 04 '17 at 06:25
-
Sorry, I took this answer because I felt it help me understand what I am trying to do. Maybe I shouldn't accept it as answer because it didn't "actually answer" my question. – user21478621 Feb 04 '17 at 07:11
-
Yes, if we can remove and delete it, is probably best, sorry if I suggested something inappropriate. – chickity china chinese chicken Feb 04 '17 at 07:12
-
@downshift I really appreciate your help. I was lost and I think your suggestion is very helpful. This is why I accepted an answer instead of waiting for more people to "do my homework" for me. – user21478621 Feb 04 '17 at 07:14
-
No worries, I don't understand the protocol sometimes. If you can unaccept this answer, I tried to delete it, but it says "you can't delete an accepted answer". – chickity china chinese chicken Feb 04 '17 at 07:26