如何从Volley的响应中返回值?

时间:2022-02-09 20:58:29
public class getString  {
String tag_string_req = "string_raq";
String url = "http://10.0.2.2/eat/locations/index.json";
String result="";

public String get_String() {
    StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            result=response;
            System.out.println(response);
            ;

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            System.out.println(volleyError.getMessage());
        }
    });
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    return result;
}}

I would to build an object of getString and call get_String in other fields. But it seems that it is hard to get the result out from the onResponse. I know it cannot work in this current way. Could anyone help me to settle this problem?

我想构建一个getString对象并在其他字段中调用get_String。但似乎很难从onResponse中获得结果。我知道它不能以当前的方式工作。任何人都可以帮我解决这个问题吗?

1 个解决方案

#1


106  

You want to use callback interfaces like so:

你想使用如下的回调接口:

public void getString(final VolleyCallback callback) {
    StringRequest strReq = new StringRequest(Request.Method.GET, url, new     Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            ...  // (optionally) some manipulation of the response 
            callback.onSuccess(response);
        }
    }...
}}

Where the callback is defined as

将回调定义为

public interface VolleyCallback{
    void onSuccess(String result);
}

Example code inside activity:

活动内的示例代码:

public void onResume(){
    super.onResume();

    getString(new VolleyCallback(){
         @Override
         public void onSuccess(String result){
             ... //do stuff here
         }
    });
}

You can also make VolleyCallback more robust, using generic types if you want to do processing, or adding start(), failed(Exception e), complete(), etc methods to do a little more fine-grained state checking.

如果你想进行处理,或者添加start(),failed(异常e),complete()等方法来进行更细粒度的状态检查,你也可以使用泛型类型使VolleyCallback更健壮。

Keep in mind this is an async call, so you will have to update views, etc when you get the result back (inside success()).

请记住,这是一个异步调用,因此当您获得结果(在success()内部时,您将不得不更新视图等。

#1


106  

You want to use callback interfaces like so:

你想使用如下的回调接口:

public void getString(final VolleyCallback callback) {
    StringRequest strReq = new StringRequest(Request.Method.GET, url, new     Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            ...  // (optionally) some manipulation of the response 
            callback.onSuccess(response);
        }
    }...
}}

Where the callback is defined as

将回调定义为

public interface VolleyCallback{
    void onSuccess(String result);
}

Example code inside activity:

活动内的示例代码:

public void onResume(){
    super.onResume();

    getString(new VolleyCallback(){
         @Override
         public void onSuccess(String result){
             ... //do stuff here
         }
    });
}

You can also make VolleyCallback more robust, using generic types if you want to do processing, or adding start(), failed(Exception e), complete(), etc methods to do a little more fine-grained state checking.

如果你想进行处理,或者添加start(),failed(异常e),complete()等方法来进行更细粒度的状态检查,你也可以使用泛型类型使VolleyCallback更健壮。

Keep in mind this is an async call, so you will have to update views, etc when you get the result back (inside success()).

请记住,这是一个异步调用,因此当您获得结果(在success()内部时,您将不得不更新视图等。