日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android 获取外网IP,实测有效

發(fā)布時(shí)間:2025/3/14 Android 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 获取外网IP,实测有效 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

網(wǎng)上有很多獲取IP的例子,不過(guò)都是獲取到的本地ip,還有的是因?yàn)樽卟煌?#xff0c;獲取到的ip為空,下面看實(shí)測(cè)獲取到外網(wǎng)IP的代碼,注意需要在線程里面執(zhí)行

?

/*** 獲取外網(wǎng)的IP(要訪問(wèn)Url,要放到后臺(tái)線程里處理)** @param @return* @return String* @throws* @Title: GetNetIp* @Description:*/public static String getNetIp() {URL infoUrl = null;InputStream inStream = null;String ipLine = "";HttpURLConnection httpConnection = null;try { // infoUrl = new URL("http://ip168.com/");infoUrl = new URL("http://pv.sohu.com/cityjson?ie=utf-8");URLConnection connection = infoUrl.openConnection();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");}Pattern pattern = Pattern.compile("((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))");Matcher matcher = pattern.matcher(strber.toString());if (matcher.find()) {ipLine = matcher.group();}}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {inStream.close();httpConnection.disconnect();} catch (IOException e) {e.printStackTrace();} catch (Exception ex) {ex.printStackTrace();}}Log.e("getNetIp", ipLine);return ipLine;}

?

轉(zhuǎn)載于:https://www.cnblogs.com/feijian/p/6601427.html

總結(jié)

以上是生活随笔為你收集整理的Android 获取外网IP,实测有效的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。