File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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 ;

I agree. Here's the link:
subject: to get ipaddress
It's not a secret anymore!