How to get IP address of the device from code?

2018-12-31 02:37发布

Is it possible to get the IP address of the device using some code?

20条回答
若你有天会懂
2楼-- · 2018-12-31 02:42

Simply use Volley to get the ip from this site

RequestQueue queue = Volley.newRequestQueue(this);    
String urlip = "http://checkip.amazonaws.com/";

    StringRequest stringRequest = new StringRequest(Request.Method.GET, urlip, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            txtIP.setText(response);

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            txtIP.setText("didnt work");
        }
    });

    queue.add(stringRequest);
查看更多
泛滥B
3楼-- · 2018-12-31 02:43

If you have a shell ; ifconfig eth0 worked for x86 device too

查看更多
大哥的爱人
4楼-- · 2018-12-31 02:44
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ipAddress = BigInteger.valueOf(wm.getDhcpInfo().netmask).toString();
查看更多
栀子花@的思念
5楼-- · 2018-12-31 02:45

Please check this code...Using this code. we will get ip from mobile internet...

for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
查看更多
只若初见
6楼-- · 2018-12-31 02:45

I don't do Android, but I'd tackle this in a totally different way.

Send a query to Google, something like: https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=my%20ip

And refer to the HTML field where the response is posted. You may also query directly to the source.

Google will most like be there for longer than your Application.

Just remember, it could be that your user does not have internet at this time, what would you like to happen !

Good Luck

查看更多
浅入江南
7楼-- · 2018-12-31 02:46

I used following code: The reason I used hashCode was because I was getting some garbage values appended to the ip address when I used getHostAddress . But hashCode worked really well for me as then I can use Formatter to get the ip address with correct formatting.

Here is the example output :

1.using getHostAddress : ***** IP=fe80::65ca:a13d:ea5a:233d%rmnet_sdio0

2.using hashCode and Formatter : ***** IP=238.194.77.212

As you can see 2nd methods gives me exactly what I need.

public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    String ip = Formatter.formatIpAddress(inetAddress.hashCode());
                    Log.i(TAG, "***** IP="+ ip);
                    return ip;
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(TAG, ex.toString());
    }
    return null;
}
查看更多
登录 后发表回答