Brooksay
Brooksay

Reputation: 41

Passing back data after onResponse with volley

I need to get some data that is returned from the api call (where the 'Get something and set.. comment is) to be returned by the api_call function. How do I do this, since everything I've tried doesn't seem to allow me to move data from inside the 'onResponse' to be returned from the 'api_call'

public void api_call(String Location){
    RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
    String api_key = "xxxxxx";
    String url = "https://api.openweathermap.org./data/2.5/weather?q=" + Location + "&appid=" + api_key + "&units=metric";
    String Number;

    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>(){
        @Override
        public void onResponse(String response){
            //Get something and set it to the number variable
        }
    }, new Response.ErrorListener(){
        @Override
        public void onErrorResponse(VolleyError error){
            ;
        }
    });

    requestQueue.add(stringRequest);
    return Number
}

Upvotes: 0

Views: 135

Answers (1)

hossein
hossein

Reputation: 1076

set first parameter in new StringRequest() your request method. example Request.Method.GET

And initialize a interface for return your string

public interface NumberCallback{
    void onSuccess(Int number);

    void onError(VolleyError error);
}

And You can't have a void function with return value.so delete return and replace this code into onResponse() method

NumberCallback.onSuccess(number);

then into Destination receive callback. ask if you don't how receive callback.

Upvotes: 1

Related Questions