Project files

This commit is contained in:
Jonttu 2020-03-03 00:31:24 +02:00
commit 92e683b3b3
2 changed files with 83 additions and 0 deletions

56
IPResolver.java Normal file
View File

@ -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();
}
}
}

27
README.md Normal file
View File

@ -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();
```