Volley JSONException:在0的字符0处输入结束

Mik*_*ker 16 java rest android json android-volley

我见过其他人遇到过这个问题,但没有一个帖子可以帮助我.我正在尝试将Volley用于我的REST调用库,当我尝试使用带有JSON对象的Put调用作为参数时,我得到了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);
    }
}
Run Code Online (Sandbox Code Playgroud)

jsonParams日志显示:

json: {"statusId":"1"}
Run Code Online (Sandbox Code Playgroud)

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

Gil*_*yof 41

我也遇到过这个问题.

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

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

    @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));
        }
    } 
Run Code Online (Sandbox Code Playgroud)

请记住,使用此修复程序,并且在服务器响应为空的情况下,请求回调将返回空引用来代替JSONObject.

  • 谢谢.这就是诀窍.救了我几个小时的头痛 (2认同)