Getting an error when making a api call [Solved]

Hello,

I am making a call to get the json here: http://tmi.twitch.tv/group/user/m0e_tv/chatters but i get an error. This is my Java code:

try {
    URL url = new URL("http://tmi.twitch.tv/group/user/m0e_tv/chatters");
    URLConnection conn = url.openConnection();
    BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() ));
    String inputLine = br.readLine();
    br.close();
    JsonObject jsonObj = JsonObject.readFrom(inputLine); //AT THIS POINT I GET ERROR
    ....
	}  catch (IOException e) 
    {
        e.printStackTrace();
    } 

Anything wrong with that API or with my coding :stuck_out_tongue: plz help

edit: Oh this is the error I get: Exception in thread "main" com.eclipsesource.json.ParseException: Unexpected end of input at 1:0

edit2: I believe there is nothing wrong with my code because I am making a call to https://api.twitch.tv/kraken/streams/m0e_tv at another place and that works like a charm. That’s why I don’t think its a programming error. And I also checked online that that link returns a JSON. I checked it at http://jsonlint.com/. So I don’t know what could be wrong

1 Like

Don’t read only a single line, read all of the response. The response may have multiple lines, like in this case, it does:

$ curl http://tmi.twitch.tv/group/user/notventic/chatters
{
  "_links": {},
  "chatter_count": 0,
  "chatters": {
    "moderators": [],
    "staff": [],
    "admins": [],
    "global_mods": [],
    "viewers": []
  }
}
1 Like

then why making a call to https://api.twitch.tv/kraken/streams/m0e_tv works? Does that return a single line? I am sorry I am new to parsing JSON and stuff

Yes, the public supported APIs return minified json.

1 Like

I will post a reply when I change my code and will tell u the results

that is so insane. I am using bufferedreader and that class doesn’t have a method that reads the whole thing. And it doesn’t even have a method that checks if it has more lines (something like hasNext(…) …) so I can run a loop and store all of it. What should I do? Plz help!

nvm i got it i need to use while ((line = reader.readLine()) != null)

Ty MR 3ventic for the help. This is what I did:
try { URL url = new URL("https://tmi.twitch.tv/group/user/m0e_tv/chatters"); URLConnection conn = url.openConnection(); BufferedReader br = new BufferedReader( new InputStreamReader( conn.getInputStream() )); String line; String all = ""; while ((line = br.readLine()) != null) { all = all + line; } br.close(); JsonObject jsonObj = JsonObject.readFrom(all); //works good now System.err.println(jsonObj.get("chatters")); } catch (IOException e) { e.printStackTrace(); }

I can get the chatters property fine. But I want viewers property inside that chatters property. How do I get that? I tried jsonObj.get("chatters.viewers") and jsonObj.get("chatters[viewers]") How do I access that property please help Thank you

3ventic too good.

very very very easy. I know what to do. This is what I did:

JsonObject jsonObj = JsonObject.readFrom(all); JsonObject jsonObj2 = (JsonObject) jsonObj.get("chatters"); System.err.println(jsonObj2.get("viewers")); //This prints out all the viewers //and only the viewers and not //garbage like mods etc...

Thank you very much for the help Mr. 3ventic

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.