使用wifi ip的位置

时间:2012-05-14 10:17:35

标签: android

我试图找到我的网络的IP并使用该IP获取位置。但是当连接到2G网络时,我的代码返回正确的ip(223.187.19.157)。但是当我连接到我的wifi时,它会像这样返回ip fe80 :: 7ad6:f0ff:fe2b:dca6%wlan0 。谁能帮我找到正确格式的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;
    }

1 个答案:

答案 0 :(得分: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));
                    }
            }

    }