将图像从网站加载到Android应用程序

时间:2013-04-20 16:36:12

标签: java android listview jsoup

我正在尝试使用Jsoup从网站加载一些图片。 我的应用程序一直在崩溃。我认为这是因为这条线:

Elements divs =((Elements) doc).select("img[src$=.jpg]");

以下是LoadImages类的代码:

public class LoadImages extends AsyncTask {

    private ArrayAdapter adapter;


private ArrayList list;


    public LoadImages(ArrayList list, Adapter adapter)
    {
        this.list = list;
        this.adapter = (ArrayAdapter) adapter;
    }

    protected void onPostExecute(Elements divs) {
        // TODO Auto-generated method stub
        super.onPostExecute(divs);
         for (org.jsoup.nodes.Element div : divs)
         {   
            list.add(div);
        }
        adapter.notifyDataSetChanged();

    }

    @Override
    protected Object doInBackground(Object... params) {
        Document doc = null;

        try {


        doc = (Document) Jsoup.connect("http://mongol.co.il/").get();
        Elements divs =((Elements) doc).select("img[src$=.jpg]");



         } catch (IOException e1) {

        e1.printStackTrace(); }
        return null;



    }



}

这也是主类:

public class MainActivity extends Activity{

    private ListView listView;
    private ArrayAdapter adapter;
    private ArrayList list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
         listView = (ListView) findViewById( R.id.listview); 
         list = new ArrayList();
          adapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, list);

            listView.setAdapter(adapter);
            new LoadImages(list, adapter).execute();

    }
}

为什么会崩溃?

04-20 12:52:38.089: I/global(400): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
04-20 12:52:38.539: D/dalvikvm(400): GC_FOR_MALLOC freed 1781 objects / 98016 bytes in 115ms
04-20 12:52:38.649: D/dalvikvm(400): GC_FOR_MALLOC freed 252 objects / 143016 bytes in 95ms
04-20 12:52:38.820: D/dalvikvm(400): GC_FOR_MALLOC freed 90 objects / 434944 bytes in 156ms
04-20 12:52:38.939: D/dalvikvm(400): GC_FOR_MALLOC freed 8 objects / 194848 bytes in 95ms
04-20 12:52:38.949: I/dalvikvm-heap(400): Grow heap (frag case) to 3.288MB for 389136-byte allocation
04-20 12:52:39.119: D/dalvikvm(400): GC_FOR_MALLOC freed 0 objects / 0 bytes in 166ms
04-20 12:52:39.269: D/dalvikvm(400): GC_FOR_MALLOC freed 44 objects / 390808 bytes in 146ms
04-20 12:52:39.659: D/dalvikvm(400): GC_FOR_MALLOC freed 1668 objects / 521136 bytes in 94ms
04-20 12:52:40.399: D/dalvikvm(400): GC_FOR_MALLOC freed 2484 objects / 134640 bytes in 90ms
04-20 12:52:40.699: D/dalvikvm(400): GC_FOR_MALLOC freed 4499 objects / 172528 bytes in 101ms
04-20 12:52:40.699: I/dalvikvm-heap(400): Grow heap (frag case) to 3.682MB for 287718-byte allocation
04-20 12:52:40.880: D/dalvikvm(400): GC_FOR_MALLOC freed 0 objects / 0 bytes in 176ms
04-20 12:52:41.559: D/dalvikvm(400): GC_FOR_MALLOC freed 6721 objects / 326776 bytes in 109ms
04-20 12:52:42.470: D/dalvikvm(400): GC_FOR_MALLOC freed 9512 objects / 399904 bytes in 122ms
04-20 12:52:43.219: D/dalvikvm(400): GC_FOR_MALLOC freed 9557 objects / 402640 bytes in 115ms
04-20 12:52:44.039: D/dalvikvm(400): GC_FOR_MALLOC freed 10870 objects / 452144 bytes in 138ms
04-20 12:52:44.902: D/dalvikvm(400): GC_FOR_MALLOC freed 11333 objects / 486376 bytes in 136ms
04-20 12:52:45.791: D/dalvikvm(400): GC_FOR_MALLOC freed 12106 objects / 505752 bytes in 148ms
04-20 12:52:45.958: D/AndroidRuntime(400): Shutting down VM
04-20 12:52:45.958: W/dalvikvm(400): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-20 12:52:46.061: E/AndroidRuntime(400): FATAL EXCEPTION: main
04-20 12:52:46.061: E/AndroidRuntime(400): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mongol/com.example.mongol.MainActivity}: java.lang.ClassCastException: org.jsoup.nodes.Document
04-20 12:52:46.061: E/AndroidRuntime(400):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-20 12:52:46.061: E/AndroidRuntime(400):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-20 12:52:46.061: E/AndroidRuntime(400):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-20 12:52:46.061: E/AndroidRuntime(400):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-20 12:52:46.061: E/AndroidRuntime(400):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 12:52:46.061: E/AndroidRuntime(400):  at android.os.Looper.loop(Looper.java:123)
04-20 12:52:46.061: E/AndroidRuntime(400):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-20 12:52:46.061: E/AndroidRuntime(400):  at java.lang.reflect.Method.invokeNative(Native Method)
04-20 12:52:46.061: E/AndroidRuntime(400):  at java.lang.reflect.Method.invoke(Method.java:521)
04-20 12:52:46.061: E/AndroidRuntime(400):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-20 12:52:46.061: E/AndroidRuntime(400):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-20 12:52:46.061: E/AndroidRuntime(400):  at dalvik.system.NativeStart.main(Native Method)
04-20 12:52:46.061: E/AndroidRuntime(400): Caused by: java.lang.ClassCastException: org.jsoup.nodes.Document
04-20 12:52:46.061: E/AndroidRuntime(400):  at com.example.mongol.MainActivity.onCreate(MainActivity.java:56)
04-20 12:52:46.061: E/AndroidRuntime(400):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-20 12:52:46.061: E/AndroidRuntime(400):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-20 12:52:46.061: E/AndroidRuntime(400):  ... 11 more
04-20 12:52:49.409: I/Process(400): Sending signal. PID: 400 SIG: 9
04-20 15:39:54.631: W/KeyCharacterMap(429): No keyboard for id 0
04-20 15:39:54.710: W/KeyCharacterMap(429): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
04-20 15:40:05.249: D/dalvikvm(429): GC_EXPLICIT freed 994 objects / 71000 bytes in 128ms
04-20 15:49:13.209: I/global(458): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
04-20 15:49:13.439: D/dalvikvm(458): GC_FOR_MALLOC freed 1802 objects / 101888 bytes in 103ms
04-20 15:49:13.560: D/dalvikvm(458): GC_FOR_MALLOC freed 263 objects / 143728 bytes in 110ms
04-20 15:49:13.669: D/dalvikvm(458): GC_FOR_MALLOC freed 122 objects / 436400 bytes in 100ms
04-20 15:49:13.769: D/dalvikvm(458): GC_FOR_MALLOC freed 3 objects / 194656 bytes in 93ms
04-20 15:49:13.779: I/dalvikvm-heap(458): Grow heap (frag case) to 3.285MB for 389136-byte allocation
04-20 15:49:13.899: D/dalvikvm(458): GC_FOR_MALLOC freed 0 objects / 0 bytes in 116ms
04-20 15:49:14.009: D/dalvikvm(458): GC_FOR_MALLOC freed 44 objects / 390808 bytes in 100ms
04-20 15:49:14.739: D/dalvikvm(458): GC_FOR_MALLOC freed 1683 objects / 528096 bytes in 344ms
04-20 15:49:15.899: D/dalvikvm(458): GC_FOR_MALLOC freed 2471 objects / 125872 bytes in 136ms
04-20 15:49:16.298: D/dalvikvm(458): GC_FOR_MALLOC freed 4499 objects / 172424 bytes in 115ms
04-20 15:49:16.298: I/dalvikvm-heap(458): Grow heap (frag case) to 3.680MB for 287718-byte allocation
04-20 15:49:16.418: D/dalvikvm(458): GC_FOR_MALLOC freed 0 objects / 0 bytes in 112ms
04-20 15:49:17.469: D/dalvikvm(458): GC_FOR_MALLOC freed 6831 objects / 326392 bytes in 108ms
04-20 15:49:18.499: D/dalvikvm(458): GC_FOR_MALLOC freed 9478 objects / 399328 bytes in 122ms
04-20 15:49:19.869: D/dalvikvm(458): GC_FOR_MALLOC freed 9571 objects / 402800 bytes in 125ms
04-20 15:49:21.090: D/dalvikvm(458): GC_FOR_MALLOC freed 10892 objects / 452648 bytes in 251ms
04-20 15:49:22.429: D/dalvikvm(458): GC_FOR_MALLOC freed 11307 objects / 484816 bytes in 184ms
04-20 15:49:23.689: D/dalvikvm(458): GC_FOR_MALLOC freed 13250 objects / 550520 bytes in 244ms
04-20 15:49:23.849: W/dalvikvm(458): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
04-20 15:49:23.979: E/AndroidRuntime(458): FATAL EXCEPTION: AsyncTask #1
04-20 15:49:23.979: E/AndroidRuntime(458): java.lang.RuntimeException: An error occured while executing doInBackground()
04-20 15:49:23.979: E/AndroidRuntime(458):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-20 15:49:23.979: E/AndroidRuntime(458):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-20 15:49:23.979: E/AndroidRuntime(458):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-20 15:49:23.979: E/AndroidRuntime(458):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-20 15:49:23.979: E/AndroidRuntime(458):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-20 15:49:23.979: E/AndroidRuntime(458):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-20 15:49:23.979: E/AndroidRuntime(458):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-20 15:49:23.979: E/AndroidRuntime(458):  at java.lang.Thread.run(Thread.java:1096)
04-20 15:49:23.979: E/AndroidRuntime(458): Caused by: java.lang.ClassCastException: org.jsoup.nodes.Document
04-20 15:49:23.979: E/AndroidRuntime(458):  at com.example.mongol.LoadImages.doInBackground(LoadImages.java:42)
04-20 15:49:23.979: E/AndroidRuntime(458):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-20 15:49:23.979: E/AndroidRuntime(458):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-20 15:49:23.979: E/AndroidRuntime(458):  ... 4 more
04-20 15:49:37.699: I/Process(458): Sending signal. PID: 458 SIG: 9
04-20 15:49:52.489: I/global(468): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
04-20 15:49:53.269: D/dalvikvm(468): GC_FOR_MALLOC freed 1829 objects / 102928 bytes in 115ms
04-20 15:49:53.371: D/dalvikvm(468): GC_FOR_MALLOC freed 268 objects / 143944 bytes in 84ms
04-20 15:49:53.469: D/dalvikvm(468): GC_FOR_MALLOC freed 122 objects / 436400 bytes in 94ms
04-20 15:49:53.562: D/dalvikvm(468): GC_FOR_MALLOC freed 3 objects / 194656 bytes in 82ms
04-20 15:49:53.571: I/dalvikvm-heap(468): Grow heap (frag case) to 3.286MB for 389136-byte allocation
04-20 15:49:53.749: D/dalvikvm(468): GC_FOR_MALLOC freed 0 objects / 0 bytes in 176ms
04-20 15:49:53.849: D/dalvikvm(468): GC_FOR_MALLOC freed 49 objects / 391000 bytes in 83ms
04-20 15:49:54.190: D/dalvikvm(468): GC_FOR_MALLOC freed 1680 objects / 528016 bytes in 84ms
04-20 15:49:54.888: D/dalvikvm(468): GC_FOR_MALLOC freed 2471 objects / 125776 bytes in 109ms
04-20 15:49:55.170: D/dalvikvm(468): GC_FOR_MALLOC freed 4499 objects / 172400 bytes in 106ms
04-20 15:49:55.170: I/dalvikvm-heap(468): Grow heap (frag case) to 3.680MB for 287718-byte allocation
04-20 15:49:55.319: D/dalvikvm(468): GC_FOR_MALLOC freed 0 objects / 0 bytes in 149ms
04-20 15:49:55.958: D/dalvikvm(468): GC_FOR_MALLOC freed 6830 objects / 326368 bytes in 126ms
04-20 15:49:56.620: D/dalvikvm(468): GC_FOR_MALLOC freed 9475 objects / 398952 bytes in 116ms
04-20 15:49:57.329: D/dalvikvm(468): GC_FOR_MALLOC freed 9550 objects / 402112 bytes in 139ms
04-20 15:49:58.140: D/dalvikvm(468): GC_FOR_MALLOC freed 10895 objects / 452624 bytes in 139ms
04-20 15:49:58.959: D/dalvikvm(468): GC_FOR_MALLOC freed 11316 objects / 485416 bytes in 151ms
04-20 15:49:59.989: D/dalvikvm(468): GC_FOR_MALLOC freed 12105 objects / 505736 bytes in 281ms
04-20 15:50:00.270: W/dalvikvm(468): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
04-20 15:50:00.279: E/AndroidRuntime(468): FATAL EXCEPTION: AsyncTask #1
04-20 15:50:00.279: E/AndroidRuntime(468): java.lang.RuntimeException: An error occured while executing doInBackground()
04-20 15:50:00.279: E/AndroidRuntime(468):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-20 15:50:00.279: E/AndroidRuntime(468):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-20 15:50:00.279: E/AndroidRuntime(468):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-20 15:50:00.279: E/AndroidRuntime(468):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-20 15:50:00.279: E/AndroidRuntime(468):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-20 15:50:00.279: E/AndroidRuntime(468):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-20 15:50:00.279: E/AndroidRuntime(468):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-20 15:50:00.279: E/AndroidRuntime(468):  at java.lang.Thread.run(Thread.java:1096)
04-20 15:50:00.279: E/AndroidRuntime(468): Caused by: java.lang.ClassCastException: org.jsoup.nodes.Document
04-20 15:50:00.279: E/AndroidRuntime(468):  at com.example.mongol.LoadImages.doInBackground(LoadImages.java:42)
04-20 15:50:00.279: E/AndroidRuntime(468):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-20 15:50:00.279: E/AndroidRuntime(468):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-20 15:50:00.279: E/AndroidRuntime(468):  ... 4 more
04-20 15:50:04.700: I/Process(468): Sending signal. PID: 468 SIG: 9
04-20 15:52:50.449: I/global(499): Default buffer size used in BufferedInputStream constructor. It would be better to be explicit if an 8k buffer is required.
04-20 15:52:50.959: D/dalvikvm(499): GC_FOR_MALLOC freed 1845 objects / 103584 bytes in 98ms
04-20 15:52:51.049: D/dalvikvm(499): GC_FOR_MALLOC freed 273 objects / 144160 bytes in 88ms
04-20 15:52:51.159: D/dalvikvm(499): GC_FOR_MALLOC freed 122 objects / 436400 bytes in 84ms
04-20 15:52:51.249: D/dalvikvm(499): GC_FOR_MALLOC freed 3 objects / 194656 bytes in 87ms
04-20 15:52:51.259: I/dalvikvm-heap(499): Grow heap (frag case) to 3.286MB for 389136-byte allocation
04-20 15:52:51.441: D/dalvikvm(499): GC_FOR_MALLOC freed 0 objects / 0 bytes in 177ms
04-20 15:52:51.549: D/dalvikvm(499): GC_FOR_MALLOC freed 49 objects / 391000 bytes in 91ms
04-20 15:52:51.909: D/dalvikvm(499): GC_FOR_MALLOC freed 1680 objects / 528008 bytes in 87ms
04-20 15:52:52.599: D/dalvikvm(499): GC_FOR_MALLOC freed 2471 objects / 125840 bytes in 90ms
04-20 15:52:52.889: D/dalvikvm(499): GC_FOR_MALLOC freed 4499 objects / 172424 bytes in 117ms
04-20 15:52:52.889: I/dalvikvm-heap(499): Grow heap (frag case) to 3.680MB for 287718-byte allocation
04-20 15:52:52.989: D/dalvikvm(499): GC_FOR_MALLOC freed 0 objects / 0 bytes in 94ms
04-20 15:52:53.639: D/dalvikvm(499): GC_FOR_MALLOC freed 6828 objects / 326288 bytes in 125ms
04-20 15:52:54.419: D/dalvikvm(499): GC_FOR_MALLOC freed 9477 objects / 398960 bytes in 103ms
04-20 15:52:55.079: D/dalvikvm(499): GC_FOR_MALLOC freed 9569 objects / 403072 bytes in 109ms
04-20 15:52:55.911: D/dalvikvm(499): GC_FOR_MALLOC freed 10890 objects / 452384 bytes in 135ms
04-20 15:52:56.719: D/dalvikvm(499): GC_FOR_MALLOC freed 11307 objects / 485008 bytes in 127ms
04-20 15:52:57.619: D/dalvikvm(499): GC_FOR_MALLOC freed 12099 objects / 505496 bytes in 165ms
04-20 15:52:57.789: W/dalvikvm(499): threadid=7: thread exiting with uncaught exception (group=0x4001d800)
04-20 15:52:57.799: E/AndroidRuntime(499): FATAL EXCEPTION: AsyncTask #1
04-20 15:52:57.799: E/AndroidRuntime(499): java.lang.RuntimeException: An error occured while executing doInBackground()
04-20 15:52:57.799: E/AndroidRuntime(499):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-20 15:52:57.799: E/AndroidRuntime(499):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
04-20 15:52:57.799: E/AndroidRuntime(499):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
04-20 15:52:57.799: E/AndroidRuntime(499):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
04-20 15:52:57.799: E/AndroidRuntime(499):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-20 15:52:57.799: E/AndroidRuntime(499):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-20 15:52:57.799: E/AndroidRuntime(499):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-20 15:52:57.799: E/AndroidRuntime(499):  at java.lang.Thread.run(Thread.java:1096)
04-20 15:52:57.799: E/AndroidRuntime(499): Caused by: java.lang.ClassCastException: org.jsoup.nodes.Document
04-20 15:52:57.799: E/AndroidRuntime(499):  at com.example.mongol.LoadImages.doInBackground(LoadImages.java:42)
04-20 15:52:57.799: E/AndroidRuntime(499):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-20 15:52:57.799: E/AndroidRuntime(499):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-20 15:52:57.799: E/AndroidRuntime(499):  ... 4 more
04-20 15:53:07.380: I/Process(499): Sending signal. PID: 499 SIG: 9

1 个答案:

答案 0 :(得分:0)

您从 doInBackground()返回 null 。将代码更改为此,它将起作用:

Document doc = null;

    try {


    doc = (Document) Jsoup.connect("http://mongol.co.il/").get();
    Elements divs =((Elements) doc).select("img[src$=.jpg]");



     } catch (IOException e1) {

    e1.printStackTrace(); }
    return divs;