Get information from web api (JSON)

Discussion in 'Plugin Development' started by pedrinholuizf, Sep 16, 2015.

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

    pedrinholuizf

    How can I get information from a web api like this one? http://mcping.net/api/jogar.stormcraft.com.br/online,max

    I'm using this code to get the text from the api.
    Code:
    StringBuilder content = new StringBuilder();
            try {
                URL url = new URL(name);
                URLConnection urlConnection = url.openConnection();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    content.append(line + "\n");
                }
                bufferedReader.close();
            } catch (Exception e) {
                return null;
            }
    
    And his code to get the value:
    Code:
            try {
                String lortu = content.toString();
                JSONParser parser = new JSONParser();
                JSONObject jo = (JSONObject) parser.parse(lortu);
                return (String) jo.get(zer);
            } catch (Exception e) {
                return null;
            }
    
    But it returns null. (I printed the 1st code response on the server console and it was ok)

    Can anyone help me? Tanks :)
     
  2. Offline

    teej107

    @pedrinholuizf Perhaps you shouldn't silence an exception like you do in your second code segment. That is why it is returning null. I also wouldn't add anything extra to the retrieved String like you are doing in your first code segment.
     
  3. Offline

    mythbusterma

    I'm going to translate this for you.

    Translates roughly to: If something goes wrong while I'm doing this, don't tell me about it, don't log an error, and don't provide any indication something went wrong.

    We have exceptions for a reason, don't throw them on the floor.
     
  4. Offline

    FabeGabeMC

    badum tsss
    What is the variable 'zer' supposed to be?
     
  5. Offline

    pedrinholuizf

    The value that i want. Like "online"

    I added exeptions, and the error looks to be in the 6th line of the second piece of code
     
  6. Offline

    teej107

    If that is true, then the Map doesn't have the value you are trying to get.
     
  7. Offline

    pedrinholuizf

    Is there any way to print that in the chat so i can see whats happening?
     
  8. Offline

    FabeGabeMC

  9. Offline

    DoggyCode™

    Pretty cool :p
    [​IMG]
     
Thread Status:
Not open for further replies.

Share This Page