Java
Chilkat2 Java
api.db-ip.com IPv4 Geolocation Lookup
See more Geolocation Examples
Demonstrates how to lookup Geolocation data for an IPv4 address using the api.db-ip.com 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: 149.250.207.170 (this was a randomly chosen address)
String jsonStr;
try {
jsonStr = http.quickGetStr("http://api.db-ip.com/v2/free/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
// {
// "ipAddress": "149.250.207.170",
// "continentCode": "EU",
// "continentName": "Europe",
// "countryCode": "DE",
// "countryName": "Germany",
// "stateProv": "Baden-W\u00fcrttemberg",
// "city": "B\u00f6blingen"
// }
String ipAddress = json.stringOf("ipAddress");
String continentCode = json.stringOf("continentCode");
String continentName = json.stringOf("continentName");
String countryCode = json.stringOf("countryCode");
String countryName = json.stringOf("countryName");
String stateProv = json.stringOf("stateProv");
String city = json.stringOf("city");
}
}