我遇到了语法问题,但我不知道出了什么问题。这条线似乎有答案,但它确实对我有用。
JSONException: Value of type java.lang.String cannot be converted to JSONObject
我正在使用Volley,我需要扩展JsonObjectRequest,以便能够访问响应中的http头。这是我的"CustomRequest“
public class CustomRequest extends JsonObjectRequest {
public CustomRequest(int method, String url, String jsonRequest,
Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
super(method, url, jsonRequest, listener, errorListener);
}
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
// Save http headers
mHeaders = response.headers;
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
private Map<String, String> mHeaders = new HashMap<>();
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return mHeaders;
}
}在这里,我的考试课:
private void testVolleyRequest() {
String url = "http://my-json-feed";
CustomRequest jsonRequest = new CustomRequest
(Request.Method.GET, url, (String)null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("[TEST]", "Response: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("[TEST]", "error = "+error.toString());
}
});
Volley.newRequestQueue(this).add(jsonRequest);
}如果我试一下上面的样本,我就会得到:
com.android.volley.ParseError: org.json.JSONException: Value <html><head><meta of type java.lang.String cannot be converted to JSONObject但是我要返回一个JSONObject:
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));如果有人能发现这个问题,我非常感激。
太棒了!
发布于 2016-02-17 23:30:57
他是个精品店,
上面的url没有提供有效的json文件。我改为:
字符串url = "http://httpbin.org/get?site=code&network=tutsplus";
而且起作用了!
https://stackoverflow.com/questions/35469634
复制相似问题