我如何等待OnPostExecute在Android中完成?

时间:2014-05-19 15:22:03

标签: android android-asynctask

我尝试执行AsyncTask但是当AsyncTask开始并且doInBackground完成(返回值)时,它正在跳过OnPostExecute并运行代码{下面的{1}},在我更改requestTask2.execute()中的值之前,它正在尝试运行OnPostExecute,因此我获得了空。

让我解释一下代码:

if condition

如何在 public void onClick(DialogInterface dialog,int id) { Intent gt = new Intent(MainActivity.this, favorite.class); String password = userInput.getText().toString(); String kadi = userInput2.getText().toString(); RequestTask2 requestTask2 = new RequestTask2(); requestTask2.execute("http://www.example.com/androfav/?fav2="+kadi+":"+password).get(); if (asd2[0][0]!=null && asd2[1][0]!=null ) { // This if condition works before on Post Excecute and it is causing the problem. if (asd2[0][0].equals(password) && asd2[1][0].endsWith(kadi) ) { // Codes }} class RequestTask2 extends AsyncTask<String, String, String> { private ProgressDialog dialog = new ProgressDialog(MainActivity.this); @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); dialog.setMessage("Diziler Yükleniyor \n Lütfen Bekleyin..."); dialog.show(); dialog.setCancelable(false); } @Override protected String doInBackground(String... uri2) { HttpClient httpclient2 = new DefaultHttpClient(); HttpResponse response2; String responseString2 = null; try { response2 = httpclient2.execute(new HttpGet(uri2[0])); StatusLine statusLine2 = response2.getStatusLine(); if (statusLine2.getStatusCode() == HttpStatus.SC_OK) { ByteArrayOutputStream out = new ByteArrayOutputStream(); response2.getEntity().writeTo(out); out.close(); responseString2 = out.toString(); } else { // Closes the connection. response2.getEntity().getContent().close(); throw new IOException(statusLine2.getReasonPhrase()); } } catch (ClientProtocolException e) { // TODO Handle problems.. } catch (IOException e) { // TODO Handle problems.. } return responseString2; } @Override protected void onPostExecute(String result2) { super.onPostExecute(result2); try { JSONArray jsonResponse2 = new JSONArray(result2); asd2 = new String[3][jsonResponse2.length()]; //............................... Codes dialog.dismiss(); } } 条件生效之前等待OnPostExecute。 希望我能理解自己。 提前谢谢。

2 个答案:

答案 0 :(得分:1)

AsyncTask顾名思义就是Asynchronous。您需要将if条件移至onPostExecute

将以下内容移至onPostExecute

JSONArray jsonResponse2 = new JSONArray(result2);
asd2 = new String[3][jsonResponse2.length()];
if (asd2[0][0]!=null && asd2[1][0]!=null ) {


if (asd2[0][0].equals(password) && asd2[1][0].endsWith(kadi) ) { 
// Codes
}
}

编辑:

我没有注意到你打电话给get()。调用get()使Asynctask不再异步。你永远不应该致电get() execute()就足够了。

为什么需要调用阻止ui线程等待任务完成的get()

答案 1 :(得分:1)

使用get()时,您应始终避免致电AsyncTask。相反,请在onPostExecute

中进行所有后期处理
@Override
protected void onPostExecute(String result2) {
    super.onPostExecute(result2);
    try {
        JSONArray jsonResponse2 = new JSONArray(result2);
        asd2 = new String[3][jsonResponse2.length()];
        if (asd2[0][0]!=null && asd2[1][0]!=null ) {
            if (asd2[0][0].equals(password) && asd2[1][0].endsWith(kadi) ) { 
                // Codes
            }
        }
    }

    dialog.dismiss();
}