【发布时间】:2017-05-19 02:32:02
【问题描述】:
谁知道是否存在任何 API 来获取使用公共 IP 的 android 设备的估计地理位置(纬度、经度)?
现在我可以通过以下方法获取 IP 地址:
private void getIPAddress(){
URL url;
BufferedReader bufferedReader=null;
InputStreamReader in=null;
HttpURLConnection urlConnection = null;
try {
url = new URL("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
in = new InputStreamReader(urlConnection.getInputStream());
bufferedReader = new BufferedReader(in);
String line;
StringBuffer buffer = new StringBuffer();
while((line = bufferedReader.readLine()) != null) {
buffer.append(line);
buffer.append('\r');
}
bufferedReader.close();
in.close();
JSONObject json_data = new JSONObject(buffer.toString());
ip = json_data.getString("ip");
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
bufferedReader.close();
in.close();
} catch (IOException e) {}
if(urlConnection != null) {
urlConnection.disconnect();
}
}
}
谢谢
【问题讨论】:
标签: android geolocation ip