How to display the ProgressDialog when do network operations using volley
It's pretty straight forward. Start the progress dialog once you add the request object in the queue.
//add the request to the queue
rq.add(request);
//initialize the progress dialog and show it
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Fetching The File....");
progressDialog.show();
Then dismiss the dialog once you have received the response from the server.
StringRequest postReq = new StringRequest(Request.Method.POST, "http://httpbin.org/post", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
tv.setText(response); // We set the response data in the TextView
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(“Volly Error”,”Error: ”+error.getLocalizedMessage());
progressDialog.dismiss();
}
});
![N Sharma](https://i.stack.imgur.com/PcDfI.png?s=256&g=1)
N Sharma
I have done masters in Advanced Software Engineering. I have worked on various technologies like Java, Android, Design patterns. My research area during my masters is revolving around the Recommendation algorithms that E-commerce websites are using in order to recommend the products to their customers on the basis of their preferences.
Updated on June 27, 2022Comments
-
N Sharma almost 2 years
I am Woking on an android application where I am very interested to use
volley
library to perform the network http calls.But my question I found that this library do operations in different background thread then How I can show
ProgressDialog
when http request start to execute then later dismiss it once it has executed.RequestQueue rq = Volley.newRequestQueue(this); StringRequest postReq = new StringRequest(Request.Method.POST, "http://httpbin.org/post", new Response.Listener<String>() { @Override public void onResponse(String response) { tv.setText(response); // We set the response data in the TextView } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { System.out.println("Error ["+error+"]"); } });
Thanks in advance.