Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get volley request tag in response

I'm using volley and I have a queue to call some APIs. The queue is filled from a database.

before adding request to volley request queue I set request tag by calling

jsonObjectRequest.setTag(id);

In response, I want to remove a column from the database that column id is equal to request tag id.

So, How can I get request tag in HttpRequest response?

like image 882
FarshidABZ Avatar asked Nov 07 '17 12:11

FarshidABZ


People also ask

How to make get request using Volley in Android?

Use newRequestQueue RequestQueue queue = Volley. newRequestQueue(this); String url = "https://www.google.com"; // Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request. Method.

How do I make my volley request synchronous?

A volley class used for blocking requests. To access the get() method and make an Android Volley synchronous request, you need to use the RequestFuture class instead of standard StringRequest or JsonObjectRequest class. This RequestFuture class also implements both Response. Listener and Response.

What is volly in Android?

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub. Volley offers the following benefits: Automatic scheduling of network requests. Multiple concurrent network connections.


1 Answers

First create a Listener that give response from your volly class

/** Callback interface for delivering parsed responses. */
public interface Listener {
    /** Called when a response is received. */
    public void onResponse(Object tag, JSONObject response);
    public void onErrorResponse(Object tag, VolleyError error);
}

And now create method as below where you pass listener and tag and call volly request. in response you able get tag and response at same time.

public void callApi(String url, final Listener listener, final Object tag){
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
            url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    listener.onResponse(tag,response);
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            listener.onErrorResponse(tag,error);
        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(jsonObjReq);
}

Its just sample code, You can modify on your requirement. If you need any help comment.

like image 159
Lokesh Avatar answered Oct 16 '22 17:10

Lokesh