Issue
i need to show an loading animation on my android project for wait the response is coming. i try to show an loading message. i need to hide after the response is come.
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();
}
in post Execute response value is come as true. but Processing Dialog is not hide.
Solution
the onPostExecute
callback called when doInBackground
work finished.
You must call dialog.show();
inside onPreExecute()
and hide this on onPostExecute()
. see below:
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();
Answered By - SamiAzar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.