0

I am familiar with Volley and creating a Singleton class, and adding requests to the queue. However, I wish to increase the modularity of volley and simply call all requests through method call to another class instead. I have setup the present action as basis for the common GET request:

public Object getRequest(String params) {

    final JSONObject getRequestReturn = new JSONObject();

    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET,
            VolleySingleton.prefixURL, ((String) null),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    // Parse the JSON:
                    try {
                        getRequestReturn = response;

                        Log.v("GET Request value", response.toString());

                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("GET Request Error", error.toString());
                }
            });

    mRequestQueue.add(getRequest);

    return getRequestReturn;
}

However, I have a the perplexing catch 22 error on the assignment of response at:

getRequestReturn = response;

The error notes that getRequestReturn must be declared final to allow for use within the inner class, but upon assigning final, another error appears noting that you cannot assign a value to a final variable.

How can this method be handled?

Sauron
  • 6,399
  • 14
  • 71
  • 136
  • declare getRequestReturn to class level – karan Nov 04 '15 at 12:45
  • Don't use return like that, you won't get anything, please take a look at my answer at the following question http://stackoverflow.com/questions/32627089/post-request-json-file-passing-string-and-wait-for-the-response-volley/32627293#32627293 – BNK Nov 04 '15 at 12:59
  • Another one http://stackoverflow.com/questions/32375295/android-how-to-return-async-jsonobject-from-method-using-volley :) – BNK Nov 04 '15 at 13:05
  • @BNK How about: http://stackoverflow.com/questions/19837820/volley-jsonobjectrequest-post-request-not-working, looks very concise, and the call is very clean as simply: `RequestQueue requestQueue = Volley.newRequestQueue(getActivity()); CustomRequest jsObjRequest = new CustomRequest(Method.POST, url, params, this.createRequestSuccessListener(), this.createRequestErrorListener()); requestQueue.add(jsObjRequest);` – Sauron Nov 04 '15 at 21:21
  • @Sauron: it's OK, sure, however, I just mean if you move these lines into a method/funtion, then return at the end of that method, you won't get anything (null) with that return value. I will give you another link for your reference :) – BNK Nov 04 '15 at 22:18
  • @Sauron: please read my question before http://stackoverflow.com/questions/31602042/android-java-how-to-delay-return-in-a-method, you should read all answers there :), hope it helps! – BNK Nov 04 '15 at 22:20
  • What is wrong with the post I made just above? – Sauron Nov 04 '15 at 23:57
  • @Sauron: if you just ask about final variable, you should only change its scope (declaration). I only tell you about the `return getRequestReturn;`, it means when you call `abc = getRequest(...);` somewhere, you will always get an empty jsonobject – BNK Nov 05 '15 at 01:32

2 Answers2

0

Declare JSONObject as global and initialize in same place like this.

 JSONObject getRequestReturn ;


getRequestReturn = new JSONObject();
Abhinav singh
  • 1,448
  • 1
  • 14
  • 31
d.k.
  • 415
  • 4
  • 16
0
public Object getRequest(String params) {

    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET,
            VolleySingleton.prefixURL, ((String) null),
            new Response.Listener<JSONObject>() {
                @Override 
                public void onResponse(JSONObject response) {

                    // Parse the JSON: 
                    try { 

                        Log.v("GET Request value", response.toString());

                    } catch (JSONException e) {
                        e.printStackTrace();
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    } 

                } 
            }, 
            new Response.ErrorListener() {
                @Override 
                public void onErrorResponse(VolleyError error) {
                    Log.d("GET Request Error", error.toString());
                } 
            }); 

    mRequestQueue.add(getRequest);

    return new JSONObject();
} 

U can't get response when u return!Volley request is async!

I use EventBus and I do it like this :

When I need to get data from web , i add a request like this.

Then when i get response from web ,I use EventBus to post a event.

I get the event and update my page.

Or U can try the RxAndroid.

tiny sunlight
  • 6,231
  • 3
  • 21
  • 42