public static void jsonArrayRequest(final MainActivity mainActivity, RequestQueue rq, String uri,
TextView tv1, List<News> datas) {
final Gson g = new Gson();
String json = g.toJson(datas);
try {
JSONArray jsonArray = new JSONArray(json);
JsonArrayRequest jar = new JsonArrayRequest(Method.POST, uri, jsonArray,
new Listener<JSONArray>() {
@Override
public void onResponse(JSONArray array) {
//如果返回值为空,JSONArray无法把它转换成JSONArray则会报End of input at character 0 of异常
//如果返回值不符合json标准,也会报异常
String json = array.toString();
Type typeOfT = new TypeToken<List<News>>() {
}.getType();
List<News> list = g.fromJson(json, typeOfT);
Log.i("pepelu", list.get(0).getTitle());
}
}, new ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
Toast.makeText(mainActivity, "error:" + arg0.getMessage(), Toast.LENGTH_SHORT)
.show();
Log.i("pepelu", arg0.getMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("accept", "application/json");
map.put("content-type", "application/json;charset=utf-8");
return map;
}
};
rq.add(jar);
} catch (JSONException e) {
e.printStackTrace();
}
}
http://stackoverflow.com/questions/8740381/getting-a-jsonexception-end-of-input-at-character-0