ProcessDialog not showing correctly?

This is my function, which is located in LoginActivity.java. So on the button I call this function.

public void postHttpRequest(String userId,String pass,TextView error){ RequestClient reqClient = new RequestClient(LoginActivity.this); String AppResponse = null; try { url = "myurl"; Log.d("URL", url); AppResponse = reqClient.execute().get(); String status = ValidateLoginStatus.checkLoginStatus(AppResponse); Log.d("Status recived", status); if(status.equals("200")){ saveInformation(userId,pass); startingActivity(HOST_URL); }else{ error.setText("Incorrect UserName or Password"); } } catch (Exception e) { Log.e("Exception Occured", "Exception is "+e.getMessage()); } } 

From this function, I call AsynkTask for the Http Communication.So onclick button, when I submit a response, then my Dialog process opens for only one second. I want me to press the buttoon button. My Dialog process must be open so that I get a response

 public class RequestClient extends AsyncTask<String, Void, String>{ ProgressDialog pDialog; Context context; public RequestClient(Context c) { context = c; } @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(context); pDialog.setMessage("Authenticating user..."); pDialog.show(); } @Override protected String doInBackground(String... aurl){ String responseString=""; DefaultHttpClient httpClient=new DefaultHttpClient(); try { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(LoginActivity.url); HttpResponse responseGet = client.execute(get); HttpEntity resEntityGet = responseGet.getEntity(); if (resEntityGet != null) { responseString = EntityUtils.toString(resEntityGet); Log.i("GET RESPONSE", responseString); } } catch (Exception e) { Log.d("ANDRO_ASYNC_ERROR", "Error is "+e.toString()); } Log.d("ANDRO_ASYNC_ERROR", responseString); httpClient.getConnectionManager().shutdown(); return responseString; } @Override protected void onPostExecute(String response) { super.onPostExecute(response); if(pDialog!=null) pDialog.dismiss(); } } 

So, please suggest me what changes I need to make so that processDialog displays correctly in the center of the device.

+1
source share
3 answers

Returns an AsyncTask value only after using the get () method

Figure from the link above

Calling the get() AsyncTask blocks the main thread and waits for the result to return. This effectively makes the use of AsyncTask synchronous, in which case it makes no sense to use AsyncTask.

The only reason I can use the get() method will be from a thread other than the main (UI) thread, although I cannot think of many reasons for this.

On click button

  RequestClient reqClient = new RequestClient(LoginActivity.this,new TheInterface() { @Override public void theMethod(String result) { Log.i("Result =",result); } }); reqClient.execute(url); // no get(). pass url to doInBackground() 

In your activity class

  public interface TheInterface { public void theMethod(String result); } } 

AsyncTask

 public class RequestClient extends AsyncTask<String, Void, String>{ ProgressDialog pDialog; Context context; TheInterface listener; public RequestClient(Context c,TheInterface listen) { context = c; listener = listen; } @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(context); pDialog.setMessage("Authenticating user..."); pDialog.show(); } @Override protected String doInBackground(String... aurl){ String responseString=""; HttpClient client; try { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(aurl[0]); // url HttpResponse responseGet = client.execute(get); HttpEntity resEntityGet = responseGet.getEntity(); if (resEntityGet != null) { responseString = EntityUtils.toString(resEntityGet); Log.i("GET RESPONSE", responseString); } } catch (Exception e) { Log.d("ANDRO_ASYNC_ERROR", "Error is "+e.toString()); } Log.d("ANDRO_ASYNC_ERROR", responseString); client.getConnectionManager().shutdown(); return responseString; } @Override protected void onPostExecute(String response) { super.onPostExecute(response); pDialog.dismiss(); if (listener != null) { listener.theMethod(result); } } } 
0
source

// add style to your progressbialog

  protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(context); pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); pDialog.setMessage("Authenticating user..."); if (pDialog != null && !pDialog.isShowing()) { pDialog.show(); } } 
+1
source

It seems your button code is wrong because it is asynchronous, but you are trying to use it as a standard synchronization code.

Try moving this code to onPostExecute:

 String status = ValidateLoginStatus.checkLoginStatus(response); Log.d("Status recived", status); if(status.equals("200")){ saveInformation(userId,pass); startingActivity(HOST_URL); }else{ error.setText("Incorrect UserName or Password"); } 

and make this button:

 public void postHttpRequest(String userId,String pass,TextView error){ RequestClient reqClient = new RequestClient(LoginActivity.this); String AppResponse = null; try { url = "myurl"; Log.d("URL", url); reqClient.execute(); } catch (Exception e) { Log.e("Exception Occured", "Exception is "+e.getMessage()); } } 
0
source

Source: https://habr.com/ru/post/949243/


All Articles