【发布时间】:2019-03-13 11:52:50
【问题描述】:
我的代码是这样的:
URL url = new URL("https://nominatim.openstreetmap.org/reverse?format=json&lat=44.400000&lon=26.088492&zoom=18&addressdetails=1");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
connection.setRequestProperty("Accept-Language","en-US");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder json = new StringBuilder(1024);
String tmp;
while ((tmp = reader.readLine()) != null) json.append(tmp).append("\n");
reader.close();
JSONObject data = new JSONObject(json.toString());
但是,我在 BufferedReader 收到 java.io.FileNotFoundException。地址正确,任何浏览器都会显示json结果。我需要从 lat 和 lon 获取人类可读的地址,也称为反向地理编码。我尝试了很多东西,但没有任何效果,所以如果你告诉我我做错了什么,我将非常感激。如果可能的话,我宁愿不使用任何外部库。
【问题讨论】:
-
您是否有理由不使用 Retrofit 来解决如此简单的问题?
-
他说他不喜欢第三部分库@ZUNJAE。弗拉基米尔你从服务器得到 403 异常。服务器返回 HTTP 响应代码:403 用于 URL:nominatim.openstreetmap.org/…
-
我从未使用过 Retrofit,没有理由,但是我想知道为什么代码不能按预期工作。正如你所说的,这很简单
-
@Beyazidy 是否有解决该问题的方法。为什么我使用浏览器没有 403 响应?
-
请求方法应该是
GET而不是POST