手机如何利用IP地址定位城市

本文介绍了一种通过IP地址获取所在城市的方法。首先介绍了如何通过访问特定网址获取公网IP地址,随后利用API服务将该IP转换为地理位置信息。文章提供了完整的Java代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载博客地址

最近在做项目的时候遇到一个功能,就是需要自动定位当前所在城市,然后去获取当年城市的天气,我负责做自动定位城市的功能,定位大家都知道有很多种,常用的就是大家经常用到的基站啦、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:

  1. public class IpUtil {  
  2.   
  3.     public static String getIpUrl = “http://www.cz88.net/ip/viewip778.aspx”;  
  4.   
  5.     public static void getWebIp() {  
  6.         new Thread() {  
  7.             public void run() {  
  8.                 String strForeignIP = ”“;  
  9.                 try {  
  10.                     URL url = new URL(getIpUrl);  
  11.   
  12.                     BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));  
  13.   
  14.                     String s = ”“;  
  15.                     StringBuffer sb = new StringBuffer(“”);  
  16.                     while ((s = br.readLine()) != null) {  
  17.                         sb.append(s + ”\r\n”);  
  18.                     }  
  19.                     br.close();  
  20.   
  21.                     String webContent = ”“;  
  22.                     webContent = sb.toString();  
  23.                     String flagofForeignIPString = ”IPMessage”;  
  24.                     int startIP = webContent.indexOf(flagofForeignIPString)  
  25.                             + flagofForeignIPString.length() + 2;  
  26.                     int endIP = webContent.indexOf(“</span>”, startIP);  
  27.                     strForeignIP = webContent.substring(startIP, endIP);  
  28.                 } catch (Exception e) {  
  29.                     e.printStackTrace();  
  30.                 }  
  31.             };  
  32.         }.start();  
  33.   
  34.     }  
  35. }  
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();

    }
}
然后就是获取城市:

  1. public static final String sGetAddrUrl = “http://ip-api.com/json/”;  
  2.   
  3.     public static void locateCityName(final String foreignIPString) {  
  4.         new Thread() {  
  5.             public void run() {  
  6.                 try {  
  7.                     HttpClient httpClient = new DefaultHttpClient();  
  8.                     String requestStr = sGetAddrUrl + foreignIPString;  
  9.                     HttpGet request = new HttpGet(requestStr);  
  10.                     HttpResponse response = httpClient.execute(request);  
  11.                     if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {  
  12.                        String cityName = EntityUtils.toString(response.getEntity());  
  13.                           
  14.                     }  
  15.                 } catch (ClientProtocolException e) {  
  16.                     e.printStackTrace();  
  17.                 } catch (IOException e) {  
  18.                     e.printStackTrace();  
  19.                 }  
  20.             };  
  21.         }.start();  
  22.   
  23.     }  
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();

    }
差不多就是这样了,第一次写博客,哈哈,大家多担待,有问题请大家多多指出哦!


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值