Android凌空抽射。 JsonArray JsonException 在字符 0 输入结束
Android Volley. JsonArray JsonException end of input at character 0
我正在尝试使用 Volley 从 url 中检索 json 数组。
问题是我得到
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":""}]
这里有什么问题?
完全是在黑暗中拍摄的,但我在 RSS 解析器上也遇到过类似的情况。事实证明,我使用的 URL 是 HTTP,但重定向到 HTTPS,我使用的是 HttpURLConnection 而不是 HttpsURLConnection。
不过,我没有使用 Android Volley,所以 YMMV。
我终于解决了这个问题,这里的问题是由于某种原因 JSonArrayRequest 没有采用 POST 参数。
所以我只是手动将参数附加到 url
我正在尝试使用 Volley 从 url 中检索 json 数组。 问题是我得到
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":""}]
这里有什么问题?
完全是在黑暗中拍摄的,但我在 RSS 解析器上也遇到过类似的情况。事实证明,我使用的 URL 是 HTTP,但重定向到 HTTPS,我使用的是 HttpURLConnection 而不是 HttpsURLConnection。
不过,我没有使用 Android Volley,所以 YMMV。
我终于解决了这个问题,这里的问题是由于某种原因 JSonArrayRequest 没有采用 POST 参数。
所以我只是手动将参数附加到 url