Sample code for 30+ languages & platforms
Java

ipapi.co IPv4 Geolocation Lookup

See more Geolocation Examples

Demonstrates how to lookup Geolocation data for an IPv4 address using the ipapi.co REST API.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.JsonObject;

public class ChilkatExample {

    public static void main(String[] args) {
        // This example requires the Chilkat API to have been previously unlocked.
        // See Global Unlock Sample for sample code.

        Http http = new Http();

        // Lookup an IPv4 address: 149.250.207.170  (this was a randomly chosen address)
        String jsonStr;
        try {
            jsonStr = http.quickGetStr("https://ipapi.co/149.250.207.170/json");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        JsonObject json = new JsonObject();
        json.setEmitCompact(false);
        json.load(jsonStr);

        System.out.println(json.emit());

        // Sample output:
        // Use this online tool to generate parsing code from sample JSON:
        // Generate Parsing Code from JSON

        // {
        //   "ip": "149.250.207.170",
        //   "city": "B�blingen",
        //   "region": "Baden-W�rttemberg",
        //   "region_code": null,
        //   "country": "DE",
        //   "country_name": "Germany",
        //   "continent_code": "EU",
        //   "in_eu": true,
        //   "postal": null,
        //   "latitude": null,
        //   "longitude": null,
        //   "timezone": null,
        //   "utc_offset": null,
        //   "country_calling_code": "+49",
        //   "currency": "EUR",
        //   "languages": "de",
        //   "asn": "AS15854",
        //   "org": "EntServ Deutschland GmbH"
        // }

        String ip = json.stringOf("ip");
        String city = json.stringOf("city");
        String region = json.stringOf("region");
        String region_code = json.stringOf("region_code");
        String country = json.stringOf("country");
        String country_name = json.stringOf("country_name");
        String continent_code = json.stringOf("continent_code");
        boolean in_eu = json.boolOf("in_eu");
        String postal = json.stringOf("postal");
        String latitude = json.stringOf("latitude");
        String longitude = json.stringOf("longitude");
        String timezone = json.stringOf("timezone");
        String utc_offset = json.stringOf("utc_offset");
        String country_calling_code = json.stringOf("country_calling_code");
        String currency = json.stringOf("currency");
        String languages = json.stringOf("languages");
        String asn = json.stringOf("asn");
        String org = json.stringOf("org");
    }
}