Jsoup HTML解析在Java中正常运行,Android Studio中计时器解析失败
Hey there! Let's break down why your eBay timer parsing works in standard Java but fails on Android—this is a super common issue with a few straightforward fixes.
Common Causes & Solutions
1. Mismatched User-Agent String
Ebay (like many sites) serves different content based on the request's User-Agent. Jsoup uses a default UA in Android that might be flagged as a non-browser client, leading to missing elements in the response.
Fix: Force a familiar browser User-Agent in your request:
public void EbayTimerTest() { new Thread(new Runnable() { @Override public void run() { Document doc; try { doc = Jsoup.connect(WEBSITE_URL) // Use a common mobile browser UA .userAgent("Mozilla/5.0 (Linux; Android 13; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36") .timeout(10000) // Add a generous timeout to avoid early cuts .get(); // Add a null check to prevent crashes if the element is missing Element timerElement = doc.select("#vi-cdown_timeLeft").first(); if (timerElement != null) { String remaining = timerElement.text(); // Remember to update UI on the main thread if needed runOnUiThread(() -> { // Handle your timer display logic here }); } else { Log.e("EbayTimer", "Timer element not found in the response"); } } catch (IOException e) { e.printStackTrace(); Log.e("EbayTimer", "Request failed: " + e.getMessage()); } } }).start(); }
2. JavaScript-Driven Timer Content
Ebay's countdown timer is almost certainly rendered dynamically with JavaScript. Jsoup doesn't execute JS, so if the element is added after the initial page load, you won't see it in the raw HTML response.
Fix: Use Android's WebView to fully render the page first, then extract the HTML:
// Make sure to initialize WebView on a UI thread or handle context properly WebView webView = new WebView(this); webView.getSettings().setJavaScriptEnabled(true); webView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); // Extract the fully rendered HTML view.evaluateJavascript("document.documentElement.outerHTML", html -> { if (html != null) { // Clean up escaped characters from the JS response String cleanedHtml = html.replace("\"", "").replace("\\n", ""); Document doc = Jsoup.parse(cleanedHtml); Element timerElement = doc.select("#vi-cdown_timeLeft").first(); if (timerElement != null) { String remaining = timerElement.text(); // Use the timer value in your app logic here } } }); } }); webView.loadUrl(WEBSITE_URL);
3. Missing Permissions or SSL Issues
Double-check your AndroidManifest.xml includes the internet permission—this is easy to overlook:
<uses-permission android:name="android.permission.INTERNET" />
If you're hitting SSL certificate errors, you can temporarily ignore them (not recommended for production apps):
doc = Jsoup.connect(WEBSITE_URL) .userAgent("your-UA-string") .ignoreHttpErrors(true) .get();
Quick Debug Tip
Print the full HTML response to see what Jsoup is actually receiving—this will confirm if the #vi-cdown_timeLeft element exists or if the page is blocking your request:
Log.d("EbayResponse", doc.html());
内容的提问来源于stack exchange,提问作者Daniele Asta

