如何在asynctask onPostExecute方法上显示加载动画

问题描述 投票:8回答:2

我需要在我的android项目上显示一个加载动画,等待响应即将到来。我试图显示加载消息。响应来之后我需要隐藏起来。

private void searchCustomer(final String username, final String 
                             password, final String baseUrl, final ProgressDialog dialog) {

    AsyncTask<String, String, String> checkLogin = new AsyncTask<String, String, String>() {

    final ProgressDialog dialog = new ProgressDialog(MainActivity.this);

    @Override
    protected String doInBackground(String... strings) {

        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, "{\n\t\"custUserName\": \""+ username +"\",\n\t\"custPassword\": \""+ password +"\"\n}");
        Request request = new Request.Builder()
                .url(baseUrl + "customerLogin")
                .post(body)
                .addHeader("Content-Type", "application/json")
                .build();

        try {
            Response response = okHttpClient.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }

    @Override
    protected void onPostExecute(String s) {

        dialog.setMessage("Processing...");
        dialog.show();

        StringBuilder sb = new StringBuilder();

        char[] temp = s.toCharArray();
         for (char current : temp) {
         if (current != '"') {
             sb.append(current);
            }
        }

        String s1 = sb.toString();

        if (s1.equals("true")) {
        Notification.showSuccessMdToast("Login Successful", getApplicationContext());

        Intent customerNav = new Intent(MainActivity.this, CustomerNav.class);
        startActivity(customerNav);

        }

    }
};

checkLogin.execute();

}

在post中执行响应值是真的。但处理对话框不是隐藏的。

android android-asynctask
2个回答
3
投票

onPostExecute工作完成时,doInBackground回调。

你必须在dialog.show();中调用onPreExecute()并将其隐藏在onPostExecute()上。见下文:

AsyncTask<String, String, String> checkLogin = new AsyncTask<String, String, String>() {

final ProgressDialog dialog = new ProgressDialog(MainActivity.this);

 @Override
 protected void onPreExecute() {
     super.onPreExecute();
     dialog.setMessage("Processing...");
     dialog.show();
 }

@Override
protected String doInBackground(String... strings) {

    MediaType mediaType = MediaType.parse("application/json");
    RequestBody body = RequestBody.create(mediaType, "{\n\t\"custUserName\": \""+ username +"\",\n\t\"custPassword\": \""+ password +"\"\n}");
    Request request = new Request.Builder()
            .url(baseUrl + "customerLogin")
            .post(body)
            .addHeader("Content-Type", "application/json")
            .build();

    try {
        Response response = okHttpClient.newCall(request).execute();
        return response.body().string();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "";
}

@Override
protected void onPostExecute(String s) {

    dialog.dismis();

    StringBuilder sb = new StringBuilder();

    char[] temp = s.toCharArray();
     for (char current : temp) {
     if (current != '"') {
         sb.append(current);
        }
    }

    String s1 = sb.toString();

    if (s1.equals("true")) {
    Notification.showSuccessMdToast("Login Successful", getApplicationContext());

    Intent customerNav = new Intent(MainActivity.this, CustomerNav.class);
    startActivity(customerNav);

    }

}
};

checkLogin.execute();

2
投票

您需要隐藏if语句中的对话框,如下所示:

if (s1.equals("true") {
    dialog.dimiss();
}

附加提示:您可以在onPreExecute()中显示progressDialog,而不是在onPostExecute()中显示

© www.soinside.com 2019 - 2024. All rights reserved.