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