From 59c0b1d3f955969c3318f523b6b523d1d0502327 Mon Sep 17 00:00:00 2001 From: Hesham Date: Tue, 29 Jan 2019 18:41:32 +0300 Subject: [PATCH] Update 'src/Location.java' --- src/Location.java | 209 +++++++++++++++++++++++----------------------- 1 file changed, 106 insertions(+), 103 deletions(-) diff --git a/src/Location.java b/src/Location.java index e4518da..4da8d54 100644 --- a/src/Location.java +++ b/src/Location.java @@ -1,103 +1,106 @@ - -import java.net.MalformedURLException; -import java.net.ProtocolException; -import java.net.URL; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; - -public class Location { - private double latitude; - private double longitude; - private String city; - - public Location(double lat, double lon) { - latitude = lat; - longitude = lon; - //city = cityBasedOnIP(); //API intansive - } - - public Location() { - city = cityBasedOnIP(); - } - - public double getLatitude() { - return latitude; - } - - public String getCity() { - return city; - } - - public void setLatitude(double latitude) { - this.latitude = latitude; - } - - public double getLongitude() { - return longitude; - } - - public void setLongitude(double longitude) { - this.longitude = longitude; - } - - public void setLocation(double lat, double lon) { - this.latitude = lat; - this.longitude = lon; - } - - public String cityBasedOnIP() { - String city = ""; - try { - - URL url = new URL("http://ip-api.com/json"); - HttpURLConnection con = (HttpURLConnection) url.openConnection(); - con.setRequestMethod("GET"); - con.setRequestProperty("Content-Type", "application/json"); - int status = con.getResponseCode(); - BufferedReader in = new BufferedReader( - new InputStreamReader(con.getInputStream())); - String inputLine; - StringBuffer content = new StringBuffer(); - while ((inputLine = in.readLine()) != null) { - content.append(inputLine); - } - in.close(); - - JSONParser parser = new JSONParser(); - Object resultObject = parser.parse(content.toString()); - - if (resultObject instanceof JSONArray) { - JSONArray array=(JSONArray)resultObject; - for (Object object : array) { - JSONObject obj =(JSONObject)object; - city = (String)obj.get("city"); - } - - } - else if (resultObject instanceof JSONObject) { - JSONObject obj =(JSONObject)resultObject; - city = (String)obj.get("city"); - - } - } - catch (MalformedURLException e) { - e.printStackTrace(); - } - catch (ProtocolException e) { - e.printStackTrace(); - } - catch (IOException e) { - e.printStackTrace(); - } - catch (ParseException e) { - e.printStackTrace(); - } - return city; - } -} + + +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; + +public class Location { + private double latitude; + private double longitude; + private String city; + + public Location(double lat, double lon) { + latitude = lat; + longitude = lon; + city = "Unknown"; + //city = cityBasedOnIP(); //API intansive + } + + public Location() { + //city = cityBasedOnIP(); + city = "Unknown"; + } + + public double getLatitude() { + return latitude; + } + + public String getCity() { + return city; + } + + public void setLatitude(double latitude) { + this.latitude = latitude; + } + + public double getLongitude() { + return longitude; + } + + public void setLongitude(double longitude) { + this.longitude = longitude; + } + + public void setLocation(double lat, double lon) { + this.latitude = lat; + this.longitude = lon; + } + + public String cityBasedOnIP() { + String city = ""; + try { + + URL url = new URL("http://ip-api.com/json"); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("GET"); + con.setRequestProperty("Content-Type", "application/json"); + int status = con.getResponseCode(); + BufferedReader in = new BufferedReader( + new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuffer content = new StringBuffer(); + while ((inputLine = in.readLine()) != null) { + content.append(inputLine); + } + in.close(); + + JSONParser parser = new JSONParser(); + Object resultObject = parser.parse(content.toString()); + + if (resultObject instanceof JSONArray) { + JSONArray array=(JSONArray)resultObject; + for (Object object : array) { + JSONObject obj =(JSONObject)object; + city = (String)obj.get("city"); + } + + } + else if (resultObject instanceof JSONObject) { + JSONObject obj =(JSONObject)resultObject; + city = (String)obj.get("city"); + + } + } + catch (MalformedURLException e) { + e.printStackTrace(); + } + catch (ProtocolException e) { + e.printStackTrace(); + } + catch (IOException e) { + e.printStackTrace(); + } + catch (ParseException e) { + e.printStackTrace(); + } + return city; + } +}