【问题标题】:How can i wait for OnPostExecute to finish in Android?我如何等待 OnPostExecute 在 Android 中完成?
【发布时间】:2014-05-19 15:22:03
【问题描述】:

我正在尝试执行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();
    }

}

if 条件起作用之前,我如何等待OnPostExecute。 希望我能理解自己。 提前致谢。

【问题讨论】:

    标签: android android-asynctask


    【解决方案1】:

    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()就足够了。

    为什么需要调用get(),它会阻塞等待任务完成的ui线程。

    【讨论】:

    • 非常感谢您的回答。我不知道我怎么没看到这个。效果很好,谢谢。
    • @user3583237 欢迎您developer.android.com/reference/android/os/AsyncTask.html#get()。我认为您误解了导致此错误的 asynctask 的使用
    • 是的,我确实误解了 AsynTask 过程,但现在我明白了。再次感谢您。
    【解决方案2】:

    在使用AsyncTask 时,您应该始终避免调用get()。相反,请在 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();
    }
    

    【讨论】:

    • 是的,你是完全正确的,我试图等待 AsyncTask,我想也许它可以工作,但没有。谢谢,你有一个很好的观点。不需要使用get()
    猜你喜欢
    • 2019-05-11
    • 2011-08-25
    • 2020-02-10
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多