我如何等待OnPostExecute在Android中完成? [英] How can i wait for OnPostExecute to finish in Android?

查看:91
本文介绍了我如何等待OnPostExecute在Android中完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to execute AsyncTask but when AsyncTask start and doInBackground finish (value returned), it is skipping the OnPostExecute and running the code requestTask2.execute() below, before i change the value in OnPostExecute, it is trying to run if condition so i'm getting 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();
    }

}

如何在if条件工作之前等待OnPostExecute. 希望我能理解我自己. 预先感谢.

How can i wait for OnPostExecute before the if condition works. Hope i could understand myself. Thanks in advance.

推荐答案

AsyncTask顾名思义是异步的.您需要将if条件移到onPostExecute.

AsyncTask as the name suggests is Asynchronous. You need to move the if condition to 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()就叫get().

I din't notice you called get(). Calling get() makes Asynctask no more asynchronous. You should never call get() just execute() is enough.

为什么需要调用get(),该代码阻止ui线程等待任务完成.

Why do you need to call get() which blocks the ui thread waiting for the task to be finished.

这篇关于我如何等待OnPostExecute在Android中完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆