0

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

这是我的代码

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

2 回答 2

2

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

于 2013-01-05T12:32:36.697 回答
0

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

$jsonOutput = array('count' => $i);
header('Content-type: application/json');
echo json_encode($jsonOutput);
于 2013-01-05T12:47:29.487 回答