This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

to get ipaddress  RSS feed

akash priyadarshi
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ;

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!