get Host Ip - Android Network

Android examples for Network:IP Address

Description

get Host Ip

Demo Code


//package com.java2s;

import java.net.InetAddress;
import java.net.NetworkInterface;

import java.util.Enumeration;

public class Main {

    public static String getHostIp() {
        try {/*www  .  j  a v a2s.c  o m*/
            Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces();
            while (null != en && en.hasMoreElements()) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> ipAddr = intf
                        .getInetAddresses(); ipAddr.hasMoreElements();) {
                    InetAddress inetAddress = ipAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related Tutorials