在凌空中接收带有json请求的布尔变量 [英] receiving Boolean variable with json request in volley

查看:100
本文介绍了在凌空中接收带有json请求的布尔变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用android中的volley通过web-api向MVC Server发出请求,我想将函数的参数作为JSON(请求主体)发送,因此我使用JSONRequest进行了调用,但问题是JSONRequest迫使您使用Response.Listner,而我正在调用的函数返回一个Boolean变量,因此每次调用时都会出现转换错误

I'm trying to make a request to MVC Server with web-api using volley in android, I want to send the parameters of the function as a JSON (the request body), so I used JSONRequest to make the call but the problem is the JSONRequest forces you to Use the Response.Listner, and the function I'm calling returns a Boolean variable , so I'm getting casting error every time I make the call

我试图进行一个接受布尔值的自定义调用,但现在出现错误400,但我不知道为什么,有没有办法使jsonRequest接收布尔值?如果没有,那么解决方案是什么?

I tried to make a custom call that accepts Boolean but now I'm getting error 400 and I don't know why, is there a way to make the jsonRequest receive Boolean? and if not, what is the solution ?

我第一次使用的功能:

public void getJSON(String controllerName,String actionName, JSONObject requestBody,Response.Listener<JSONObject> success,Response.ErrorListener error){
    String url = makeURL(controllerName,actionName);
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST,url,requestBody,success,error);
    request.setTag(APP_TAG);
    requestQueue.add(request);
}

这是我的第二次自定义请求审判(错误400):

and here is my second trial with custom request (getting error 400):

public void getBooleanRequest(String controllerName,String actionName, final HashMap<String,String> body, final HashMap<String,String> header, final Response.Listener<Boolean> success,Response.ErrorListener error){
    String url = makeURL(controllerName,actionName);
    Request<Boolean> request = new Request<Boolean>(Request.Method.POST,url,error) {
        @Override
        protected Response<Boolean> parseNetworkResponse(NetworkResponse response) {
            return Response.success(true, HttpHeaderParser.parseCacheHeaders(response));
        }

        @Override
        protected void deliverResponse(Boolean response) {
            success.onResponse(response);
        }

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            return body;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return header;
        }
    };
    request.setTag(APP_TAG);
    requestQueue.add(request);
}

推荐答案

您可以尝试以下示例代码:

You can try my following sample code:

private class BooleanRequest extends Request<Boolean> {
    private final Response.Listener<Boolean> mListener;
    private final Response.ErrorListener mErrorListener;
    private final String mRequestBody;

    private final String PROTOCOL_CHARSET = "utf-8";
    private final String PROTOCOL_CONTENT_TYPE = String.format("application/json; charset=%s", PROTOCOL_CHARSET);

    public BooleanRequest(int method, String url, String requestBody, Response.Listener<Boolean> listener, Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        this.mListener = listener;
        this.mErrorListener = errorListener;
        this.mRequestBody = requestBody;
    }

    @Override
    protected Response<Boolean> parseNetworkResponse(NetworkResponse response) {
        Boolean parsed;
        try {
            parsed = Boolean.valueOf(new String(response.data, HttpHeaderParser.parseCharset(response.headers)));
        } catch (UnsupportedEncodingException e) {
            parsed = Boolean.valueOf(new String(response.data));
        }
        return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
    }

    @Override
    protected VolleyError parseNetworkError(VolleyError volleyError) {
        return super.parseNetworkError(volleyError);
    }

    @Override
    protected void deliverResponse(Boolean response) {
        mListener.onResponse(response);
    }

    @Override
    public void deliverError(VolleyError error) {
        mErrorListener.onErrorResponse(error);
    }

    @Override
    public String getBodyContentType() {
        return PROTOCOL_CONTENT_TYPE;
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        try {
            return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
        } catch (UnsupportedEncodingException uee) {
            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                    mRequestBody, PROTOCOL_CHARSET);
            return null;
        }
    }
}

然后在您的活动中:

   try {
        JSONObject jsonBody;
        jsonBody = new JSONObject();
        jsonBody.put("Title", "Android Demo");
        jsonBody.put("Author", "BNK");
        jsonBody.put("Date", "2015/08/28");
        String requestBody = jsonBody.toString();
        BooleanRequest booleanRequest = new BooleanRequest(0, url, requestBody, new Response.Listener<Boolean>() {
            @Override
            public void onResponse(Boolean response) {
                Toast.makeText(mContext, String.valueOf(response), Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(mContext, error.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        // Add the request to the RequestQueue.
        queue.add(booleanRequest);
    } catch (JSONException e) {
        e.printStackTrace();
    }

这篇关于在凌空中接收带有json请求的布尔变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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