【发布时间】:2018-12-15 14:45:10
【问题描述】:
我在使用 Here API 时遇到了一些问题。当我运行 curl 命令时,我得到一个 JSON 对象,但是当我在我的代码中尝试它时,我不断地返回 HTML。谁能帮帮我吗?我究竟做错了什么? 这是 curl 命令:
curl \
--compressed \
-H 'Accept-Encoding:gzip' \
-H 'Accept-Language:en-US,en;q=0.5' \
--get 'https://places.cit.api.here.com/places/v1/autosuggest' \
--data-urlencode 'app_code=my_code' \
--data-urlencode 'app_id=my_id' \
--data-urlencode 'at=48.13642,11.57755' \
--data-urlencode 'pretty=true' \
--data-urlencode 'q=Hofbräuhaus am Platz'
这是我的 Java 代码:
private void searchForPlacesInMunich(String place) throws IOException {
String link = "https://places.cit.api.here.com/places/v1/autosuggest"
+ "?app_id=" + app_id
+ "&app_code=" + app_code
+ "&at=" + latitude + "%2C" + longitude
+ "&q=" + place
+ "&pretty";
URL url = new URL(link);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Accept-Encoding", "gzip");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
con.setRequestMethod("GET");
con.connect();
System.out.println("Length : " + con.getContentLength());
Reader reader = null;
if ("gzip".equals(con.getContentEncoding())) {
reader = new InputStreamReader(new GZIPInputStream(con.getInputStream()));
}
else {
reader = new InputStreamReader(con.getInputStream());
}
while (true) {
int ch = reader.read();
if (ch==-1) {
break;
}
System.out.print((char)ch);
}
}
【问题讨论】:
-
更多信息,因为我觉得我没有很好地解释问题:问题是:当我通过互联网浏览器发送 GET 请求时,它工作正常(因为它获取 HTML作为响应),但是在页面左下角的“将此请求带回家”下有一个 curl 命令(curl 命令在问题中)。当我在终端中运行命令时,我得到原始 JSON 响应(没有 HTML),但是当我运行我的 Java 代码(也在问题中)时,我只得到 HTML 响应,而不是预期的 JSON .