Android Tutorial - Network : IP

 Get Ip Address

   
//package com.totsp.bookworm.util;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.util.Log;


import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * NetworkUtils.
 * 
 * @author ccollins
 *
 */
public final class NetworkUtil {

   private NetworkUtil() {
   }

   public static String getIpAddress() {
      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()) {
                  return inetAddress.getHostAddress().toString();
               }
            }
         }
      } catch (SocketException e) {
        // Log.e(Constants.LOG_TAG, e.getMessage(), e);
      }
      return null;
   }

   public static boolean connectionPresent(final ConnectivityManager cMgr) {
      if (cMgr != null) {
         NetworkInfo netInfo = cMgr.getActiveNetworkInfo();
         if ((netInfo != null) && (netInfo.getState() != null)) {
            return netInfo.getState().equals(State.CONNECTED);
         } else {
            return false;
         }
      }
      return false;
   }
}

Get Ip Address Text

   

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;


 class WifiUtil {
   public static final String getIpAddressText(Context context) {
       WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
       WifiInfo wifiInfo = wifiManager.getConnectionInfo();
       int ipAddress = wifiInfo.getIpAddress();
       String strIPAddess = ((ipAddress >> 0) & 0xFF) + "." + ((ipAddress >> 8) & 0xFF) + "."
               + ((ipAddress >> 16) & 0xFF) + "." + ((ipAddress >> 24) & 0xFF);
       return strIPAddess;
   }

}

ip To Dotted Decimal

   
//package edu.umich.jezzball.net;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

class Utility
{
  static final int IP_ADDRESS_LENGTH = 32;

  public static String ipToDottedDecimal(int ipAddress)
  {
    int a = (ipAddress >> 0) & 0xFF, b = (ipAddress >> 8) & 0xFF, c = (ipAddress >> 16) & 0xFF, d = (ipAddress >> 24) & 0xFF;
    return Integer.toString(a) + "." + Integer.toString(b) + "." + Integer.toString(c) + "." + Integer.toString(d);
  }

}

get Local Ip Address

   
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

class Main {
  public static 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()) {
            return inetAddress.getHostAddress().toString();
          }
        }
      }
    } catch (SocketException ex) {
      ex.printStackTrace();
    }
    return null;
  }
}
Int To InetAddress
//package org.swiftp;

import java.net.InetAddress;
import java.net.UnknownHostException;

abstract class Util {
  public static byte byteOfInt(int value, int which) {
    int shift = which * 8;
    return (byte)(value >> shift); 
  }
  public static InetAddress intToInet(int value) {
    byte[] bytes = new byte[4];
    for(int i = 0; i<4; i++) {
      bytes[i] = byteOfInt(value, i);
    }
    try {
      return InetAddress.getByAddress(bytes);
    } catch (UnknownHostException e) {
      // This only happens if the byte array has a bad length
      return null;
    }
  }
  
}