編輯:關於 Android 程式設計
很多人搜尋「取得 WiFi IP」時,真正想拿的其實是兩種完全不同的東西。一種是裝置在區網中的私有 IP,透過 WifiManager.getConnectionInfo().getIpAddress() 就能取得,完全不需要網路;另一種是對外那個公開 IP,則必須發請求問外部服務。
以下說明的是後者。
public static String GetNetIp() {
URL infoUrl = null;
InputStream inStream = null;
try {
infoUrl = new URL("https://ip38.com");
URLConnection connection = infoUrl.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection) connection;
if (httpConnection.getResponseCode() == 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 字串
line = strber.substring(378, 395);
return line.replaceAll(" ", "");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
這個寫法能動,但很脆弱。用固定的字元索引 substring(378, 395) 去切字串,等於把對方網頁的排版寫死在程式碼裡——對方改一次版面,你的功能就壞了,而且壞得毫無徵兆。只適合臨時應急。
比較可靠的方式是找一個提供結構化資料的服務,例如淘宝的 IP 查詢介面:
public static String GetNetIp() {
String IP = "";
try {
String address = "https://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 {
Log.e("提示", "IP 介面異常,無法取得 IP 位址!");
}
} else {
Log.e("提示", "網路連線異常,無法取得 IP 位址!");
}
} catch (Exception e) {
Log.e("提示", "取得 IP 位址時出現異常,資訊是:" + e.toString());
}
return IP;
}
相較之下,這種做法有幾個明顯優勢:解析的是結構化欄位而非字元位置,介面改版時只要欄位名稱不變就不受影響;同時還能拿到國家、地區、城市與 ISP 等額外資訊;每個失敗分支都有明確的錯誤訊息,排查問題時不必猜。
這個操作必須放在主執行緒以外,NetworkOnMainThreadException 是必然的結果。此外記得在 manifest 加上 android.permission.INTERNET。
還有一點值得思考:取得「外網 IP」只能知道出口位址,如果使用者同時開著 VPN 或代理,拿到的就不是真實網路的位址了。若需求其實是區網 IP,請改用 WifiManager,那樣既快又不需要任何權限以外的網路連線。
打造真正的下拉刷新 RecyclerView(二):頭尾部的增刪與封裝實戰
前言 上一篇我們跑通了「下拉刷新、上拉加載」的 RecyclerView 基本用法,從這篇開始進入實作細節,第一步先把頭部與尾部的添加、刪除做乾淨。 大家都清楚,Recy
React Native 如何用一份程式碼同時跑 Android 與 iOS
共用程式碼是 React Native 的核心價值 React Native 開發的 App,所有元件在 iOS 與 Android 上是共用的,也就是說你只需要維護一份
GreenDao 在 Android Studio 中的配置與使用全流程
GreenDao 的 GitHub 位址: https://github.com/greenrobot/greenDAO 先建立一個 gradle 空白專案,專案結構如下
Activity 與 Fragment 生命週期完整對比
Fragment 是 Android 3.0 引入的元件,用來解決「同一套程式碼要同時適配手機與平板」的問題。但它的生命週期並不是 Activity 的子集,而是多出了幾