大侠们...快出来帮帮小弟!android 怎么获取手机自己的IP?```````` 大哥们我现在要做android手机之间的ping业务。 怎么获取手机自己的IP?大哥们小弟先在这谢谢了···· 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 内网IP很容易查这个网址可以查看外网IPhttp://fw.qq.com/ipaddresspublic String GetNetIp(String ipaddr){ URL infoUrl = null; InputStream inStream = null; try { infoUrl = new URL(ipaddr); URLConnection connection = infoUrl.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection)connection; int responseCode = httpConnection.getResponseCode(); if(responseCode == HttpURLConnection.HTTP_OK) { inStream = httpConnection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8")); StringBuilder strber = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) strber.append(line + "\n"); inStream.close(); return strber.toString(); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; } 查看 System.out.println((GetNetIp("http://fw.qq.com/ipaddress")));加权限 <uses-permission android:name="android.permission.INTERNET"></uses-permission> //Get Android local Mac address public String getLocalMacAddress(Context context) { WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); return info.getMacAddress(); }//Get IP address public InetAddress 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; } } } } catch (SocketException ex) { Log.e(debug_v_tag, ex.toString()); } return null; }AndroidManifest.xml <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 大哥我不需要外网的IP... 我要的时手机自己的IP.也就是手机IP,不是外网的IP. 谢谢···· 看看这里http://blog.csdn.net/android_tutor/article/details/5576544;可以输入你要的IP; 大哥谢谢了···getLocalIpAddress()得到的是 10.0.2.15····好像是ip ``` 谢谢了···大哥··楼上的yinyuanqings 说的好像是对的··· 3楼正解封人的技术也太差了,直接销户啊small person 因为我上班那只可以上csdn 和一些技术性论坛,别的不可以上····所以就发帖 问问大家了···· 谢谢···· 想自学Android开发,请大家推荐一下用哪本书比较好 用scrollview做瀑布流,如何实现点击后,利用galary进行浏览 电子杂志图片大小的问题,如何自适应屏幕减小APK大小 eclipse的模拟器设置问题 android PAD UI开发 类似分屏效果的疑问 在线等 怎么添加图片资源? 如何继承VC中的CString类 通讯录问题 有没有在android上实现过ssl服务器端的?问问怎么生成证书哇 android关机闹钟怎样启动的 List模板的问题? 在静态布局动态添加编辑框
http://fw.qq.com/ipaddress
public String GetNetIp(String ipaddr){
URL infoUrl = null;
InputStream inStream = null;
try {
infoUrl = new URL(ipaddr);
URLConnection connection = infoUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK)
{
inStream = httpConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream,"utf-8"));
StringBuilder strber = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
strber.append(line + "\n");
inStream.close();
return strber.toString();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
查看 System.out.println((GetNetIp("http://fw.qq.com/ipaddress")));
加权限 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
public String getLocalMacAddress(Context context) {
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
return info.getMacAddress();
}//Get IP address
public InetAddress 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;
}
}
}
} catch (SocketException ex) {
Log.e(debug_v_tag, ex.toString());
}
return null;
}AndroidManifest.xml <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
我要的时手机自己的IP.也就是手机IP,不是外网的IP. 谢谢····
getLocalIpAddress()得到的是 10.0.2.15····好像是ip ```
谢谢····