Java
Chilkat2 Java
geo.ipify.org IPv4 Geolocation Lookup
See more Geolocation Examples
Demonstrates how to lookup Geolocation data for an IPv4 address using the geo.ipify.org REST 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: 8.8.8.8
String jsonStr;
try {
jsonStr = http.quickGetStr("https://geo.ipify.org/api/v1?apiKey=API_KEY&ipAddress=8.8.8.8");
} 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": "8.8.8.8",
// "location": {
// "country": "IT",
// "region": "Lombardy",
// "city": "Milan",
// "lat": 45.4707,
// "lng": 9.1889,
// "postalCode": "20147",
// "timezone": "+02:00"
// }
// }
String ip = json.stringOf("ip");
String locationCountry = json.stringOf("location.country");
String locationRegion = json.stringOf("location.region");
String locationCity = json.stringOf("location.city");
String locationLat = json.stringOf("location.lat");
String locationLng = json.stringOf("location.lng");
String locationPostalCode = json.stringOf("location.postalCode");
String locationTimezone = json.stringOf("location.timezone");
}
}