我正在尝试执行AsyncTask,但是当AsyncTask开始并且doInBackground完成(返回值)时,它会跳过OnPostExecute并运行下面的代码requestTask2.execute(),然后再更改OnPostExecute中的值,它正在尝试运行if condition,所以我得到了null。

让我用代码解释一下:

  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条件工作之前,我如何等待if
希望我能理解我自己。
提前致谢。

最佳答案

顾名思义,AsyncTask是异步的。您需要将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线程等待任务完成。

关于android - 我如何等待OnPostExecute在Android中完成?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23741510/

10-12 00:30