Android開發之獲取網路IP位址方法總結

2021-10-01 01:49:21 字數 1156 閱讀 1207

1.1wifi下獲取本地區域網ip位址

// wifi下獲取本地網路ip位址(區域網位址)

public static string getlocalipaddress(context context)

return "";

}

1.2.流動網路獲取網路ip位址
// 獲取有限網ip

public static string gethostip() }}

} catch (exception ex)

return "0.0.0.0";

}

獲取android裝置的外網位址,即當前wifi網路真正的網路位址,也即是網路運營商分配給使用者的ip位址。

/**

* 獲取外網ip位址(非本地區域網位址)的方法

*/public static string getoutnetip()

jsonobject jsonobject = new jsonobject(retjson.tostring());

string code = jsonobject.getstring("code");

if (code.equals("0")) else

} else

} catch (exception e)

return ipaddress;

}

@suppresslint("missingpermission")

public static string getipaddress(context context)

connectivitymanager conmanager = (connectivitymanager) context

.getsystemservice(context.connectivity_service);

try else if (info.gettype() == connectivitymanager.type_wifi) else if (info.gettype() == connectivitymanager.type_ethernet)

}} catch (exception e)

return "";

}

android開發 獲取WIFI和有線的IP位址

首先設定許可權 if intf.getname tolowercase equals eth0 intf.getname tolowercase equals wlan0 表示 僅過濾無線和有線的ip.networkinte ce是有很多的名稱的 比如sim0,remt1.等等.我不需要用到就直接過...

Android開發之全域性獲取Context的技巧

第一行 android 高階篇 這本書對於入門來說確實很棒,很簡單明瞭的介紹了android開發中涉及到的方方面面,對我的幫助很大,同時記錄一些該書中一些對我以後開發有用的東西,以方便使用。1 public23 private static context context 45 override 6...

Android 網路獲取資料之XML解析

android中通過url獲取的資料中json格式和xml格式是兩種非常常見的響應返回格式 這篇文章主要講解通過pull解析的方式來解析xml格式的文件 2.1 xml 文字 例如我們通過url訪問網路,返回的是以下的xml格式文字。在xml文件中 整個文件內容稱之為乙個document.為乙個開始...