Volley JSONException: End of input at character 0 of

前端 未结 5 1785
-上瘾入骨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条回答
  •  盖世英雄少女心
    2020-12-29 23:59

    I had the same problem, I fixed it by creating a custom JsonObjectRequest that can catch a null or empty response :

    public class CustomJsonObjectRequest extends JsonObjectRequest {
    
    public CustomJsonObjectRequest(int method, String url, JSONObject jsonRequest, Response.Listener listener, Response.ErrorListener errorListener) {
        super(method, url, jsonRequest, listener, errorListener);
    }
    
    public CustomJsonObjectRequest(String url, JSONObject jsonRequest, Response.Listener listener, Response.ErrorListener errorListener) {
        super(url, jsonRequest, listener, errorListener);
    }
    
    @Override
    protected Response parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
    
            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));
        }
    }
    

    Then just replace the default JsonObjectRequest by this one !

提交回复
热议问题