我正在尝试使用 Volley 从 url 中检索 jsonarray。问题是我得到
JsonException end of input at character 0
代码如下:
JsonArrayRequest req = new JsonArrayRequest(Request.Method.POST, openMatchesUrl,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d("JSON", response.toString());
try {
for (int i = 0; i < response.length(); i++) {
//do stuff
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
"onErrorResponse ongoing: "+error.getMessage(), Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams()
{
//build params
}
};
// Add the request to the RequestQueue.
queue.add(req);
我在想问题出在错误的参数上。但我尝试了一个简单的字符串请求:
StringRequest req = new StringRequest(Request.Method.POST, openMatchesUrl,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("JSON", "resp: " +response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("JSON", error.toString());
}
}){
@Override
protected Map<String, String> getParams()
{
//build params
}
};
它实际上正确地返回了 json。例如:
[{"roundid":4152,"numberofplayers":1,"dateevent":"2015-04-13 19:45:32.121124+02","playernames":"cat","turn":1,"codedboard":""},{"roundid":4154,"numberofplayers":1,"dateevent":"2015-04-13 20:16:08.845409+02","playernames":"cat","turn":1,"codedboard":""},{"roundid":4155,"numberofplayers":1,"dateevent":"2015-04-13 20:18:22.002411+02","playernames":"cat","turn":1,"codedboard":""}]
这里有什么问题?