Java Volley JSONException:在字符 0 处输入结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32104423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Volley JSONException: End of input at character 0 of
提问by Mike Walker
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
.
我见过其他人遇到过这个问题,但没有一个帖子能够帮助我。我正在尝试将 Volley 用于我的 REST 调用库,当我尝试使用带有 JSON 对象作为参数的 Put 调用时,我得到error with: org.json.JSONException: End of input at character 0 of
.
Here is the code:
这是代码:
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);
}
}
The jsonParams log displays:
jsonParams 日志显示:
json: {"statusId":"1"}
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.
是否还有我缺少的其他设置?该请求似乎无法解析 JSON 对象。我什至尝试创建一个 HashMap,然后使用它来创建一个 JSON 对象,但我仍然得到相同的结果。
采纳答案by Gil Moshayof
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.
这是因为您的服务器端出现问题不一定是真的 - 这只是意味着 的响应JsonObjectRequest
为空。
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.
很可能服务器应该向您发送内容,而其响应为空的事实是一个错误。但是,如果这是服务器的行为方式,那么要解决此问题,您将需要更改 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));
}
}
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
.
请记住,使用此修复程序,并且在来自服务器的空响应的情况下,请求回调将返回空引用代替JSONObject
.
回答by blackout
Might not make sense but nothing else worked for me but adding a content-type header
可能没有意义,但除了添加内容类型标头之外没有其他方法对我有用
mHeaders.put("Content-Type", "application/json");
回答by Oush
In my case it was simply the request I was sending(POST) was not correct. I cross-checked my fields and noted that there was a mismatch, which the server was expecting to get thus the error->end of input at character 0 of...
就我而言,这只是我发送的请求(POST)不正确。我交叉检查了我的字段并注意到有一个不匹配,服务器期望得到这样的错误->输入的字符 0 结束......
回答by kamel sabri
I had the same problem, I fixed it by creating a custom JsonObjectRequest that can catch a null or empty response :
我遇到了同样的问题,我通过创建一个可以捕获空或空响应的自定义 JsonObjectRequest 来修复它:
public class CustomJsonObjectRequest extends JsonObjectRequest {
public CustomJsonObjectRequest(int method, String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
super(method, url, jsonRequest, listener, errorListener);
}
public CustomJsonObjectRequest(String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
super(url, jsonRequest, listener, errorListener);
}
@Override
protected Response<JSONObject> 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 !
然后用这个替换默认的 JsonObjectRequest !