How to read Jsno from URL

Discussion in 'Plugin Development' started by MCCoding, Aug 18, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    MCCoding

    I'm using this method I found on stack trace, but when I run it it gives me the error "Server requested too many times (20)" anyone know of a better method I could use or a fix to this annoying bug?

    Code:java
    1. public static String readJsonFromUrl(String urlString) throws Exception {
    2. BufferedReader reader = null;
    3. try {
    4. URL url = new URL(urlString);
    5. reader = new BufferedReader(new InputStreamReader(url.openStream()));
    6. StringBuffer buffer = new StringBuffer();
    7. int read;
    8. char[] chars = new char[1024];
    9. while ((read = reader.read(chars)) != -1) {
    10. buffer.append(chars, 0, read);
    11. }
    12.  
    13. return buffer.toString();
    14. } finally {
    15. if (reader != null) {
    16. reader.close();
    17. }
    18. }
    19. }
     
  2. Offline

    xJeremyCx

    This is Mojang's problem, not yours. It limits requests per minute.
     
  3. Offline

    MCCoding

    xJeremyCx
    Is there any way to bypass this?
     
  4. Offline

    xTrollxDudex

    Use a StringBuilder. StringBuffer is synchronized, although the JIT almost certainly will catch this (assuming it is Java 7 (not sure about that) or the IBM JVM implementation).

    Now to the actual question - how many times do you call this method, and at what rate?
     
  5. Offline

    MCCoding

    xTrollxDudex
    I'm calling this when my plugin gets enabled, it gets the Twitch Channel names from the config and then gets the json from the url. When i was testing this it was only getting called once, but bby setting it to use a stringBuilder fixed the previous error but now I'm getting this "com.google.gson.stream.malformedjsonexception use jsonreader.setlenient(true)"

    Here is the method I'm using and here is the line the error is occurring on:

    Code:java
    1. JsonArray jb = gson.fromJson(json, JsonArray.class);



    Code:java
    1. public static TwitchStream getStream(String channelname) {
    2. try {
    3. String json = API.readJsonFromUrl("[url]http://api.justin.tv/api/stream/list.json?jsonp=&channel=[/url]" + channelname);
    4.  
    5. TwitchStream stream = new TwitchStream();
    6. if (json.equalsIgnoreCase("[]")) {
    7. stream.setOnline(false);
    8. return stream;
    9. }
    10.  
    11. Gson gson = new Gson();
    12. JsonArray jb = gson.fromJson(json, JsonArray.class);
    13. JsonObject jo = (JsonObject) jb.get(0);
    14. stream.setOnline(true);
    15. stream.load(jo);
    16. return stream;
    17. } catch (Exception error) {
    18. error.printStackTrace();
    19. }
    20.  
    21. return null;
    22. }
     
  6. Offline

    xTrollxDudex

    It means the JSON string has syntax errors in it. Using setLentient(true) helps avoid some of those errors from cropping up.
     
  7. Offline

    MCCoding

    xTrollxDudex
    I managed to fix it all now all I need to do is load all the data into my object I have created, here is the Json I got from the URL I'm just unsure on how I would gather all of the vales.

    Code:
    {
      "_links": {
          "self": "https://api.twitch.tv/kraken/streams/siylisstv",
          "channel": "https://api.twitch.tv/kraken/channels/siylisstv"
      },
      "stream": {
          "_id": 10732803952,
          "game": "Minecraft",
          "viewers": 164,
          "_links": {
            "self": "https://api.twitch.tv/kraken/streams/siylisstv"
          },
          "preview": {
            "small": "http://static-cdn.jtvnw.net/previews-ttv/live_user_siylisstv-80x50.jpg",
            "medium": "http://static-cdn.jtvnw.net/previews-ttv/live_user_siylisstv-320x200.jpg",
            "large": "http://static-cdn.jtvnw.net/previews-ttv/live_user_siylisstv-640x400.jpg",
            "template": "http://static-cdn.jtvnw.net/previews-ttv/live_user_siylisstv-{width}x{height}.jpg"
          },
          "channel": {
            "_links": {
                "self": "http://api.twitch.tv/kraken/channels/siylisstv",
                "follows": "http://api.twitch.tv/kraken/channels/siylisstv/follows",
                "commercial": "http://api.twitch.tv/kraken/channels/siylisstv/commercial",
                "stream_key": "http://api.twitch.tv/kraken/channels/siylisstv/stream_key",
                "chat": "http://api.twitch.tv/kraken/chat/siylisstv",
                "features": "http://api.twitch.tv/kraken/channels/siylisstv/features",
                "subscriptions": "http://api.twitch.tv/kraken/channels/siylisstv/subscriptions",
                "editors": "http://api.twitch.tv/kraken/channels/siylisstv/editors",
                "videos": "http://api.twitch.tv/kraken/channels/siylisstv/videos",
                "teams": "http://api.twitch.tv/kraken/channels/siylisstv/teams"
            },
            "background": null,
            "banner": null,
            "display_name": "SiylissTV",
            "game": "Minecraft",
            "logo": "http://static-cdn.jtvnw.net/jtv_user_pictures/siylisstv-profile_image-670ee76b74e32cac-300x300.png",
            "mature": false,
            "status": "[ Minecraft Artwork! ] Check out siyliss.com",
            "url": "http://www.twitch.tv/siylisstv",
            "video_banner": "http://static-cdn.jtvnw.net/jtv_user_pictures/siylisstv-channel_offline_image-1752d850ab2d2968-640x360.png",
            "_id": 49700822,
            "name": "siylisstv",
            "created_at": "2013-10-02T14:18:47Z",
            "updated_at": "2014-08-19T02:50:41Z",
            "abuse_reported": null,
            "delay": 0,
            "followers": 24607,
            "profile_banner": "http://static-cdn.jtvnw.net/jtv_user_pictures/siylisstv-profile_banner-058d28715ed691d0-480.png",
            "profile_banner_background_color": "#000000",
            "views": 371070,
            "language": "en"
          }
      }
    }
     
  8. Offline

    xTrollxDudex

  9. Offline

    MCCoding

    xTrollxDudex

    Ah okay I had a look through it all but how would I get all the Json elements from the JsonObject?
     
  10. Offline

    xTrollxDudex

  11. Offline

    MCCoding

    xTrollxDudex

    Ahh okay here is what I have but it won't print out the broadcasters name. I'm getting MalformedJsonException: Undetermined array at line 1 column 9

    here is my method that used the JsonStreamParser in

    Code:java
    1. public void loadStream(JsonObject job) {
    2. JsonStreamParser paraser = new JsonStreamParser(job.entrySet().toString());
    3. while (paraser.hasNext()) {
    4. System.out.println("HAS NEXT");
    5. if (paraser.next().equals("display_name")) {
    6. System.out.println("display_name == " + paraser.next()); //Creates the exception
    7. }
    8. }
     
  12. Offline

    xTrollxDudex

    You iterate through the job's entrySet, like a HashMap entrySet. Don't wrap with a stream parser.
     
  13. Offline

    xJeremyCx

    Oh you're getting JSON strings from Twitch? I don't know about this. What I know is Mojang limits request(Name -> UUID) per minute. You can try to put a some valid heads in your inventory then place them quickly(and look at your server console).
     
Thread Status:
Not open for further replies.

Share This Page