2012-01-11 49 views
2

我在ubuntu上使用eclipse + android SDK,并使用套接字运行测试活动服务器。尽管互联网许可设置在清单上,但我无法在Android设备上获得我的ip

我的清单有上网权限

<uses-permission android:name="INTERNET"/> 

但是,当我使用查找我的IP设备上:

// gets the ip address of your device 
private String getLocalIpAddress() 
{ 
    try 
    { 
     for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) 
     { 
      NetworkInterface intf = en.nextElement(); 
      for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) 
      { 
       InetAddress inetAddress = enumIpAddr.nextElement(); 
       if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } 
      } 
     } 
    } 
    catch (SocketException ex) 
    { 
     Log.e("ServerActivity", ex.toString()); 
    } 
    return null; 
} 

我得到一个异常的logcat的:

Java.net.SocketException: Permission denied 

标签:MyActivity,但我有清单上的互联网许可。

,如果我尝试手动将IP,当我使用插座,我也得到与TAG例外:System.err的

有关问题的一些想法?

在此先感谢。

+1

你应该更仔细地检查你的清单文件,你放置它使用权限标签。我刚刚测试了代码,它工作正常。 – 2012-01-11 15:23:03

+1

其他项目的许可工作,我认为是在正确的地方。但感谢评论。 – vgonisanz 2012-01-11 15:36:24

回答

2

我认为你必须这样写权限清单中的XML:

<uses-permission android:name="android.permission.INTERNET"/> 

,而不是只有“互联网”。以防万一。

+2

是的,问题是这样的。非常感谢Jav_Rock。我是个笨蛋。 – vgonisanz 2012-01-11 15:38:26

+2

我很高兴看到这个! :) – 2012-01-11 15:39:37

相关问题