【问题标题】:I'm getting java.io.FileNotFoundException for HTTPS URL我收到 HTTPS URL 的 java.io.FileNotFoundException
【发布时间】: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

标签: java android file


【解决方案1】:

我编写了这个代码块并找到了解决方案。您可以查看 setRequestProperty 方法的参数

    String response = null;
    try {
        URL url = new URL("https://nominatim.openstreetmap.org/reverse?format=json&lat=44.400000&lon=26.088492&zoom=18&addressdetails=1");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
        connection.connect();
        connection.getResponseCode(); //if you want to check response code
        InputStream stream = connection.getErrorStream();
        if (stream == null) {
            stream = connection.getInputStream();
            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());
    }
        } catch (Exception e) {
        e.printStackTrace();
    }

【讨论】:

  • 非常感谢您抽出宝贵时间,感谢您愿意提供帮助。我发现了问题,现在将发布工作代码
【解决方案2】:

事实上,这个问题现在似乎已经消失了,因为唯一更正的是 addRequestProperty 而不是 setRequestProperty 和用户代理数据,但我认为这不是那么重要。我对 add 和 set requestproperty 不太熟悉,也不知道到底有什么区别,但在这种情况下似乎很重要。

        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("GET"); //POST or GET no matter
        connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0");
        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());

谢谢大家的解答,问题解决了!

【讨论】:

    猜你喜欢
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 2015-09-01
    • 2016-06-30
    • 2020-10-08
    • 2018-05-17
    相关资源
    最近更新 更多