Update 'src/Location.java'
This commit is contained in:
parent
a26e9a9a9f
commit
59c0b1d3f9
@ -1,103 +1,106 @@
|
|||||||
|
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.net.ProtocolException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.ProtocolException;
|
||||||
import java.io.BufferedReader;
|
import java.net.URL;
|
||||||
import java.io.IOException;
|
import java.io.BufferedReader;
|
||||||
import java.io.InputStreamReader;
|
import java.io.IOException;
|
||||||
import java.net.HttpURLConnection;
|
import java.io.InputStreamReader;
|
||||||
import org.json.simple.parser.JSONParser;
|
import java.net.HttpURLConnection;
|
||||||
import org.json.simple.parser.ParseException;
|
import org.json.simple.parser.JSONParser;
|
||||||
import org.json.simple.JSONArray;
|
import org.json.simple.parser.ParseException;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONArray;
|
||||||
|
import org.json.simple.JSONObject;
|
||||||
public class Location {
|
|
||||||
private double latitude;
|
public class Location {
|
||||||
private double longitude;
|
private double latitude;
|
||||||
private String city;
|
private double longitude;
|
||||||
|
private String city;
|
||||||
public Location(double lat, double lon) {
|
|
||||||
latitude = lat;
|
public Location(double lat, double lon) {
|
||||||
longitude = lon;
|
latitude = lat;
|
||||||
//city = cityBasedOnIP(); //API intansive
|
longitude = lon;
|
||||||
}
|
city = "Unknown";
|
||||||
|
//city = cityBasedOnIP(); //API intansive
|
||||||
public Location() {
|
}
|
||||||
city = cityBasedOnIP();
|
|
||||||
}
|
public Location() {
|
||||||
|
//city = cityBasedOnIP();
|
||||||
public double getLatitude() {
|
city = "Unknown";
|
||||||
return latitude;
|
}
|
||||||
}
|
|
||||||
|
public double getLatitude() {
|
||||||
public String getCity() {
|
return latitude;
|
||||||
return city;
|
}
|
||||||
}
|
|
||||||
|
public String getCity() {
|
||||||
public void setLatitude(double latitude) {
|
return city;
|
||||||
this.latitude = latitude;
|
}
|
||||||
}
|
|
||||||
|
public void setLatitude(double latitude) {
|
||||||
public double getLongitude() {
|
this.latitude = latitude;
|
||||||
return longitude;
|
}
|
||||||
}
|
|
||||||
|
public double getLongitude() {
|
||||||
public void setLongitude(double longitude) {
|
return longitude;
|
||||||
this.longitude = longitude;
|
}
|
||||||
}
|
|
||||||
|
public void setLongitude(double longitude) {
|
||||||
public void setLocation(double lat, double lon) {
|
this.longitude = longitude;
|
||||||
this.latitude = lat;
|
}
|
||||||
this.longitude = lon;
|
|
||||||
}
|
public void setLocation(double lat, double lon) {
|
||||||
|
this.latitude = lat;
|
||||||
public String cityBasedOnIP() {
|
this.longitude = lon;
|
||||||
String city = "";
|
}
|
||||||
try {
|
|
||||||
|
public String cityBasedOnIP() {
|
||||||
URL url = new URL("http://ip-api.com/json");
|
String city = "";
|
||||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
try {
|
||||||
con.setRequestMethod("GET");
|
|
||||||
con.setRequestProperty("Content-Type", "application/json");
|
URL url = new URL("http://ip-api.com/json");
|
||||||
int status = con.getResponseCode();
|
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||||
BufferedReader in = new BufferedReader(
|
con.setRequestMethod("GET");
|
||||||
new InputStreamReader(con.getInputStream()));
|
con.setRequestProperty("Content-Type", "application/json");
|
||||||
String inputLine;
|
int status = con.getResponseCode();
|
||||||
StringBuffer content = new StringBuffer();
|
BufferedReader in = new BufferedReader(
|
||||||
while ((inputLine = in.readLine()) != null) {
|
new InputStreamReader(con.getInputStream()));
|
||||||
content.append(inputLine);
|
String inputLine;
|
||||||
}
|
StringBuffer content = new StringBuffer();
|
||||||
in.close();
|
while ((inputLine = in.readLine()) != null) {
|
||||||
|
content.append(inputLine);
|
||||||
JSONParser parser = new JSONParser();
|
}
|
||||||
Object resultObject = parser.parse(content.toString());
|
in.close();
|
||||||
|
|
||||||
if (resultObject instanceof JSONArray) {
|
JSONParser parser = new JSONParser();
|
||||||
JSONArray array=(JSONArray)resultObject;
|
Object resultObject = parser.parse(content.toString());
|
||||||
for (Object object : array) {
|
|
||||||
JSONObject obj =(JSONObject)object;
|
if (resultObject instanceof JSONArray) {
|
||||||
city = (String)obj.get("city");
|
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");
|
}
|
||||||
|
else if (resultObject instanceof JSONObject) {
|
||||||
}
|
JSONObject obj =(JSONObject)resultObject;
|
||||||
}
|
city = (String)obj.get("city");
|
||||||
catch (MalformedURLException e) {
|
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
}
|
||||||
catch (ProtocolException e) {
|
catch (MalformedURLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (ProtocolException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
catch (ParseException e) {
|
catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return city;
|
catch (ParseException e) {
|
||||||
}
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
return city;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user