2012-05-14 42 views
3

我试图找到我的网络ip,并使用该ip获取位置。但是,当连接到2G网络时,我的代码会返回正确的IP(223.187.19.157)。但是,当我连接到我的无线网络,它会返回像这样的IP fe80 :: 7ad6:f0ff:fe2b:dca6%wlan0。任何人都可以请帮我找到我的无线上网ip格式正确。代码如下使用wifi ip的位置

public String getIpAddress() { 
     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) { 
      ex.printStackTrace(); 
     } 
     return null; 
    } 
+0

您需要使用WifiManager作为描述在这个所谓的答案](http://stackoverflow.com/questions/7975473/ detect-wifi-ip-address-on-android) – keyser

+0

我和我的中文平板电脑上的这段代码有完全相同的问题。但它在我的SGS手机上可以正常工作。 – deviant

回答

1

试试这个代码,希望这将有助于你

import java.net.InetAddress; 
import java.net.NetworkInterface; 
import java.net.Socket; 
import java.net.SocketException; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Collections; 
import java.util.Enumeration; 
import java.util.Iterator; 
import java.util.List; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 

    public class WhatIsMyIP extends Activity { 
      private static final String TAG = WhatIsMyIP.class.getSimpleName(); 

      @Override 
      public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 

        new Thread(new Runnable() { 
          public void run() { 
            initUI(); 
          } 
        }).run(); 
      } 

      private List<String> getIpAddresses() { 
        List<String> ips = new ArrayList<String>(); 
        try { 
          for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en 
            .hasMoreElements();) { 
            NetworkInterface intf = en.nextElement(); 
            for (Enumeration<InetAddress> e = intf.getInetAddresses(); e.hasMoreElements();) { 
              InetAddress inetAddress = e.nextElement(); 
              if (!inetAddress.isLoopbackAddress()) 
                ips.add(inetAddress.getHostAddress().toString()); 
            } 
          } 
        } catch (SocketException ex) { 
          Log.e(TAG, ex.toString(), ex); 
        } 
        return !ips.isEmpty() ? ips : Collections.<String> emptyList(); 
      } 

      private String getSocketIPAdress() { 
        Socket conn = null; 
        String result = null; 
        try { 
          try { 
            conn = new Socket("www.google.com", 80); 
            result = conn.getLocalAddress().toString(); 
          } finally { 
            if (conn != null && !conn.isClosed()) 
              conn.close(); 
          } 
        } catch (Throwable t) { 
          Log.i(TAG, t.getMessage(), t); 
        } 
        return result; 
      } 

      private void initUI() { 
        List<String> ips = getIpAddresses(); 
        final String ipAddress = !ips.isEmpty() ? join(ips, ", ") : getSocketIPAdress(); 
        runOnUiThread(new Runnable() { 
          public void run() { 
            updateTextView(ipAddress); 
          } 
        }); 
      } 

      private String join(Collection<?> s, String delimiter) { 
        StringBuffer buffer = new StringBuffer(); 
        Iterator<?> iter = s.iterator(); 
        while (iter.hasNext()) { 
          buffer.append(iter.next()); 
          if (iter.hasNext()) { 
            buffer.append(delimiter); 
          } 
        } 
        return buffer.toString(); 
      } 

      private void updateTextView(String ipAddress) { 
        TextView textView = (TextView) findViewById(R.id.ip_address); 
        if (ipAddress != null) { 
          textView.setText(getString(R.string.ip_address) + ipAddress); 
        } else { 
          textView.setText(getString(R.string.ip_address) + getString(R.string.not_available)); 
        } 
      } 

    }