3

我试图找到我的网络的 ip 并使用该 ip 获取位置。但我的代码在连接到 2G 网络时返回正确的 ip(223.187.19.157)。但是当我连接到我的wifi时,它会返回这样的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;
    }
4

1 回答 1

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));
                    }
            }

    }
于 2012-05-14T10:41:06.250 回答