lib.roomAvailable()
当我在 doInBackground 方法中调用时,我有一个 AsyncTask 无法完成。当 QueueTask 被调用时,progressDialog 将显示并坚持下去。它应该在 5 秒后消失。
如果我lib.roomAvailable()
从 doInBackground 方法中删除,一切都会按预期工作。为什么我打电话时会出现这种奇怪的行为lib.roomAvailable()
?
roomAvailable()
调用另一个 AsyncTask 这可能是一个问题吗?
public class QueueTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog progressDialog;
private Library lib;
private String date;
private int room;
private int time;
private String user;
private String id;
public QueueTask(String date, int room, int time, String user, String id) {
this.date = date;
this.room = room;
this.time = time;
this.user = user;
this.id = id;
}
@Override
protected void onPreExecute() {
lib = new Library();
progressDialog = ProgressDialog.show(ManageRoomActivity.this, "Queuing", "Queuing process", true);
}
@Override
protected Void doInBackground(Void... params) {
int timer = 5;
while(timer > 0) {
if(lib.roomAvailable(date, room, time)) {
Log.w("RoomAvailable", "Room Available");
}
android.os.SystemClock.sleep(1000);
timer --;
Log.w("TIMER", "" + timer);
}
progressDialog.dismiss();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
Intent intent = new Intent(ManageRoomActivity.this, DatePickerActivity.class);
ManageRoomActivity.this.startActivity(intent);
}
}
编辑1:
public boolean roomAvailable(String date, int room, int time) {
boolean available = false;
String timeString = "";
if(time < 10)
timeString = "0";
timeString = timeString + time;
String roomNum = "";
if(room < 10)
roomNum = "0";
roomNum = roomNum + room;
AsyncTask<String, Void, String> readSource = new URL_Source();
String source = "";
try {
String uri = getURI(date);
source = readSource.execute(getURI(date)).get();
String[] chunks = source.split("<td");
for(String chunk:chunks) {
if(chunk.contains(date) & chunk.contains("=Grp" + roomNum) & chunk.contains("stid=" + timeString))
available = true;
}
return available;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return available;
}
另一个异步任务:
public final class URL_Source extends AsyncTask<String, Void, String> {
public URL_Source() {
}
protected String doInBackground(String... uri) {
try {
URL url = new URL(uri[0]);
InputStream in = url.openStream();
StringBuilder source = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while((line = reader.readLine()) != null) {
source.append(line);
}
in.close();
return source.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "ERROR";
}
}
解决方案:
我不知道 AsyncTask 是如何工作的。您不能在 AsyncTask 中调用 AsynchTask!