AsyncTask doInBackground无法完成/挂起

时间:2013-01-12 12:41:52

标签: android android-asynctask

当我在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!

1 个答案:

答案 0 :(得分:2)

您正在尝试从UI线程以外的线程创建并启动AsyncTask。这不受支持:

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

请参阅标题为“线程规则”的部分