我是WebAPI的新手,我被要求在某个网站的API的帮助下获取特定位置的经度和纬度。 (网站已删除),我也获得了资产密钥。 我想我的问题是,如何将这个API导入我的Java程序?
答案 0 :(得分:0)
以下是使用提供的演示密钥从该页面进行资产查询的简单示例:
示例查询
https://api.nasa.gov/planetary/earth/assets?lon=100.75&lat=1.5&begin=2014-02-01&api_key=DEMO_KEY
[这是在Java 6中,修改以适合您的Java版本]
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());
}
}
输出:
{"count": 61, "results": [{"date": "2014-02-04T03:30:01", "id": "LC8_L1T_TOA/LC81270592014035LGN00"}, ...