如何判断android设备的IP地址是否公开

发布于 2024-10-06 12:38:54 字数 643 浏览 5 评论 0原文

当手机设备连接3G网络时,如何判断android设备的ip地址是否是公共的?

public static InetAddress getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress address = enumIpAddr.nextElement();
                return address;
            }
        }
    } catch (SocketException ex) {
        Log.e(DEBUG_TAG, ex.toString());
    }
    return null;
}

When phone device connects on 3G network, How can I determine How to determine whether android device's ip address is public or not?

public static InetAddress getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress address = enumIpAddr.nextElement();
                return address;
            }
        }
    } catch (SocketException ex) {
        Log.e(DEBUG_TAG, ex.toString());
    }
    return null;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

音盲 2024-10-13 12:38:54

编写一个像这样的快速小网页:

<?php echo $_SERVER["REMOTE_ADDR"]; ?>

然后使用异步任务获取它:

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;

public class NetreadTask extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... urls) {
        return netread(urls[0]);
    }

    public final String netread(String url) {
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            ResponseHandler<String> resHandler = new BasicResponseHandler();
            String page;
            page = httpClient.execute(httpGet, resHandler);
            return page;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    }
}

像这样调用它:

String publicip = new NetreadTask().exec([yourpage]).get();

然后比较两者。通过上述方法获得的ip保证为您提供公网ip地址。

我知道这是一个老问题,但我希望它对某人有所帮助!

Write a quick little webpage like this:

<?php echo $_SERVER["REMOTE_ADDR"]; ?>

Then fetch that with an async task:

import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;

public class NetreadTask extends AsyncTask<String, Void, String> {
    protected String doInBackground(String... urls) {
        return netread(urls[0]);
    }

    public final String netread(String url) {
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            ResponseHandler<String> resHandler = new BasicResponseHandler();
            String page;
            page = httpClient.execute(httpGet, resHandler);
            return page;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    }
}

Call it like:

String publicip = new NetreadTask().exec([yourpage]).get();

Then compare the two. The ip you get via the method above is guaranteed to give you the public ip address.

I know this is an old question but I hope it helps somebody!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文