Volley JSONException: End of input at character 0 of

前端 未结 5 1773
-上瘾入骨i
-上瘾入骨i 2020-12-29 23:43

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 attempti

5条回答
  •  Happy的楠姐
    2020-12-29 23:57

    I also have encountered this issue.

    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.

    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 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));
            }
        } 
    

    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.

提交回复
热议问题