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

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

    }

}
于 2013-11-03T10:39:13.783 に答える