Java – how to display ProgressDialog when using salvo for network operations

I work on an Android application and I am very interested in using the volley library to perform network HTTP calls

But my problem is that I found that this library operates in different background threads. Then how can I show ProgressDialog when the HTTP request starts to execute and fire it once it is 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+"]");

    }
});

Thank you in advance

Solution

This is very direct After adding the request object to the queue, start the progress dialog box

//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 close the dialog box after receiving the response from the server

StringRequest postReq = new StringRequest(Request.Method.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();
    }
});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>