This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Android and the fly likes to get ipaddress Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "to get ipaddress" Watch "to get ipaddress" New topic

to get ipaddress

akash priyadarshi

Joined: May 18, 2012
Posts: 6
hi, i am new in android development . i have written this code to get my ip address but it is returning null . please point out my mistake in the code.

package quiz.invigilator;

import java.util.Enumeration;

import android.R.string;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class QuizActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
String s = localip();
TextView tv = new TextView(this);
tv.setText("Hello, Android" + s);


protected String localip()
String localip = "a";
int i = 0;
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();i++) {
localip += i ;
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {

InetAddress inetAddress = enumIpAddr.nextElement();
localip += 'c' ;
if (!inetAddress.isLoopbackAddress()) {
localip+= 'd';
return inetAddress.getHostAddress().toString();
}catch (Exception e) {

Log.e("ServerActivity", e.toString());

return localip ;

It is sorta covered in the JavaRanch Style Guide.
subject: to get ipaddress
Similar Threads
how to get zim card number
simple hello world
Phone to phone/emulator comminication
emulator window was out of view and was recentred
class not found in Eclipse-built external jar, but found in Netbeans-built one