响应代码为 200 的 HttpURLConnection FileNotFoundException

HttpURLConnection FileNotFoundException with Response Code 200

我正在尝试从 Twitch 的 Helix API 检索一些带有 GameId 的视频。使用 REST 客户端我可以检索数据并且它可以工作,但是当我在 android 上尝试它时它不会工作。 我得到一个响应代码 200,但是当我尝试访问 InputStream 时,我得到一个 FileNotFoundException。

这是我使用的代码:

        HttpURLConnection urlConnection = null;
        URL url = new URL(urlString);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestProperty("Client-ID", Configuration.TWITCH_CLIENT_ID);
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();

        System.out.println("ResponseCode: " + urlConnection.getResponseCode());

        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
        StringBuilder sb = new StringBuilder();

        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        br.close();

        String jsonString = sb.toString();
        System.out.println("JSON: " + jsonString);

        return new JSONObject(jsonString);

其中 Configuration.TWITCH_CLIENT_ID 是我的 Twitch 客户端 ID。 我得到的输出是:

01-16 02:19:19.658 21184-21242/ec.com.aurinow.aurinow I/System.out: ResponseCode: 200
01-16 02:19:20.133 21184-21242/ec.com.aurinow.aurinow W/System.err: java.io.FileNotFoundException: https://api.twitch.tv/helix/videos?game_id=9435
01-16 02:19:20.133 21184-21242/ec.com.aurinow.aurinow W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)
01-16 02:19:20.133 21184-21242/ec.com.aurinow.aurinow W/System.err:     at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
01-16 02:19:20.133 21184-21242/ec.com.aurinow.aurinow W/System.err:     at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
01-16 02:19:20.133 21184-21242/ec.com.aurinow.aurinow W/System.err:     at java.net.URL.openStream(URL.java:470)
01-16 02:19:20.133 21184-21242/ec.com.aurinow.aurinow W/System.err:     at ec.com.aurinow.aurinow.TwitchSearchActivity$RetrieveResponseTask.sendRequest(TwitchSearchActivity.java:272)
01-16 02:19:20.133 21184-21242/ec.com.aurinow.aurinow W/System.err:     at ec.com.aurinow.aurinow.TwitchSearchActivity$RetrieveResponseTask.doInBackground(TwitchSearchActivity.java:233)
01-16 02:19:20.133 21184-21242/ec.com.aurinow.aurinow W/System.err:     at ec.com.aurinow.aurinow.TwitchSearchActivity$RetrieveResponseTask.doInBackground(TwitchSearchActivity.java:229)
01-16 02:19:20.133 21184-21242/ec.com.aurinow.aurinow W/System.err:     at android.os.AsyncTask.call(AsyncTask.java:295)
01-16 02:19:20.134 21184-21242/ec.com.aurinow.aurinow W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-16 02:19:20.134 21184-21242/ec.com.aurinow.aurinow W/System.err:     at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:234)
01-16 02:19:20.134 21184-21242/ec.com.aurinow.aurinow W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
01-16 02:19:20.134 21184-21242/ec.com.aurinow.aurinow W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
01-16 02:19:20.134 21184-21242/ec.com.aurinow.aurinow W/System.err:     at java.lang.Thread.run(Thread.java:818)

谢谢大家

编辑:所以我使用 urlConnection.getInputStream() 而不是 url.openStream() 使其工作。我不明白为什么在这种特定情况下它会那样工作。

您应该从 HttpURLConnection 对象获得响应。那就是回应的地方。

URL 对象如文档所述:

Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web.

因此 URL 仅指向您希望从中检索的资源。 HttpURLConnection 对象实际包含指向资源后的响应。

HttpURLConnection对象:

Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server...

所以改为这样做: (new InputStreamReader(urlConnection.getInputStream()));

HttpURLConnection urlConnection = null;
URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Client-ID", Configuration.TWITCH_CLIENT_ID);
urlConnection.setRequestMethod("GET");
urlConnection.connect();

System.out.println("ResponseCode: " + urlConnection.getResponseCode());

BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder sb = new StringBuilder();

String line;
while ((line = br.readLine()) != null) {
    sb.append(line + "\n");
}
br.close();

String jsonString = sb.toString();
System.out.println("JSON: " + jsonString);

return new JSONObject(jsonString);