将照片从网站上传到Android应用程序

时间:2013-04-20 15:05:09

标签: java android listview android-asynctask jsoup

我正在尝试使用AsyncTask,ListView和Jsoup从我的Android应用程序中检索网站上的照片。在两个类之间遇到一些困难(一个扩展Activity而另一个扩展AsyncTask)这是我的主要活动:

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();

}

这是AsyncTask类:

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;



}

}

2 个答案:

答案 0 :(得分:0)

尝试在适配器的imageviewHolder上执行此操作:

Connect.loadBitmap( 0, "http://www.saudeanimal.com.br/imagens/suricate06.jpg",
                new BitmapLoadListener() {
                    @Override
                    public void onLoadListener( int id, Bitmap bitmap ) {
                        if( bitmap != null ) {
                            tableBean.setImg( bitmap );
                            notifyDataSetChanged();
                        }
                    }
                } );

答案 1 :(得分:0)

你的MainActivity.java应该是

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();
    }
}

你的AsyncTask课程应该是

public class LoadImages extends AsyncTask
{
    private ArrayAdapter adapter;
    private ArrayList list;

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

    @Override
    protected void onPostExecute(Elements divs)
    {
        super.onPostExecute(result);
        for ( org.jsoup.nodes.Element div : divs )
        {
            list.add(div);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    protected Void doInBackground(Void... params)
    {
        Document doc = null;
        try
        {
            doc = (Document) Jsoup.connect("http://mongol.co.il/").get();
            return  ((Elements) doc).select("img[src$=.jpg]");
        }
        catch (IOException e1)
        {
            e1.printStackTrace();
        }
        return null;
    }
}

注意:return ((Elements) doc).select("img[src$=.jpg]");中有ClassCastExcception。解决方案只是从后台线程更新列表视图而不是使用logcat错误编辑的问题(请参阅下面的注释以获取说明)。