Android获取 公网ip

一、通过某些企业提供的服务

  • 通过 http://ip.chinaz.com/getip.aspx
  • 通过 http://pv.sohu.com/cityjson

浏览器 get请求 效果图:
Android获取 公网ip_第1张图片

git地址:
https://github.com/xiaxveliang/Android_HttpUrlConnect_Demo

二、Head请求 某个网络地址

查看其Response Header数据
在这里插入图片描述

/**
 * create by xiaxl.
 * 获取:
 * 用户公网ip
 * cdn ip
 * via
 */
public class CdnUtil {


    private static final String TAG = "CdnUtil";


    /**
     * @param url
     * @param callBack
     */
    public static void getCdnInfo(final String url, final OnCdnInfoCallBack callBack) {
        Task.call(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                // UI_Thread

                return null;
            }
        }, Task.UI_THREAD_EXECUTOR).onSuccess(new Continuation<Void, CdnInfoBean>() {
            @Override
            public CdnInfoBean then(Task<Void> task) throws Exception {
                // Background_Thread
                return getResponseCDN_Sync(url);
            }
        }, Task.BACKGROUND_EXECUTOR).continueWith(new Continuation<CdnInfoBean, Void>() {
            @Override
            public Void then(Task<CdnInfoBean> task) throws Exception {
                // UI_Thread
                if (task != null && task.getResult() != null) {
                    CdnInfoBean cdnBean = task.getResult();
                    if (callBack != null) {
                        callBack.onSuccess(cdnBean);
                    }
                } else {
                    if (callBack != null) {
                        callBack.onError();
                    }
                }
                return null;
            }
        }, Task.UI_THREAD_EXECUTOR);
    }


    /**
     * 返回相应的cdn服务器ip
     * 

* 同步方法(需开线程执行) * add by xiaxl * * @return */ private static CdnInfoBean getResponseCDN_Sync(String url) { PalLog.d(TAG, "---getResponseCDN_Sync---"); // CdnInfoBean cdnBean = new CdnInfoBean(); // if (TextUtils.isEmpty(url)) { return cdnBean; } try { URL obj = new URL(url); URLConnection conn = obj.openConnection(); Map<String, List<String>> map = conn.getHeaderFields(); for (Map.Entry<String, List<String>> entry : map.entrySet()) { if (entry.getKey() != null) { if (entry.getKey().equals(CdnInfoBean.CDN_IP)) { cdnBean.cdnIp = entry.getValue().toString(); } else if (entry.getKey().equals(CdnInfoBean.CDN_USER_IP)) { cdnBean.cdnUserIp = entry.getValue().toString(); } else if (entry.getKey().equals(CdnInfoBean.X_VIA)) { cdnBean.xVia = entry.getValue().toString(); } } } } catch (Exception e) { e.printStackTrace(); } PalLog.d(TAG, "cdnBean: " + cdnBean); return cdnBean; } /** * cdn 信息 */ public static class CdnInfoBean { public static final String CDN_IP = "cdn-ip"; public static final String CDN_USER_IP = "cdn-user-ip"; public static final String X_VIA = "X-Via"; /** * */ public String cdnIp; public String cdnUserIp; public String xVia; @Override public String toString() { StringBuffer sb = new StringBuffer(); sb.append("cdn-ip: "); sb.append(cdnIp); sb.append(" cdn-user-ip: "); sb.append(cdnUserIp); sb.append(" xVia: "); sb.append(xVia); return sb.toString(); } } public interface OnCdnInfoCallBack { void onSuccess(CdnInfoBean info); void onError(); } }

三、是否还有其他方式? 请留言谢谢

不知是否还有其他获取”运营商公网Ip“的方式,如果您知道,请您指导,谢谢

你可能感兴趣的:(Android获取 公网ip)