Java
Chilkat2 Java
ip-api.com IPv4 Geolocation Lookup
See more Geolocation Examples
Demonstrates how to lookup Geolocation data for an IPv4 address using the ip-api.com API.Chilkat Java Downloads
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("http://ip-api.com/json/149.250.207.170");
} 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
// {
// "as": "AS15854 Hewlett Packard GmbH",
// "city": "B�blingen",
// "country": "Germany",
// "countryCode": "DE",
// "isp": "Triaton Frankfurt",
// "lat": 48.6779,
// "lon": 8.97297,
// "org": "EntServ Deutschland GmbH",
// "query": "149.250.207.170",
// "region": "BW",
// "regionName": "Baden-W�rttemberg",
// "status": "success",
// "timezone": "Europe/Berlin",
// "zip": "71034"
// }
String as = json.stringOf("as");
String city = json.stringOf("city");
String country = json.stringOf("country");
String countryCode = json.stringOf("countryCode");
String isp = json.stringOf("isp");
String lat = json.stringOf("lat");
String lon = json.stringOf("lon");
String org = json.stringOf("org");
String query = json.stringOf("query");
String region = json.stringOf("region");
String regionName = json.stringOf("regionName");
String status = json.stringOf("status");
String timezone = json.stringOf("timezone");
String zip = json.stringOf("zip");
}
}