最近在做项目的时候遇到一个功能,就是需要自动定位当前所在城市,然后去获取当年城市的天气,我负责做自动定位城市的功能,定位大家都知道有很多种,常用的就是大家经常用到的基站啦、GPS啦等等,一开始打算使用Google的定位服务,但是使用这个需要加一个lib包,有几百K,领导说太大了不让用,然后考虑到使用基站,找了好久现在已经没有什么靠谱的服务器,然后有一个minigps的东西,还特么是要收费的,然后又没使用,接着就找利用IP地址来定位城市,确定好了方案,接着就是找方法实现啦。
1、首先最大的问题就是要找一个可以提供“上传IP返回地址”的可靠服务器
找这个就是自己想点关键字各种Google,本人英语比较垃圾,找了好久才找到,具体的地址是http://ip-api.com/docs/api:json,直接能返回json格式,好像还有其他格式的,具体木有太看。
2、然后就是要看怎么去获取本机上网的IP地址
这个IP并不是大家通常在手机里面看到的那个地址,能看到的那个IP应该是局域网的IP,我也不是很懂,上网的IP是广域网IP吧?反正这 个IP是无法通过本机获取的(我也是问的大神才知道,网上查到的也都是获取本机的IP地址),必须要通过访问网络时服务器才能知道你的IP,比如大家在百度里直接搜“IP”能看到的那个
http://blog.csdn.net/h7870181/article/details/8480452),然后就是抄的这里面的获取上网IP的那段,具体大家自己看,待会会贴代码出来。 所以又得找一个靠谱的查询IP地址的服务器,然后大神发给我一个帖子()
3、然后有了IP地址有了查询城市的服务器,就OK啦。
下面贴代码,首先是获取上网IP:
- public class IpUtil {
- public static String getIpUrl = “http://www.cz88.net/ip/viewip778.aspx”;
- public static void getWebIp() {
- new Thread() {
- public void run() {
- String strForeignIP = ”“;
- try {
- URL url = new URL(getIpUrl);
- BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
- String s = ”“;
- StringBuffer sb = new StringBuffer(“”);
- while ((s = br.readLine()) != null) {
- sb.append(s + ”\r\n”);
- }
- br.close();
- String webContent = ”“;
- webContent = sb.toString();
- String flagofForeignIPString = ”IPMessage”;
- int startIP = webContent.indexOf(flagofForeignIPString)
- + flagofForeignIPString.length() + 2;
- int endIP = webContent.indexOf(“</span>”, startIP);
- strForeignIP = webContent.substring(startIP, endIP);
- } catch (Exception e) {
- e.printStackTrace();
- }
- };
- }.start();
- }
- }
public class IpUtil {
public static String getIpUrl = "http://www.cz88.net/ip/viewip778.aspx";
public static void getWebIp() {
new Thread() {
public void run() {
String strForeignIP = "";
try {
URL url = new URL(getIpUrl);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
String s = "";
StringBuffer sb = new StringBuffer("");
while ((s = br.readLine()) != null) {
sb.append(s + "\r\n");
}
br.close();
String webContent = "";
webContent = sb.toString();
String flagofForeignIPString = "IPMessage";
int startIP = webContent.indexOf(flagofForeignIPString)
+ flagofForeignIPString.length() + 2;
int endIP = webContent.indexOf("</span>", startIP);
strForeignIP = webContent.substring(startIP, endIP);
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
}
}
然后就是获取城市:
- public static final String sGetAddrUrl = “http://ip-api.com/json/”;
- public static void locateCityName(final String foreignIPString) {
- new Thread() {
- public void run() {
- try {
- HttpClient httpClient = new DefaultHttpClient();
- String requestStr = sGetAddrUrl + foreignIPString;
- HttpGet request = new HttpGet(requestStr);
- HttpResponse response = httpClient.execute(request);
- if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
- String cityName = EntityUtils.toString(response.getEntity());
- }
- } catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- };
- }.start();
- }
public static final String sGetAddrUrl = "http://ip-api.com/json/";
public static void locateCityName(final String foreignIPString) {
new Thread() {
public void run() {
try {
HttpClient httpClient = new DefaultHttpClient();
String requestStr = sGetAddrUrl + foreignIPString;
HttpGet request = new HttpGet(requestStr);
HttpResponse response = httpClient.execute(request);
if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
String cityName = EntityUtils.toString(response.getEntity());
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
}
差不多就是这样了,第一次写博客,哈哈,大家多担待,有问题请大家多多指出哦!