【问题标题】:End of input at character 0 while parsing json response from server解析来自服务器的 json 响应时在字符 0 处输入结束
【发布时间】:2013-01-05 11:26:27
【问题描述】:

我正在将数据发送到服务器,并尝试解析格式为 {"count":"2"} 的 json 响应,但我遇到了异常

这是我的代码

post(serverUrl, params);

HttpClient httpClient = new DefaultHttpClient();
try{
    get = new HttpPost(url);
    r = httpClient.execute(get);
    he = r.getEntity();
    InputStream is = he.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
    StringBuilder sb = new StringBuilder();

    String line = null;
    while ((line = reader.readLine()) != null) { sb.append(line + "\n"); }

    String result = sb.toString();
    jObj = new JSONObject(result);
    Log.d(TAG, "Count is" + jObj);
    count = jObj.getString("count");
    Log.d(TAG, "Count is" + count);
}
catch (Exception e) { e.printStackTrace(); }

【问题讨论】:

    标签: android httprequest httpresponse json


    【解决方案1】:

    响应中可能存在空白值。它不能被认为是空的。这个错误是因为空白响应。

    【讨论】:

      【解决方案2】:

      这是我的服务器代码,我保持默认值,所以它肯定不能为空

      $jsonOutput = array('count' => $i);
      header('Content-type: application/json');
      echo json_encode($jsonOutput);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-17
        • 2017-08-01
        • 2014-09-26
        • 2014-06-03
        • 2013-08-20
        • 2013-08-05
        相关资源
        最近更新 更多