JsonParseException: Unrecognized
2017-06-28 本文已影响0人
考特林
安卓用Volley做为请求框架,后台用SpringMvc,数据传输格式用Json。
请求的时候报一个错。
{
"timestamp": 1498636645291,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "Could not read document: Unrecognized token 'diffTime': was expecting ('true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream@3a2ef3ba; line: 1, column: 10]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'diffTime': was expecting ('true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream@3a2ef3ba; line: 1, column: 10]",
"path": "/data/country/update"
}
挣扎许久后,发现是客户端Volley框架发出请求时的数据格式不对。
修改如下即可
在Volley框架中的Request.java类:
private byte[] encodeParameters(Map<String, String> params, String paramsEncoding) {
// StringBuilder encodedParams = new StringBuilder();
// try {
// for (Map.Entry<String, String> entry : params.entrySet()) {
// encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
// encodedParams.append('=');
// encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
// encodedParams.append('&');
// }
// return encodedParams.toString().getBytes(paramsEncoding);
// } catch (UnsupportedEncodingException uee) {
// throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
// }
try{
JSONObject jsonObject = new JSONObject(params);
return jsonObject.toString().getBytes(paramsEncoding);
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
}
}
自己封装的继承Request<T>类,也需要补充点代码:
主要是getHeaders()、getBodyContentType()、parseNetworkResponse()方法
public class StringRequestJsonResult extends Request<JSONObject> {
protected static final String PROTOCOL_CHARSET = "utf-8";
private final Response.Listener<JSONObject> mListener;
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Accept", "application/json");
params.put("Content-Type", "application/json; charset=UTF-8");
return params;
}
@Override
public String getBodyContentType() {
return "application/json; charset=" + getParamsEncoding();
}
/**
* Creates a new request with the given method.
*
* @param method the request {@link Method} to use
* @param url URL to fetch the string at
* @param listener Listener to receive the String response
* @param errorListener Error listener, or null to ignore errors
*/
public StringRequestJsonResult(int method, String url, Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
}
/**
* Creates a new GET request.
*
* @param url URL to fetch the string at
* @param listener Listener to receive the String response
* @param errorListener Error listener, or null to ignore errors
*/
public StringRequestJsonResult(String url, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
this(Method.GET, url, listener, errorListener);
}
@Override
protected void deliverResponse(JSONObject response) {
mListener.onResponse(response);
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
return Response.success(JSON.parseObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
}
以上,完毕。