-1

我想从 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 扩展类代码:

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

    }

}
4

1 回答 1

1

不,你只需要创建界面

TaskCompleteListner.java

public Interface TaskCompleteListner{

 void onTaskComplete(String result);
}

你的活动.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();

    }

}
于 2013-11-03T10:39:13.783 回答