From 92e683b3b356d5126812308011b7fbf39a409d6f Mon Sep 17 00:00:00 2001 From: Jonttu Date: Tue, 3 Mar 2020 00:31:24 +0200 Subject: [PATCH] Project files --- IPResolver.java | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 27 ++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 IPResolver.java create mode 100644 README.md diff --git a/IPResolver.java b/IPResolver.java new file mode 100644 index 0000000..8c31b5a --- /dev/null +++ b/IPResolver.java @@ -0,0 +1,56 @@ +package fi.jonttu.api; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.nio.charset.Charset; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +public class IPResolver { + + private static final String token = "TOKEN"; + private static final String url = "https://api.jonttu.fi/ip.php?token=%token%&ip=%ip%"; + + private String status = "no data"; + private String ip = "?"; + private String country = "?"; + private String city = "?"; + private String isp = "?"; + private boolean hosting = false; + + public boolean getStatus() { return status.equals("success"); } + public String getStatusMsg() { return status; } + public String getIP() { return ip; } + public String getCountry() { return country; } + public String getCity() { return city; } + public String getIsp() { return isp; } + public boolean isHosting() { return hosting; } + + public IPResolver(String ip) { + try { + InputStream is = new URL(url.replace("%token%", token).replace("%ip%", ip)).openStream(); + BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); + StringBuilder sb = new StringBuilder(); + int cp; + while ((cp = rd.read()) != -1) { + sb.append((char) cp); + } + JsonObject json = new JsonParser().parse(sb.toString()).getAsJsonObject(); + status = json.get("status").getAsString(); + if (status.equals("success")) { + this.ip = json.get("address").getAsString(); + this.country = json.get("country").getAsString(); + this.city = json.get("city").getAsString(); + this.isp = json.get("isp").getAsString(); + this.hosting = json.get("hosting").getAsBoolean(); + } + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..d5758fd --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# Java-IP-API +Simple java class example for https://api.jonttu.fi/ip.php API + +When using this API you will need token and token has to be placed in IPResolver class to token variable! + +Example: +```java +// Makes new instansce of IPResolver and gets data from API +// Running this calls web api and recommended to run asyncronoysly +IPResolver resolver = new IPResolver("127.0.0.1"); + +// If request succeeded returns true +boolean status = resolver.getStatus(); + +// Returns request status message "success" is output when everything is alright. +String statusMsg = resolver.getStatusMsg(); + +// General information about ip +String address = resolver.getIP(); +String country = resolver.getCountry(); +String city = resolver.getCity(); +String isp = resolver.getIsp(); + +// This is kind of special parameter... +// Parameter tells if ip address belongs to somekind of hosting company or is vpn or proxy service. +boolean hosting = resolver.isHosting(); +```