排球请求回调传递了错误的回调响应 [英] Volley Request Callback passing the wrong callback response

查看:133
本文介绍了排球请求回调传递了错误的回调响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在截击请求上添加了一个回调,并且我有2个请求是从2个不同的活动开始的.当我执行第一个请求时,第二个请求将返回第一个请求的响应.

I've added a callback on the volley request and, I have two request started from 2 different activity. When I perform the first request, then the second return the response of the first..

这是我的请求和回叫:

public static void RequestJsonToServer(Context ctx, final JSONObject params, final VolleyCallback callback){

    MySingleVolley.getInstance(ctx).
            getRequestQueue();

    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,ctx.getString(R.string.defaultServerRequestUrl),params,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    callback.onSuccess(response);
                }
            },new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("REQUEST_JSON_TO_SERVER", "Error: " + error);
                }
            }){
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> headers = new HashMap<>();
                        headers.put("Content-Type", "application/json");
                        return headers;
                    }
            };

    MySingleVolley.getInstance(ctx).addToRequestQueue(jsObjRequest);

}

public interface VolleyCallback{
    void onSuccess(JSONObject string);
}

这是两个启动请求之一:

And this is one of the two starting request:

Global.RequestJsonToServer(getActivity(), jsonObject, new Global.VolleyCallback() {
        @Override
        public void onSuccess(JSONObject result) {
            Toast.makeText(getActivity(),result.toString(), Toast.LENGTH_LONG).show();
        }
    });

我希望有人能帮助我

谢谢

修改: 我这样改变

Global.RequestJsonToServer(getApplicationContext(), jsonObject, new Global.VolleyCallback() {

        @Override
        public void onSuccess(JSONObject result) {
            Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
        }

        @Override
        public void onSuccessCustom(JSONObject string) {

        }
    }, true);

另一个为假..但是使用它的方式可能是错误的.我希望有一个回调并重用它,而不是在两个回调之间切换

And the other one with false.. But maybe is the wrong way to use it..I would like to have one single callback and reuse it, not switch between two callbacks

我的解决方案

我找到了自己的解决方案,问题不在回调中,而在截击请求中.每个请求的响应都被缓存了,我不知道为什么,它将总是返回错误的响应.

I've found my own solution, the problem wasn't in the callback but was in the volley request. The response of each request was cached and i don't know why, it will be return the wrong always the wrong response.

我刚刚在将请求添加到队列之前添加了此内容:

I've just added this before adding request to queue:

jsObjRequest.setShouldCache(false);

推荐答案

之所以会这样,是因为您的回调方法很常见[onSuccess(...)]为什么您不编写2个回调,并且根据条件使用必需的回调. 要实现它,请在您的接口中编写2个方法,并传递某种标记以选择回调.
将界面更改为此.

This is happening because your callback method is common[onSuccess(...)]why don't you write 2 call backs and based on the condition use the required callback. To implement it write 2 methods in your interface and pass some sort of flag to choose callback.
Change your interface to this.

public interface VolleyCallback{
    void onSuccess(JSONObject string);
    void customOnSuccess(JSONObject string);
}

以及您对此的方法

public static void RequestJsonToServer(boolean flag,Context ctx, final JSONObject params, final VolleyCallback callback){

    MySingleVolley.getInstance(ctx).
            getRequestQueue();

    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,ctx.getString(R.string.defaultServerRequestUrl),params,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    if(flag){
                    callback.onSuccess(response);
                    }else{
                    callback.CustomOnSuccess(response);
                   }
                }
            },new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("REQUEST_JSON_TO_SERVER", "Error: " + error);
                }
            }){
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> headers = new HashMap<>();
                        headers.put("Content-Type", "application/json");
                        return headers;
                    }
            };

    MySingleVolley.getInstance(ctx).addToRequestQueue(jsObjRequest);

}

是的,它在onSucces上可重用,在您只有一个回调的情况下,一次使用多个回调都会使用.

Yes its reusable use onSucces where ever you have only one callback use both when you have more than one.

这篇关于排球请求回调传递了错误的回调响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆