From 3e4fa62284e2af89137f8493171bf13e0e3a3d59 Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 23 Jun 2020 19:42:48 +0200 Subject: [PATCH] Extended IP info --- app/src/main/java/eu/faircode/email/IPInfo.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/IPInfo.java b/app/src/main/java/eu/faircode/email/IPInfo.java index e446335769..5f247447bc 100644 --- a/app/src/main/java/eu/faircode/email/IPInfo.java +++ b/app/src/main/java/eu/faircode/email/IPInfo.java @@ -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; } }