zoukankan      html  css  js  c++  java
  • 异常:org.json.JSONException: End of input at character 0 of

    在使用Gson时遇到org.json.JSONException: End of input at character 0 of 异常。

    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();
            }
    
        }

    原因:在请求发送后会调用 public void onResponse(JSONArray array)方法,如果服务端无返回值,就会报End of input at character 0 of 异常。如果返回值不符合json标准,也会报异常。

    解决办法:1.在服务器端返回一个缺省json值。2.使用其他JSON类代替。3.关闭异常输出

    如果返回值无法转换成json也会报异常。

    http://stackoverflow.com/questions/8740381/getting-a-jsonexception-end-of-input-at-character-0

  • 相关阅读:
    Infopath Notify 弹出提示信息
    window.showModalDialog 返回值
    【转】获得正文内容中的所有img标签的图片路径
    Json Datable Convert
    Sharepoint 列表 附件 小功能
    Surgey 权限更改
    SQL 触发器用于IP记录转换
    Caml语句 查询分配给当前用户及当前组
    jquery 1.3.2 auto referenced when new web application in VSTS2010(DEV10)
    TFS diff/merge configuration
  • 原文地址:https://www.cnblogs.com/mada0/p/4864787.html
Copyright © 2011-2022 走看看