Android Studio中使用Jsoup提取用户公网IP遇Toast空白问题求助
Hey there! Let's troubleshoot why your Toast is coming up blank when trying to fetch that public IP. I’ve run into similar issues before, so here are the most likely fixes and a complete working example:
1. 你可能忘了添加网络权限
Android requires explicit permission to access the internet. Add this line to your AndroidManifest.xml (outside the <application> tag):
<uses-permission android:name="android.permission.INTERNET" />
Without this, your network request will silently fail, leaving no data to show in the Toast.
2. UI operations belong in the main thread
Your doit AsyncTask’s doInBackground method runs on a background thread—you can’t show a Toast from there. All UI updates (like Toast) must happen in the onPostExecute method, which runs on the main thread.
3. Network response parsing might be broken
If you’re not properly reading the response body or parsing the JSON, you’ll end up with a null/empty value. The target site returns {"ip":" current ip "}, so you’ll also want to trim the extra spaces around the IP string.
Here’s the full, corrected implementation of your AsyncTask:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_homepage); new FetchIpTask().execute(); // Renamed for better readability (Java convention) } private class FetchIpTask extends AsyncTask<Void, Void, String> { @Override protected String doInBackground(Void... voids) { String publicIp = null; try { // Replace with your target website URL URL url = new URL("https://your-target-ip-api.com"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); // 5-second timeout connection.setReadTimeout(5000); // Read the response body BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream()) ); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); connection.disconnect(); // Parse the JSON response JSONObject json = new JSONObject(response.toString()); publicIp = json.getString("ip").trim(); // Trim extra spaces } catch (Exception e) { e.printStackTrace(); // Log errors to debug what went wrong } return publicIp; // Pass result to onPostExecute } @Override protected void onPostExecute(String ip) { super.onPostExecute(ip); // This runs on the main thread—safe to show Toast here if (ip != null) { Toast.makeText(HomepageActivity.this, "Public IP: " + ip, Toast.LENGTH_LONG).show(); } else { Toast.makeText(HomepageActivity.this, "Failed to fetch IP", Toast.LENGTH_LONG).show(); } } }
- If your target site uses plain HTTP (not HTTPS) and your app targets Android 10+, add
android:usesCleartextTraffic="true"to the<application>tag inAndroidManifest.xmlto allow unencrypted requests. - For better network handling long-term, consider using modern libraries like OkHttp or Retrofit instead of
HttpURLConnection—they simplify error handling and response parsing.
内容的提问来源于stack exchange,提问作者Calum Johnston

