简体   繁体   English

在 onResponse 之后通过 volley 传回数据

[英]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'我需要获取一些从 api 调用返回的数据(其中“Get something and set..允许我从“onResponse”内部移动数据以从“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
}

set first parameter in new StringRequest() your request method.new StringRequest()中设置您的请求方法的第一个参数。 example Request.Method.GET示例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而且你不能有一个 void function 返回值。所以删除 return 并将此代码替换为 onResponse() 方法

NumberCallback.onSuccess(number);

then into Destination receive callback.然后进入 Destination 接收回调。 ask if you don't how receive callback.问你不怎么收到回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM