Solved Trying to parse JSON from WebAPI data.

Discussion in 'Spigot Plugin Development' started by TWW, Aug 15, 2019.

  1. TWW

    TWW

    So I am developing plugin, and I am trying it to get a result from the website: https://ip.teoh.io/api/vpn/

    I am trying to get what text was given in the field "vpn_or_proxy" which will be yes/no

    And if it it yes, it executes some code, and if it is no, it executes some other code.


    Thanks.
     
  2. SteelPhoenix

    Junior Mod

    Convert the connection's input stream to a String using IOUtils for example and parse it using google's GSON
     
  3. TWW

    TWW

    im kinda new to java so i dont really know how to.
     
  4. Parsing with Google's GSON is fairly simple
    Create a new class and add fields with the same name as in the JSON
    Code (Java):
    public class ProxyInfo {
        String ip;
        String organization;
        String asn;
        String type;
        int is_hosting;
        String vpn_or_proxy;
    }
    And then you can deserialize the received string using something
    Code (Java):
    String data = "...";
    Gson gson = new Gson();
    ProxyInfo info = gson.fromJson(data, ProxyInfo.class);
    if (info.vpn_or_proxy.equals("yes")) {
        //do stuff
    } else {
        //do other stuff
    }
    Hope this helps.
     
  5. TWW

    TWW

    Thanks alot, but im struggling to fetch the data from the api, could you help? If not, thats fine.
     
  6. As SteelPhoenix suggested, use IOUtils included with Spigot
    Code (Java):
    try {
        URL url = new URL("https://ip.teoh.io/api/vpn/");
        BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
        String data = IOUtils.toString(in);
        //Use the Gson code to get what you are looking for
    } catch (IOException e) {
        e.printStackTrace();
    }
     
  7. TWW

    TWW

    Thanks so much!
     
  8. You wont need the IOUtils if you are using Java 8, you can use the Stream on the InputStream to join all the lines
     
  9. Hello Sir, you can simply make a request and then get the response. After that you can use google's GSON library (available here).

    Code:
    Code (Java):
    public boolean isVpnOrProxy() {
                URL website = new URL("https://ip.teoh.io/api/vpn");
                URLConnection connection = website.openConnection();
                connection.setRequestProperty("Content-Type", "application/json");
                connection.setRequestProperty("accept", "application/json");
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String inputLine;
                while((inputLine = in.readLine()) != null) response.append(inputLine);
                in.close();
                JsonParser parser = new JsonParser(response);
                return parser.getAsJsonObject().get("vpn_or_proxy").getAsBoolean();
    }
     
  10. SteelPhoenix

    Junior Mod

    He should still use it. The method is available in the server jar so use it. It's way easier than writing your own code to do the exact same thing.
    I mean you can also write your own json parser but I'd stick to gson...
     
  11. AS happen with Apache Commonds that could change, and also is not that hard just do
    Code (Java):
                URL url = new URL(surl);
                URLConnection con = url.openConnection();

                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String jsonString = in.lines().collect(Collectors.joining());
                in.close();
     
    #11 MrMaurice211, Aug 15, 2019
    Last edited: Aug 15, 2019
  12. Or also you can do like
    Code (Java):
    URL url = new URL(surl);
                URLConnection con = url.openConnection();

                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                JsonElement je = new Gson().fromJson(in, Type.class);
    in.close();
     
  13. TWW

    TWW

    Thanks all of you! I much appreaciate your help! I'll be closing this thread now.