Fetctching API response from http://api.ipinfodb.com and converting them to Java Objects. Similiar strategy can be used for other APIs, also includes the implementation of POJO classes (Getters & Setters method)
JAR Files Used
- fastjson-1.2.73
Fetctching API response from http://api.ipinfodb.com and converting them to Java Objects. Similiar strategy can be used for other APIs, also includes the implementation of POJO classes (Getters & Setters method)
JAR Files Used
package com.company; | |
// | |
// Source code recreated from a .class file by IntelliJ IDEA | |
// (powered by FernFlower decompiler) | |
// | |
import com.alibaba.fastjson.JSON; | |
import com.fasterxml.jackson.core.JsonGenerationException; | |
import com.fasterxml.jackson.databind.JsonMappingException; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
public class APIFunctions { | |
public static String ip = "94.200.55.38"; | |
public static String key = "9ceb467edd24cf7985f97e1e931af3eb9aca5cf63143ef09e442c188e0b7082f"; | |
public static String url; | |
static { | |
url = "http://api.ipinfodb.com/v3/ip-city/?key=" + key + "&ip=" + ip + "&format=json"; | |
} | |
public APIFunctions() { | |
} | |
public static void MyGETRequest() throws IOException { | |
URL urlForGetRequest = new URL(url); | |
String readLine = null; | |
HttpURLConnection conection = (HttpURLConnection)urlForGetRequest.openConnection(); | |
conection.setRequestMethod("GET"); | |
int responseCode = conection.getResponseCode(); | |
if (responseCode == 200) { | |
BufferedReader in = new BufferedReader(new InputStreamReader(conection.getInputStream())); | |
StringBuffer response = new StringBuffer(); | |
while((readLine = in.readLine()) != null) { | |
response.append(readLine); | |
} | |
in.close(); | |
System.out.println("API Response " + response.toString()); | |
} else { | |
System.out.println("GET NOT WORKED"); | |
} | |
} | |
public static void Convert_JSON() throws JsonGenerationException, JsonMappingException, IOException { | |
URL obj = new URL(url); | |
HttpURLConnection con = (HttpURLConnection)obj.openConnection(); | |
con.setRequestMethod("GET"); | |
con.setRequestProperty("User-Agent", "Mozilla/5.0"); | |
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
StringBuffer response = new StringBuffer(); | |
String inputLine; | |
while((inputLine = in.readLine()) != null) { | |
response.append(inputLine); | |
} | |
in.close(); | |
System.out.println(response.toString()); | |
new jsonPojo(); | |
jsonPojo instanceOfPojo = (jsonPojo)JSON.parseObject(response.toString(), jsonPojo.class); | |
System.out.println("-------Afer converting to java object-------------------"); | |
System.out.println("statusCode --" + instanceOfPojo.getStatusCode()); | |
System.out.println("ipAddress --" + instanceOfPojo.getIpAddress()); | |
System.out.println("countryCode --" + instanceOfPojo.getCountryCode()); | |
System.out.println("countryName --" + instanceOfPojo.getCountryName()); | |
System.out.println("regionName --" + instanceOfPojo.getRegionName()); | |
System.out.println("cityName --" + instanceOfPojo.getCityName()); | |
System.out.println("zipCode --" + instanceOfPojo.getZipCode()); | |
System.out.println("latitude --" + instanceOfPojo.getLatitude()); | |
System.out.println("longitude --" + instanceOfPojo.getLongitude()); | |
} | |
} |
package com.company; | |
// | |
// Source code recreated from a .class file by IntelliJ IDEA | |
// (powered by FernFlower decompiler) | |
// | |
public class jsonPojo { | |
private String statusMessage; | |
private String ipAddress; | |
private String countryCode; | |
private String countryName; | |
private String regionName; | |
private String cityName; | |
private String zipCode; | |
private String longitude; | |
private String latitude; | |
private String timezone; | |
private String statusCode; | |
public jsonPojo() { | |
} | |
public String getStatusCode() { | |
return this.statusCode; | |
} | |
public void setStatusCode(String statusCode) { | |
this.statusCode = statusCode; | |
} | |
public String getStatusMessage() { | |
return this.statusMessage; | |
} | |
public void setStatusMessage(String statusMessage) { | |
this.statusMessage = statusMessage; | |
} | |
public String getIpAddress() { | |
return this.ipAddress; | |
} | |
public void setIpAddress(String ipAddress) { | |
this.ipAddress = ipAddress; | |
} | |
public String getCountryCode() { | |
return this.countryCode; | |
} | |
public void setCountryCode(String countryCode) { | |
this.countryCode = countryCode; | |
} | |
public String getCountryName() { | |
return this.countryName; | |
} | |
public void setCountryName(String countryName) { | |
this.countryName = countryName; | |
} | |
public String getRegionName() { | |
return this.regionName; | |
} | |
public void setRegionName(String regionName) { | |
this.regionName = regionName; | |
} | |
public String getCityName() { | |
return this.cityName; | |
} | |
public void setCityName(String cityName) { | |
this.cityName = cityName; | |
} | |
public String getZipCode() { | |
return this.zipCode; | |
} | |
public void setZipCode(String zipCode) { | |
this.zipCode = zipCode; | |
} | |
public String getLongitude() { | |
return this.longitude; | |
} | |
public void setLongitude(String longitude) { | |
this.longitude = longitude; | |
} | |
public String getLatitude() { | |
return this.latitude; | |
} | |
public void setLatitude(String latitude) { | |
this.latitude = latitude; | |
} | |
public String getTimezone() { | |
return this.timezone; | |
} | |
public void setTimezone(String timezone) { | |
this.timezone = timezone; | |
} | |
} | |
package com.company; | |
public class Main { | |
public static void main(String[] args) { | |
// write your code here | |
try { | |
new APIFunctions(); | |
APIFunctions.Convert_JSON(); | |
} catch (Exception var2) { | |
System.out.println(var2); | |
} | |
} | |
} |
"C:\Program Files\Java\jdk1.8.0_261\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2\lib\idea_rt.jar=49645:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_261\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_261\jre\lib\rt.jar;D:\Work\TPTechnologies\Working Projects\out\production\Working Projects;D:\Work\TPTechnologies\Java Patterns\Airline Shipping -20200801T163431Z-001\Airline Shipping\JAR Files\ALL\fastjson-1.2.73.jar;D:\Work\TPTechnologies\Java Patterns\Airline Shipping -20200801T163431Z-001\Airline Shipping\JAR Files\ALL\javax.xml-1.3.4.jar;D:\Work\TPTechnologies\Java Patterns\Airline Shipping -20200801T163431Z-001\Airline Shipping\JAR Files\ALL\com.fasterxml.jackson.core.jar;D:\Work\TPTechnologies\Java Patterns\Airline Shipping -20200801T163431Z-001\Airline Shipping\JAR Files\ALL\com.fasterxml.jackson.databind.jar" com.company.Main | |
{ "statusCode" : "OK", "statusMessage" : "", "ipAddress" : "xx", "countryCode" : "AE", "countryName" : "United Arab Emirates", "regionName" : "Dubayy", "cityName" : "Dubai", "zipCode" : "-", "latitude" : "xxx", "longitude" : "xx", "timeZone" : "+04:00"} | |
-------Afer converting to java object------------------- | |
statusCode --OK | |
ipAddress --xxxxxxx | |
countryCode --AE | |
countryName --United Arab Emirates | |
regionName --Dubayy | |
cityName --Dubai | |
zipCode --- | |
latitude --xxxxxx | |
longitude --xxxxxx |