Android - Jsoup:如何从Jsoup.connect(“url”)获取结果。来自AsyncTask的get()

时间:2013-11-03 10:23:15

标签: android android-asynctask jsoup

我想从AsyncTask获取返回Jsoup.connect(“url”)。get()的DOCUMENT,我可以返回它吗? 我只想创建一个类来帮助我从某个URL获取数据))

活动类:

        @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.listView1);

        try {
            doc = new GetDataFromUrl(this).execute(functions_list).get();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




    }

和AsyncTask扩展类代码:

package com.example.PACKAGENAME;

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

public class GetDataFromUrl extends AsyncTask<String, Void, Document> {

    Context mContext;
    ProgressDialog pdialog;

    public GetDataFromUrl(Context context) {
        mContext = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pdialog = new ProgressDialog(mContext);
        pdialog.setTitle(R.string.loading);
        pdialog.setCancelable(false);
        pdialog.show();
    }

    @Override
    protected Document doInBackground(String... data) {

        try {
            Document doc = Jsoup.connect(data[0]).get();
            return doc;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Document result) {
        if (result != null) {
            super.onPostExecute(result);
            pdialog.cancel();
        }
        else
            Toast.makeText(mContext, "NULL ASYNC", Toast.LENGTH_LONG).show();

    }

}

1 个答案:

答案 0 :(得分:1)

不,你只需要创建界面

TaskCompleteListner.java

public Interface TaskCompleteListner{

 void onTaskComplete(String result);
}

YourActivity.java

public Class YourActivity extends Activity implements TaskCompleteListner
{

      @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.listView1);

        try {
            doc = new GetDataFromUrl(this).execute(functions_list).get();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




    }
  @override
  void onTaskComplete(String result)
  {

      //your result will be here in result variable
  }

}

GetDataFromUrl.java

mport java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

public class GetDataFromUrl extends AsyncTask<String, Void, Document> {

    Context mContext;
    ProgressDialog pdialog;
    TaskCompleteListner taskCompleteListener; 
    public GetDataFromUrl(Context context) {
        mContext = context;
        taskCompleteListener=(TaskCompleteListner)context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pdialog = new ProgressDialog(mContext);
        pdialog.setTitle(R.string.loading);
        pdialog.setCancelable(false);
        pdialog.show();
    }

    @Override
    protected Document doInBackground(String... data) {

        try {
            Document doc = Jsoup.connect(data[0]).get();
            return doc;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Document result) {
        pdialog.cancel();
        if (result != null) {

              taskCompleteListener.OnTaskComplete(result);
        }
        else
            Toast.makeText(mContext, "NULL ASYNC", Toast.LENGTH_LONG).show();

    }

}