Android Studio 中的 FileNotFoundException,但在浏览器中工作正常

FileNotFoundException in Android Studio, but works fine in browser

我正在尝试连接到网站以接收一些 JSON 信息。当我 运行 Android Studio 中的应用程序使用连接的 Nexus 7 设备时,我得到一个 java.io.FileNotFound 异常,但是如果我单击未找到的文件的名称,响应是预期的立即显示在我的浏览器中。这对我来说是一个新的应用程序,但我过去做过类似的事情,但效果很好。在过去的两天里,我一直在尝试多种方法,但似乎找不到问题所在。当我调用 connection.getInputStream() 时,代码崩溃了。所有这些都在 AsyncTask 中 运行ning。

我的代码

public byte[] getUrlBytes(String urlSpec) throws IOException{
    URL url = new URL(urlSpec);
    // urlSpec: https://api.weather.gov/points/48.0174,-115.2278

    try {
        connection = (HttpsURLConnection) url.openConnection();
    } catch (IOException ioe) {
        Log.d(TAG, "connection ioe: " + ioe.toString());
        Toast.makeText(context, "@string/can_not_connect",
                Toast.LENGTH_LONG).show();
    }


    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        InputStream in = connection.getInputStream(); *** Blows up here ****

        if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            Log.d(TAG, "connection Response code: " + 
                  connection.getResponseMessage());
        }

        int bytesRead = 0;
        byte[] buffer = new byte[1024];
        while ((bytesRead = in.read(buffer)) > 0) {
            out.write(buffer, 0, bytesRead);
        }

        out.close();
        return out.toByteArray();
    } finally {
        connection.disconnect();
    }
}

Logcat

04-02 15:40:59.693 32471-32495/com.drme.weathertest E/WeatherFetcher: Failed 
 to fetch items
java.io.FileNotFoundException: https://api.weather.gov/points/48.0174,-115.2278 
*** Note that if I click on this file name, it works in my browser ***
    at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)       
    at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)   
    at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
    at com.drme.weatherNoaa.WeatherFetcher.getUrlBytes(WeatherFetcher.java:148)
    at com.drme.weatherNoaa.WeatherFetcher.getUrlString(WeatherFetcher.java:183)
    at com.drme.weatherNoaa.WeatherFetcher.downloadGridPoints(WeatherFetcher.java:202)
    at com.drme.weatherNoaa.WeatherFetcher.requestForecast(WeatherFetcher.java:262)
    at com.drme.weatherNoaa.WeatherFragment$SearchTask.doInBackground(WeatherFragment.java:329)
    at com.drme.weatherNoaa.WeatherFragment$SearchTask.doInBackground(WeatherFragment.java:296)
    at android.os.AsyncTask.call(AsyncTask.java:292)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)

感谢您的帮助。

在进一步阅读网络服务文档(其中一些对我来说似乎有点单薄)和网络上的一些其他帖子后,答案是它们需要验收和 User-agent headers要求。我进行了这些更改,代码现在可以正常工作了。

新静态常量

private static final String ACCEPT_PROPERTY = "application/geo+json;version=1";
private static final String USER_AGENT_PROPERTY = "xxxx.com (xxxxxxxxx@gmail.com)";

代码更改

 connection = (HttpsURLConnection) url.openConnection();
 connection.setRequestProperty("Accept", ACCEPT_PROPERTY);  // added
 connection.setRequestProperty("User-Agent", USER_AGENT_PROPERTY); // added

不需要其他更改,尽管我花了一段时间才弄清楚如何添加 headers 以及它们的格式可能是什么。

感谢您查看此内容。