响应代码为200的HttpURLConnection FileNotFoundException

时间:2018-01-16 23:10:29

标签: android httpurlconnection twitch

我正在尝试使用来自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$2.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$1.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()使其有效。我不知道为什么在这种特殊情况下它会像那样工作。

1 个答案:

答案 0 :(得分:1)

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

URL对象正如文档所述:

  

类URL表示统一资源定位符,指向a的指针   "资源"在万维网上。

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

HttpURLConnection对象:

  

每个HttpURLConnection实例用于发出单个请求   与HTTP服务器的基础网络连接...

所以这样做: (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);