Meaningless Drivel is fun!*
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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "to get ipaddress" Watch "to get ipaddress" New topic
Author

to get ipaddress

akash priyadarshi
Greenhorn

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. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String s = localip();
TextView tv = new TextView(this);
tv.setText("Hello, Android" + s);
setContentView(tv);


}

protected String localip()
{
String localip = "a";
int i = 0;
try{
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