【发布时间】:2015-09-03 09:47:51
【问题描述】:
所以我的问题是我有一个 AsyncTask 从服务器上的页面中删除 html,所以我使用 Jsoup 作为库。 所以问题是如果我没有从页面接收到任何数据并显示吐司上存在“通信错误”,我想设置一个超时来取消任务
是否有办法杀死或停止其中的异步任务并在 onPostExecute 上返回结果
{
private class getPageTitle extends AsyncTask<Void, Void, String> {
String title;
@Override
protected void onPreExecute() {
super.onPreExecute();
connectServerProgressDialog = new ProgressDialog(LoginScreen.this);
connectServerProgressDialog.setTitle("CheckingServer");
connectServerProgressDialog.setMessage("Loading...");
connectServerProgressDialog.setIndeterminate(true);
connectServerProgressDialog.show();
}
@Override
protected String doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(CONNECT_URL).get();
title = document.title();
} catch (IOException e) {
e.printStackTrace();
}
return null ;
}
@Override
protected void onPostExecute(String result) {
if(result!=null){
switch (title) {
case "0":
Toast.makeText(LoginScreen.this,"offline",Toast.LENGTH_SHORT).show();
connectServerProgressDialog.dismiss();
break;
case "1":
connectServerProgressDialog.dismiss();
Toast.makeText(LoginScreen.this,"Connected",Toast.LENGTH_SHORT).show();
break;
}}else{
Toast.makeText(LoginScreen.this,"Communication error",Toast.LENGTH_SHORT).show();
}
}
}}
【问题讨论】:
-
我从来没有用过soup但是阅读文档你可以指定一个连接超时参数。jsoup.org/apidocs/org/jsoup/Connection.html
标签: java android android-asynctask timeout jsoup