我正在获取HTTPS URL的java.io.FileNotFoundException

时间:2019-03-13 11:52:51

标签: java android file

我的代码是这样的:

        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获取人类可读的地址,也称为反向地址编码。我已经尝试了很多事情,但是没有任何效果,所以如果您告诉我我做错了什么,我将非常感谢。如果可能的话,我宁愿不使用任何外部库。

2 个答案:

答案 0 :(得分:0)

我编写了此代码块并找到了解决方案。您可以查看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();
    }

答案 1 :(得分:0)

实际上,该问题现在似乎已消失,因为已纠正的唯一问题是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());

谢谢大家的回答,问题已解决!