我正在尝试使用Volley从网址中检索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":""}]
这里有什么问题?
答案 0 :(得分:0)
在黑暗中完全拍摄,但我在RSS解析器上发生了类似情况。事实证明我使用的URL是HTTP,但重定向到HTTPS,我使用的是HttpURLConnection而不是HttpsURLConnection。
虽然,我没有使用Android Volley所以YMMV。
答案 1 :(得分:0)
我终于解决了这个问题,这里的问题是由于某种原因,JSonArrayRequest没有采用POST参数。
所以我只是手动将参数附加到网址