【android获取wifi外网ip的方法】android获取wifi外网ip的方法

更新时间:2019-12-07    来源:.Net开发    手机版     字体:

【www.bbyears.com--.Net开发】

android获取wifi外网ip的方法

// 获取外网IP  
public static String GetNetIp() {  
    URL infoUrl = null;  
    InputStream inStream = null;  
    try {  
        // http://iframe.ip138.com/ic.asp  
        // infoUrl = new URL("http://city.ip138.com/city0.asp");  
        infoUrl = new URL("http://ip38.com");  
        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();  
            // 从反馈的结果中提取出IP地址  
            // int start = strber.indexOf("[");  
            // Log.d("zph", "" + start);  
            // int end = strber.indexOf("]", start + 1);  
            // Log.d("zph", "" + end);  
            line = strber.substring(378, 395);  
            line.replaceAll(" ", "");  
            return line;  
        }  
    } catch (MalformedURLException e) {  
        e.printStackTrace();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  
    return null;  
}


另有一个获取外网IP的高端方法

public static String GetNetIp()  
{  
String IP = "";  
try  
{  
String address = "http://ip.taobao.com/service/getIpInfo2.php?ip=myip";  
URL url = new URL(address);  
HttpURLConnection connection = (HttpURLConnection) url  
.openConnection();  
connection.setUseCaches(false);  
  
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK)  
{  
InputStream in = connection.getInputStream();  
  
// 将流转化为字符串  
BufferedReader reader = new BufferedReader(  
new InputStreamReader(in));  
  
String tmpString = "";  
StringBuilder retJSON = new StringBuilder();  
while ((tmpString = reader.readLine()) != null)  
{  
retJSON.append(tmpString + "\\n");  
}  
  
JSONObject jsonObject = new JSONObject(retJSON.toString());  
String code = jsonObject.getString("code");  
if (code.equals("0"))  
{  
JSONObject data = jsonObject.getJSONObject("data");  
IP = data.getString("ip") + "(" + data.getString("country")  
+ data.getString("area") + "区"  
+ data.getString("region") + data.getString("city")  
+ data.getString("isp") + ")";  
  
Log.e("提示", "您的IP地址是:" + IP);  
}  
else  
{  
IP = "";  
Log.e("提示", "IP接口异常,无法获取IP地址!");  
}  
}  
else  
{  
IP = "";  
Log.e("提示", "网络连接异常,无法获取IP地址!");  
}  
}  
catch (Exception e)  
{  
IP = "";  
Log.e("提示", "获取IP地址时出现异常,异常信息是:" + e.toString());  
}  
return IP;  
}



Android 获取wifi的IP地址

WifiManager wifimanage=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);//获取WifiManager  
  
//检查wifi是否开启  
  
if(!wifimanage.isWifiEnabled())  {  
  
  wifimanage.setWifiEnabled(true);  
  
}  
  
WifiInfo wifiinfo= wifimanage.getConnectionInfo();  
  
String ip=intToIp(wifiinfo.getIpAddress());  
  
//将获取的int转为真正的ip地址,参考的网上的,修改了下  
  
private String intToIp(int i)  {
  return (i & 0xFF)+ "." + ((i >> 8 ) & 0xFF)? + "." + ((i >> 16 ) & 0xFF) +"."+((i >> 24 ) & 0xFF );
}

    

OK,这样就好了吗?呵呵,别忘记加上权限  


  



android开发 获取WIFI和有线的IP地址

首先设置权限:

  
  
/** 
* if (intf.getName().toLowerCase().equals("eth0") || intf.getName().toLowerCase().equals("wlan0")) 
* 表示:仅过滤无线和有线的ip. networkInterface是有很多的名称的 
* 比如sim0,remt1.....等等.我不需要用到就直接过滤了 
*  
* if (!ipaddress.contains("::"))  
* 表示: 过滤掉ipv6的地址.不管无线还是有线 都有这个地址, 
* 我这边显示地址大体是:fe80::288:88ff:fe00:1%eth0 fe80::ee17:2fff:fece:c0b4%wlan0 
* 一般都是出现在第一次循环.第二次循环就是真正的ipv4的地址. 
*  
* @return 
* @throws SocketException 
*/  
public String GetIpAddress() throws SocketException {  
String ipaddress = "";  
Enumeration netInterfaces = null;  
try {  
netInterfaces = NetworkInterface.getNetworkInterfaces();  
while (netInterfaces.hasMoreElements()) {  
NetworkInterface intf = netInterfaces.nextElement();  
if (intf.getName().toLowerCase().equals("eth0") | | intf.getName().toLowerCase().equals("wlan0")) {  
for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {  
InetAddress inetAddress = enumIpAddr.nextElement();  
if (!inetAddress.isLoopbackAddress()) {  
ipaddress = inetAddress.getHostAddress().toString();  
if (!ipaddress.contains("::")) {// ipV6的地址  
ipaddress = ipaddress;  
}  
}  
}  
} else {  
continue;  
}  
}  
} catch (Exception e) {  
e.printStackTrace();  
}  
// final ContentResolver mContentResolver = getContentResolver();  
// Settings.System.putInt( mContentResolver,  
// Settings.System.WIFI_USE_STATIC_IP, 1);  
// Settings.System.putString( mContentResolver,  
// Settings.System.WIFI_STATIC_IP, "你的ip地址");  
  
return ipaddress;  
}  
  
  
public String getAddress() {  
WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);  
// 判断wifi是否开启  
if (!wifiManager.isWifiEnabled()) {  
wifiManager.setWifiEnabled(true);  
}  
WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
DhcpInfo info = wifiManager.getDhcpInfo();  
int ipAddress = wifiInfo.getIpAddress();  
int ii = info.ipAddress;  
// return intToIp(ipAddress);  
return ipAddress + "    dhcp: " + ii;  
  
}  
  
  
private String intToIp(int i) {  
return (i & 0xFF) + "." +  
((i >> 8) & 0xFF) + "." +  
((i >> 16) & 0xFF) + "." +  
(i >> 24 & 0xFF);  
}  
}


本文来源:http://www.bbyears.com/asp/82531.html

热门标签

更多>>

本类排行