AsyncTask doInBackground不会完成/挂起



当我在doInBackground方法中调用lib.roomAvailable()时,我有一个AsyncTask不会完成。当调用QueueTask时,progressDialog将显示和它棒。5秒后会消失。

如果我从doInBackground方法中删除lib.roomAvailable(),一切都按预期工作。当我调用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;
      }

另一个AsyncTask:

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 !

您正在尝试从UI线程以外的线程创建并启动AsyncTask。

http://developer.android.com/reference/android/os/AsyncTask.html

参见"线程规则"章节

相关内容

  • 没有找到相关文章

最新更新