0

In my project i receive data from my server using the "Volley" lib, I made PHP file which returns JSON encoded values and I want to take those values and return them from and not just process them inside the StringRequest,

My code is:

public static void getStatus(final String id, final Marker marker1)
    {
        // Tag used to cancel the request
        String tag_string_req = "req_register";
        temp="";
        StringRequest strReq = new StringRequest(
                Request.Method.POST,
                "http://URL/file.php",
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {



                        try {
                            JSONObject jObj = new JSONObject(response);
                            String status = jObj.getString("string");
                            temp = status;


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

                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                // Posting parameters to getData url
                Map<String, String> params = new HashMap<String, String>();
                params.put("id", id);
                return params;
            }

        };

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

    }

How can i return the string "status" from this method? (If i change the method to String from void I still couldn't return any values from inside the onResponse (idk why...))? please explain to me why and how can I make it work?

drdisrespect
  • 55
  • 1
  • 11

2 Answers2

0

The short answer is that you don't return. Nor should you.

Instead, you pass the result into whatever code block you want to do next. This could be a separate method or could all be done from within the StringRequest onResponse.

The main point is that you can't assign temp at the top, set it within the StringRequest, then expect a value on the line after addToRequestQueue

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Yes I know it doesn't work to put the temp as the return string, but can you explain just why? or where can i read about that? – drdisrespect Jun 17 '17 at 23:25
  • That's how asynchronous code works. Threading is the general topic you could read about – OneCricketeer Jun 17 '17 at 23:35
  • In similar regards, you might have seen Asynctask compared to Volley. You could build a Asynctask with a Response Listener as well https://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a – OneCricketeer Jun 17 '17 at 23:37
  • Thank you that will help me alot. – drdisrespect Jun 17 '17 at 23:41
0

This type of Asynchronous method (StringRequest) does not work like other methods. For that you can see the documentation for Threads here https://developer.android.com/reference/java/lang/Thread

You can't return anything directly from String Request's Response. The JSON object parameter that a StringRequest gets from the server side API is of this format [{"success":"1"}]. In here, note that JSON objects are formed like a {key:value} pair. So, the JSON object's keys are the left sided value of ":" and the response values are the right sided one. In the above response, "1" was returned for the key "success". You can explicitly introduce field parameter in the class and use that variable to catch the response value inside the onResponse and then use it as you like. Hope this info will help you. So, just remove the temp declaration from inside the getStatus and declare that above in the class field. Hope this will help you!

Farhan Ibn Wahid
  • 926
  • 1
  • 9
  • 22