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