Extended IP info

This commit is contained in:
M66B 2020-06-23 19:42:48 +02:00
parent aeb0a7acee
commit 3e4fa62284
1 changed files with 12 additions and 5 deletions

View File

@ -25,6 +25,9 @@ import android.net.ParseException;
import android.net.Uri;
import android.util.Pair;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
@ -67,19 +70,22 @@ public class IPInfo {
}
// https://ipinfo.io/developers
URL url = new URL("https://ipinfo.io/" + address.getHostAddress() + "/org");
URL url = new URL("https://ipinfo.io/" + address.getHostAddress());
Log.i("GET " + url);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
connection.setReadTimeout(FETCH_TIMEOUT);
connection.connect();
Organization organization = new Organization();
try {
String response = Helper.readStream(connection.getInputStream(), StandardCharsets.UTF_8.name());
organization.name = response.trim();
if ("".equals(organization.name) || "undefined".equals(organization.name))
organization.name = null;
String json = Helper.readStream(connection.getInputStream(), StandardCharsets.UTF_8.name());
JSONObject jinfo = new JSONObject(json);
organization.name = jinfo.optString("org");
organization.country = jinfo.optString("country");
} catch (JSONException ex) {
Log.w(ex);
} finally {
connection.disconnect();
}
@ -93,5 +99,6 @@ public class IPInfo {
static class Organization {
String name;
String country;
}
}