jQuery in Action, 3rd edition
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 REST with Spring (video course) this week in the Spring 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.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.R.string;
import android.app.Activity;
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 ;

I agree. Here's the link: http://aspose.com/file-tools
subject: to get ipaddress
It's not a secret anymore!