Example usage for java.net InetAddress getHostAddress

List of usage examples for java.net InetAddress getHostAddress

Introduction

In this page you can find the example usage for java.net InetAddress getHostAddress.

Prototype

public String getHostAddress() 

Source Link

Document

Returns the IP address string in textual presentation.

Usage

From source file:Main.java

public static String getIpInfo() {
    String ipInfo = null;//from w  w  w.j ava2  s  .c  o m

    try {
        Enumeration<NetworkInterface> faces = NetworkInterface.getNetworkInterfaces();

        LOOP: while (faces.hasMoreElements()) {
            Enumeration<InetAddress> addresses = faces.nextElement().getInetAddresses();

            while (addresses.hasMoreElements()) {
                InetAddress inetAddress = addresses.nextElement();

                if (!inetAddress.isLoopbackAddress()) {
                    ipInfo = inetAddress.getHostAddress().toString();

                    break LOOP;
                }
            }
        }

    } catch (Exception e) {
    }

    if (TextUtils.isEmpty(ipInfo)) {
        ipInfo = "";
    }

    return ipInfo;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {/*from   w ww.jav  a2  s  .  c  o m*/
        Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
        while (en.hasMoreElements()) {
            NetworkInterface intf = en.nextElement();
            String name = intf.getName();
            if (name.compareTo("eth0") == 0 || name.compareTo("wlan0") == 0) {
                Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
                while (enumIpAddr.hasMoreElements()) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (inetAddress.getClass() == Inet4Address.class) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("MY", ex.toString());
    }
    return null;
}

From source file:Main.java

public static HashMap<String, ArrayList<String>> getLocalIpAddresses() {
    try {/*  w  w w. j  a  v  a2s.co m*/
        HashMap<String, ArrayList<String>> result = new HashMap<String, ArrayList<String>>();
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();

            String name = intf.getName();
            ArrayList<String> addresses = new ArrayList<String>();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {

                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    addresses.add(inetAddress.getHostAddress().toString());
                }
            }
            result.put(name, addresses);
        }
        return result;
    } catch (SocketException ex) {
        Log.e(TAG, ex.toString());
    }
    return null;
}

From source file:Main.java

public static String getIpAddress() {
    String ipaddress = "";

    try {//from   w w w .  j ava2s .  c o  m
        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()) {
                    ipaddress = ipaddress + ";" + inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        ipaddress = null;
        Log.e("WifiPreference IpAddress", ex.toString());
    }
    return ipaddress;
}

From source file:Main.java

public static String getGPRSIP() {
    String ip = null;/* w w w  .  j  av  a  2s .  c o  m*/
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            for (Enumeration<InetAddress> enumIpAddr = en.nextElement().getInetAddresses(); enumIpAddr
                    .hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    ip = inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
        ip = null;
    }
    return ip;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {//from  w  w w  .j av  a2s. c  o  m
        String ipv4;
        List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface ni : nilist) {
            List<InetAddress> ialist = Collections.list(ni.getInetAddresses());
            for (InetAddress address : ialist) {
                if (!address.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) {
                    return ipv4;
                }
            }
        }
    } catch (SocketException ex) {
    }

    return null;
}

From source file:io.selendroid.server.SelendroidStandaloneServer.java

private static URI remoteUri(int port) {
    try {/*ww  w . j a v a 2  s  .co  m*/
        InetAddress address = InetAddress.getByName("0.0.0.0");

        return new URI("http://" + address.getHostAddress() + (port == 80 ? "" : (":" + port)) + "/");
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("can not create URI from HostAddress", e);
    }
}

From source file:Main.java

public static String getIpWithoutWifi() {
    try {//from w  ww .j a  v  a 2  s.com
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
                .hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumeration = intf.getInetAddresses(); en.hasMoreElements();) {
                InetAddress inetAddress = enumeration.nextElement();
                if (inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (Exception e) {
        Log.w(TAG, "getIpWithoutWifi error:" + e.getMessage());
    }
    return null;
}

From source file:Main.java

public static String getLocalIpAddress() {
    try {/*from  w  w w .ja va 2  s  .c  o m*/
        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() && !inetAddress.isLinkLocalAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("WifiPreference IpAddress", ex.toString());
    }
    return null;

}

From source file:Main.java

public static String getIpAddress() {
    try {//from ww  w .ja v a  2  s . com
        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() && inetAddress instanceof Inet4Address) {

                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}