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

时间:2019-04-28 04:16:20

标签: android android-asynctask

我需要在我的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();

}

执行后的

值为真实值。但是“处理对话框”不是隐藏的。

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:2)

您需要像这样将对话框隐藏在if语句中:

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

其他提示:您可以在onPreExecute()中显示您的progressDialog,而不是在onPostExecute()中显示它