I'm trying to retrieve some videos with a GameId from Helix API of Twitch. Using a REST client I can retrieve the data and it works, but when I try it on android it won't work. I get a response code 200, but when I try to access the InputStream I get a FileNotFoundException.
Here's the code I'm using:
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);
Where Configuration.TWITCH_CLIENT_ID
is my Twitch Client ID.
The output I get is:
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)
Thank You Guys
EDIT: So I make it work using urlConnection.getInputStream()
instead of url.openStream()
. I don't why in this specific case it works like that.
You should get the response from the HttpURLConnection
object. That is where the response will be.
The URL
object is as the documentation puts it:
Class URL represents a Uniform Resource Locator, a pointer to a "resource" on the World Wide Web.
So the URL
only points to the resource you wish to retrieve from. The HttpURLConnection
object is what actually contains the response after being pointed to the resource.
The HttpURLConnection
object:
Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server...
So do this instead:
(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);