How to display the ProgressDialog when do network operations using volley
Asked Answered
M

1

5

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 showProgressDialog 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.

Merchantman answered 30/5, 2014 at 19:44 Comment(3)
You display one at request and dismiss it in onResponse or onErrorResponse.Clergyman
@Clergyman can you please give an example to me ?Merchantman
What is the problem? Just create a ProgressDialog before the code you posted. Or at the moment you put postReq in the queue. And show him. Or is there an onStart event?Clergyman
R
23

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();
    }
});
Radiography answered 11/8, 2014 at 19:42 Comment(3)
How do we handle the progress dialog if the activity is recreated during orientation changes without using configChanges in manifest?Oneidaoneil
This answer is not a complete solution. What if you change the orientation? what if you press cancel progress - where does the network operation get's cancelled?Guanine
That's a very old issue and somewhat unrelated to this question and solution. The orientation change can cause problem to any activity/view if you are not careful enough to handle that. See here for more info: androiddesignpatterns.com/2013/04/… . You can also check discussion on this issue in SO. One example is this one: https://mcmap.net/q/47618/-avoid-reloading-activity-with-asynctask-on-orientation-change-in-androidRadiography

© 2022 - 2024 — McMap. All rights reserved.