# 前言 #
本篇文章主要介紹android如何獲取本機(jī)ip地址及ip歸屬地。
# 定義 #
ip地址是指手機(jī)在連接到互聯(lián)網(wǎng)時(shí)所獲得的唯一網(wǎng)絡(luò)地址。
ip歸屬地是指通過(guò)ip地址查詢器對(duì)應(yīng)的地理位置信息,如省市區(qū)等信息。
# 獲取ip地址 #
如果只是查看本機(jī)ip,不涉及應(yīng)用開(kāi)發(fā),可以依次打開(kāi)手機(jī)設(shè)置-我的設(shè)備-狀態(tài)信息-ip地址界面進(jìn)行查看(不同品牌手機(jī)型號(hào)會(huì)有差異)。
下面開(kāi)發(fā)過(guò)程中獲取本機(jī)ip方法:
1.首先是要在清單文件中配置必要的權(quán)限:
2.手機(jī)在不同的網(wǎng)絡(luò)環(huán)境下獲取ip的方法:
//獲取ip
public void getIPAddress(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
//網(wǎng)絡(luò)連接可用,判斷網(wǎng)絡(luò)連接類型
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
//wifi網(wǎng)絡(luò)
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = String.format("%d.%d.%d.%d",
(ipAddress & 0xff),
(ipAddress >> 8 & 0xff),
(ipAddress >> 16 & 0xff),
(ipAddress >> 24 & 0xff));
Log.e("tag", "ip:" + ip);
} else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
//移動(dòng)網(wǎng)絡(luò)
getLocalIpAddress();
}
} else {
//沒(méi)有網(wǎng)絡(luò)鏈接
}
}
private void getLocalIpAddress() {
try {
ArrayList networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface networkInterface : networkInterfaces) {
ArrayList inetAddresses = Collections.list(networkInterface.getInetAddresses());
for (InetAddress address : inetAddresses) {
String ip = address.getHostAddress();
if (!address.isLoopbackAddress() && (address instanceof Inet4Address)) {
Log.e("tag", "ipv4:" + ip);
}
if (!address.isLoopbackAddress() && (address instanceof Inet6Address)) {
Log.e("tag", "ipv6:" + ip);
}
}
}
} catch (SocketException socketException) {
Log.e("tag", socketException.getMessage());
}
}
# 獲取ip歸屬地 #
想要獲取到ip歸屬地,一般需要獲取到ip地址后通過(guò)第三方服務(wù)來(lái)查詢,下面展示一下android使用ip數(shù)據(jù)云獲取ip歸屬地的具體方法:
//獲取ip歸屬地
private Location getIpData(String ip, String key) {
Location location = null;
try {
URL url = new URL("https://api.ipdatacloud.com/v2/query?ip=" + ip + "&key=+" + key);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
connection.disconnect();
// 解析返回的JSON數(shù)據(jù),獲取IP歸屬地信息
// 這里需要使用JSON解析庫(kù),例如gson、fastjson
String jsonResult = sb.toString();
location = new Gson().fromJson(jsonResult, Location.class);
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
class Location {
private String AreaCode; //行政區(qū)碼
private String City; //城市
private String CityCode; //城市代碼
private String Continent; //洲
private String Country; //國(guó)家/地區(qū)
private String CountryCode; //國(guó)家/地區(qū)英文簡(jiǎn)寫(xiě)
private String District; //區(qū)縣
private String Elevation; //海拔
private String Ip; //ip地址
private String Isp; //運(yùn)營(yíng)商
private String Latitude; //緯度
private String Longitude; //經(jīng)度
private Street[] MultiStreet; //歷史街道位置
private String Province; //省份
private String Street; //街道
private String TimeZone; //時(shí)區(qū)
private String WeatherStation; //氣象站
private String ZipCode; //郵編
}
class Street {
private String Lng; //經(jīng)度
private String Lat; //緯度
private String Province; //省份
private String City; //城市
private String District; //區(qū)縣
private String Street; //街道
private String Radius; //范圍半徑
private String ZipCode; //郵政編碼
}
# 總結(jié) #
本文簡(jiǎn)要總結(jié)了android獲取ip地址及歸屬地的方法,在實(shí)際開(kāi)發(fā)中還需要根據(jù)自身的實(shí)際情況進(jìn)行修改。
在眾多第三方服務(wù)中,ip數(shù)據(jù)云作為新一代ip地址數(shù)據(jù)服務(wù)領(lǐng)軍者,為廣大開(kāi)發(fā)者提供了豐富的產(chǎn)品服務(wù),具體可去官網(wǎng)https://www.ipdatacloud.com/?utm-source=WZJ&utm-keyword=?2826進(jìn)行測(cè)試、咨詢。
審核編輯 黃宇
-
Android
+關(guān)注
關(guān)注
12文章
3935瀏覽量
127335 -
IP
+關(guān)注
關(guān)注
5文章
1701瀏覽量
149500
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論