大哥们我现在要做android手机之间的ping业务。
 怎么获取手机自己的IP?
大哥们小弟先在这谢谢了····

解决方案 »

  1.   

    内网IP很容易查这个网址可以查看外网IP
    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> 
      

  2.   

    //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>
      

  3.   

    大哥我不需要外网的IP... 
       我要的时手机自己的IP.也就是手机IP,不是外网的IP. 谢谢····
      

  4.   

    看看这里http://blog.csdn.net/android_tutor/article/details/5576544;可以输入你要的IP;
      

  5.   

    大哥谢谢了···
    getLocalIpAddress()得到的是  10.0.2.15····好像是ip ```
      

  6.   

    谢谢了···大哥··楼上的yinyuanqings  说的好像是对的···
     
      

  7.   

    3楼正解封人的技术也太差了,直接销户啊small person
      

  8.   

    因为我上班那只可以上csdn 和一些技术性论坛,别的不可以上····所以就发帖 问问大家了····
       谢谢····