Android中AsyncTask结合Jsoup解析后ArrayList始终为空求助
Hey there, let's dig into why your imgURLS ArrayList isn't getting populated when using your URLcollector AsyncTask—since you mentioned manual additions work and Jsoup functions normally in Eclipse, the issue is almost certainly tied to how the AsyncTask interacts with your list or thread timing.
Common Causes & Fixes
1. You're Not Using the Same ArrayList Instance in the AsyncTask
This is the most likely culprit. If you initialize imgURLS in onCreate() but the AsyncTask creates its own separate ArrayList (or references a different one), any additions in the background thread won't show up in the list you're checking in the main thread.
Fix: Pass your existing ArrayList to the AsyncTask via its constructor:
class URLcollector extends AsyncTask<Void, Void, Void> { private ArrayList<String> imgURLS; // Constructor to receive the list reference public URLcollector(ArrayList<String> imgURLS) { this.imgURLS = imgURLS; } @Override protected Void doInBackground(Void... voids) { try { Document doc = Jsoup.connect("your-personal-site-url").get(); Elements imageElements = doc.select("img[src]"); // Log to confirm Jsoup is finding elements Log.d("URLcollector", "Found " + imageElements.size() + " images"); for (Element img : imageElements) { String absoluteUrl = img.attr("abs:src"); // Use abs:src to get full URL imgURLS.add(absoluteUrl); Log.d("URLcollector", "Added URL: " + absoluteUrl); } } catch (IOException e) { e.printStackTrace(); Log.e("URLcollector", "Failed to parse site: " + e.getMessage()); } return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); // Only access the list AFTER the AsyncTask finishes here if (!imgURLS.isEmpty()) { loadImagesIntoImageView(imgURLS); // Call your image loading method } else { Log.w("URLcollector", "List is still empty after parsing"); } } }
Then initialize and execute the AsyncTask in onCreate() like this:
ArrayList<String> imgURLS = new ArrayList<>(); new URLcollector(imgURLS).execute();
2. You're Accessing the List Before the AsyncTask Finishes
AsyncTask runs doInBackground() on a background thread—if you try to use imgURLS immediately after calling execute(), the background task probably hasn't finished parsing yet, so the list will still be empty.
Fix: Always wait until onPostExecute() (which runs on the main thread) to use the populated list. That's the safe place to trigger your ImageView loading logic.
3. Missing Internet Permission
Even if Jsoup works in Eclipse, Android requires explicit permission to access the internet. If you haven't added this to your AndroidManifest.xml, the network call will fail silently (or throw an exception), leaving your list empty.
Fix: Add this line inside your <manifest> tag (before <application>):
<uses-permission android:name="android.permission.INTERNET" />
4. Thread Safety Concerns (Optional)
While ArrayList works for single-threaded access, if you ever need to modify the list from multiple threads, consider using a thread-safe alternative like CopyOnWriteArrayList:
CopyOnWriteArrayList<String> imgURLS = new CopyOnWriteArrayList<>();
It behaves almost identically to ArrayList but handles concurrent read/write operations safely.
Quick Checks to Verify
- Check Logcat for the debug messages we added—if you don't see "Found X images", Jsoup isn't parsing any elements (double-check your selector or site URL).
- Confirm the AsyncTask is actually being executed (no early return or conditional that skips
execute()).
内容的提问来源于stack exchange,提问作者k copy k

