Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.text.TextUtils;

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

import java.util.Enumeration;

public class Main {

    public static String getIpInfo() {
        String ipInfo = null;

        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;
    }
}