Girish Bhutiya
Girish Bhutiya

Reputation: 3127

onPreExecute method in volley

Is there any method in volley as like AsyncTask ?

I am used volley lib in my app and I want do some pre execution in volley. So is there any method in volley lib ad like below methods in AsyncTask because I want to create json with Base64 image string and when I going to create json with image my app is stop responding.

protected void onPreExecute() {
        pDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Retrieving data ...", true);
    }

    protected Void doInBackground(Void... unused) {
        items = parser.getItems();

        for (Item it : items) {
            publishProgress(it);
        }
        return(null);
    }

    protected void onProgressUpdate(Item... item) {
        adapter.add(item[0]);
    }

    protected void onPostExecute(Void unused) {
        pDialog.dismiss();
    }

Thanks

Upvotes: 1

Views: 2029

Answers (2)

MChaker
MChaker

Reputation: 2649

You can add a ProgressDialog to your layout, then change its visibility to View.GONE on response.

An example of layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    ...

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

An example of volley library use:

JsonObjectRequest jsObjRequest = new JsonObjectRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        // add your logic here
                        progressBar.setVisibility(View.GONE);
                        // set you progressBar variable using findViewById method
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub

                    }
                });

        // Access the RequestQueue through your singleton class.
        VolleySingleton.getInstance(getActivity()).addToRequestQueue(jsObjRequest);

Upvotes: 1

TheLittleNaruto
TheLittleNaruto

Reputation: 8473

Volley Library has no such method like onPreExecute.

You can initialize variables before invoking querying the request in Volley.

Here is an Simple example, the piece of code has been taken from the example included with the Volley lib :

     //Here you'll initialize variables like showing dialog etc.


    RequestQueue queue = Volley.newRequestQueue(this);
            String url = "https://ajax.googleapis.com/ajax/services/search/images?" +
                        "v=1.0&q=dog&rsz=8";

            JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    // TODO Auto-generated method stub
                    Log.d("Response", response.toString());
                    //Here you'll dismiss dialog :)
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    // TODO Auto-generated method stub

                }
            });

            queue.add(jsObjRequest);

        }

Upvotes: 0

Related Questions