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

Android 如何在asynctask onPostExecute方法上显示加载动画,android,android-asynctask,Android,Android Asynctask,我需要在我的android项目上显示一个正在加载的动画,等待响应的到来。我尝试显示正在加载的消息。收到回复后我需要躲起来 private void searchCustomer(final String username, final String password, final String baseUrl, final ProgressDialog dialog) { AsyncTask<String, String

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


在执行后响应中,值为true。但是处理对话框不是隐藏的。

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

if (s1.equals("true") {
    dialog.dimiss();
}
附加提示:您可以在onPreExecute中显示progressDialog,而不是在onPostExecute中显示它,在后台工作完成时调用onPostExecute回调

您必须调用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();

对在邮局。我将用完整代码更新问题更新代码。dialog.dismis;>>>dialog.disclose;//小字体