Twitch API with HttpURLConnection not working?

sa
- in Twitch
2

Moin everyone,

I would like to access the Twitch API and query whether a certain channel is currently streaming.

Here is the code:

String Streamer = "Test";

Url url;
HttpURLConnection connection = null;

try {
// create connection
url = new URL (("https://api.twitch.tv/kraken/channels/" + Streamer));
connection = (HttpURLConnection) url.openConnection ();
connection.setRequestMethod ("GET");
connection.setRequestProperty ("Client-ID",
Secrets.twitch_clientid);

connection.setUseCaches (false);
connection.setDoInput (true);
connection.setDoOutput (true);

// Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());

// Get response
InputStream is = connection.getInputStream ();
BufferedReader rd = new BufferedReader (new InputStreamReader (is));
String line;
StringBuffer response = new StringBuffer ();
while ((line = rd.readLine ())! = null) {
response.append (line);
response.append ('\ r');
}
rd.close ();
System.out.println (response.toString ());
} catch (exception e) {
e.printStackTrace ();
System.out.println ("NULL");
} finally {
if (connection! = null) {
connection.disconnect ();
}
}

What I get as output:

java.io.FileNotFoundException: https://api.twitch.tv/kraken/channels/Test
at java.base / sun.net.http://www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1923)
at java.base / sun.net.http://www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1523)
at java.base / sun.net.http://www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224)
at me.duncte123.ge.commands.cmdTest.handle (cmdTest.java:47)
at me.duncte123.ge.CommandManager.handleCommand (CommandManager.java:59)
at me.duncte123.ge.Listener.onGuildMessageReceived (Listener.java:60)
at net.dv8tion.jda.core.hooks.ListenerAdapter.onEvent (ListenerAdapter.java:381)
at net.dv8tion.jda.core.hooks.InterfacedEventManager.handle (InterfacedEventManager.java:84)
at net.dv8tion.jda.core.handle.MessageCreateHandler.handleInternally (MessageCreateHandler.java:95)
at net.dv8tion.jda.core.handle.SocketHandler.handle (SocketHandler.java:37)
at net.dv8tion.jda.core.requests.WebSocketClient.onDispatch (WebSocketClient.java:868)
at

Has anyone ever had the same problem? Am I doing something wrong?

ma

Due to the lack of a client ID, I can't test it. When I try it in the browser, I get error 400. If I enter a random client ID (4711), the error "Invalid client id specified" appears. As far as can be expected. I don't know whether the error will be mapped to FileNotFoundException in Java. See if the exception has an InnerException that reveals more - or what it's called in Java… https://stackoverflow.com/...pt-in-java says something about it.

sa

Now have e.getCause (); done there's nothing as output… Here is the client ID: nzljca10enva1syj8o2vyojmp636vu
(Can change it again later.)

Decompile failed? ch chauffeurpower
Got Twitch API OAuth Token? Aw Awesomepakistan
PS4 strange news? uk ukrainianBear