get Loopback IP Address - Android Network

Android examples for Network:IP Address

Description

get Loopback IP Address

Demo Code


//package com.java2s;
import java.net.InetAddress;
import java.net.UnknownHostException;
import android.os.Build;

public class Main {
    public static InetAddress getLoopbackAddress() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            return InetAddress.getLoopbackAddress();
        }//from  w  w  w .ja v a 2  s. co m

        try {
            return InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            // wtf...
            return null;
        }
    }
}

Related Tutorials