【问题标题】:org.json.JSON Exception : End of input at character 0org.json.JSON 异常:字符 0 处的输入结束
【发布时间】:2012-01-02 09:00:53
【问题描述】:

我正在尝试从 android 解析 json,但我得到了这个奇怪的异常。我的json数据是

{"id":"1","owner":"1","name":"gravitas","description":"is a fest","start_time":"0000-00-00 00: 00:00","end_time":"0000-00-00 00:00:00","venue":"vellore","radius":"10","lat":"11","lng": "11","type":"type","ownername":"dilip","noofpolls":0,"noofquizes":0,"peopleattending":0,"result":true}

在android中我会这样做

JSONObject j =new JSONObject(response);
Event pst = gson.fromJson(j.toString(),  Event.class);

我明白了:

org.json.JSONException: end of input at character 0 of

它有什么问题?这是代码...

RestClient client = new RestClient("http://192.168.1.3/services/events/"+eve.getName());         
        try {
             Log.i("MY INFO", "calling boston");
            client.Execute(RequestMethod.POST);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String response = client.getResponse();
         Log.i("MY INFO", response);
         GsonBuilder gsonb = new GsonBuilder();
         Gson gson = gsonb.create();
         Event pst = null;

        try {
            JSONObject j =new JSONObject(response);
            pst = gson.fromJson(j.toString(),  Event.class);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

【问题讨论】:

    标签: android json


    【解决方案1】:

    哎呀!我的错,我应该使用 GET 方法。那个 url 不响应 POST 请求,所以我得到了 org.json.JSON 异常:字符 0.its 的输入结束,因为这个我得到了产生该异常的空响应.

    【讨论】:

    • 嗨,Rakon_188,我也遇到了同样的问题..但我正在尝试不同的方式..你能把完整的代码发给我吗..
    • 在这里遇到了同样的问题,但看起来我不得不将我的切换到 POST 而不是 GET。如果您只是有错误的错误,似乎会引发此错误,因此您可能需要简单地切换您的呼叫。
    【解决方案2】:

    在我的例子中,我指的是一个甚至不存在的函数名。在更正函数后检查你的网络服务的函数名名字,它对我有用!

    【讨论】:

      【解决方案3】:

      这是一个关于如何使用 GSON 进行解析的 sn-p

          Gson gson = new GsonBuilder().create();
          JsonParser jsonParser = new JsonParser();
      
           String response = client.getResponse();
          JsonObject jsonResp = jsonParser.parse(response).getAsJsonObject();
          // Important note: JsonObject is the one from GSON lib!
          // now you build as you like e.g.
          Event mEvent = gson.fromJson(jsonResp, Event.class);
      
      ...
      

      【讨论】:

      【解决方案4】:

      这个错误有时也是因为json_encode函数要求所有传入的数据都被UTF-8编码。

      尝试将mysqli_set_charset($con, 'utf8'); 添加到您的 php 中。

      【讨论】:

        猜你喜欢
        • 2016-10-04
        • 1970-01-01
        • 2014-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        相关资源
        最近更新 更多