Volley JSONException:在0的字符0处输入结束 [英] Volley JSONException: End of input at character 0 of

查看:139
本文介绍了Volley JSONException:在0的字符0处输入结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过其他人遇到过这个问题,但没有一个帖子可以帮助我。我正在尝试将Volley用于我的REST调用库,当我尝试使用带有JSON对象的Put调用作为参数时,我得到错误:org.json.JSONException :的字符0处的输入结束。

I've seen others come across this problem, but none of the posts have been able to assist me. I'm attempting to use Volley for my REST call library, and when I'm attempting to use a Put call with a JSON Object as a parameter, I'm getting error with: org.json.JSONException: End of input at character 0 of.

以下是代码:

protected void updateClientDeviceStatus(Activity activity, final int status) {
    JSONObject jsonParams = new JSONObject();
    try {
        jsonParams.put("statusId", String.valueOf(status));
    } catch (JSONException e1) {
        e1.printStackTrace();
    }
    Log.i(LOG_TAG, "json: " + jsonParams.toString());

    String url = Constants.API_URL + "client/device/" + getDeviceId();
    // Request a response from the provided URL.
    JsonObjectRequest request = new JsonObjectRequest
            (Request.Method.PUT, url, jsonParams, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.i(LOG_TAG, "updated client status");
                    Log.i(LOG_TAG, "response: " + response.toString());
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.i(LOG_TAG, "error with: " + error.getMessage());
                    if (error.networkResponse != null)
                        Log.i(LOG_TAG, "status code: " + error.networkResponse.statusCode);


                }
            }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String>  params = new HashMap<String, String>();
            params.put("User-Agent", getUserAgent());
            params.put("X-BC-API", getKey());

            return params;
        }

        @Override
        public String getBodyContentType() {
            return "application/json";
        }
    };

    request.setRetryPolicy(new DefaultRetryPolicy(20000, 3, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    MySingleton.getInstance(activity).addToRequestQueue(request);
    }
}

jsonParams日志显示:

The jsonParams log displays:

json: {"statusId":"1"}

我还缺少其他设置吗?似乎请求无法解析JSON对象。我甚至尝试创建一个HashMap,然后用它来创建一个JSON对象,但我仍然得到相同的结果。

Is there another setting that I'm missing? It appears that the request can't parse the JSON Object. I even tried creating a HashMap and then using that to create a JSON Object, but I still get the same result.

推荐答案

我也遇到过这个问题。

这不一定是因为服务器端出现问题 - 它只是意味着的响应JsonObjectRequest 是空的。

It's not necessarily true that this is because a problem on your server side - it simply means that the response of the JsonObjectRequest is empty.

很可能是服务器应该向你发送内容,而且它的响应为空的事实是错误。但是,如果这是服务器的行为方式,那么要解决此问题,您需要更改JsonObjectRequest解析其响应的方式,这意味着创建 JsonObjectRequest的子类 ,并将 parseNetworkResponse 覆盖到下面的示例。

It could very well be that the server should be sending you content, and the fact that its response is empty is a bug. If, however, this is how the server is supposed to behave, then to solve this issue, you will need to change how JsonObjectRequest parses its response, meaning creating a subclass of JsonObjectRequest, and overriding the parseNetworkResponse to the example below.

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));

            JSONObject result = null;

            if (jsonString != null && jsonString.length() > 0)
                 result = new JSONObject(jsonString);

            return Response.success(result,
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    } 

请记住,使用此修复程序,以及来自服务器的响应为空的事件,请求回调将返回一个空引用来代替 JSONObject

Keep in mind that with this fix, and in the event of an empty response from the server, the request callback will return a null reference in place of the JSONObject.

这篇关于Volley JSONException:在0的字符0处输入结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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