【问题标题】:Android JSON request from PHP server - JSON Mismatch Exception来自 PHP 服务器的 Android JSON 请求 - JSON 不匹配异常
【发布时间】:2013-03-29 04:25:20
【问题描述】:

使用这个函数,我将 JSON 数据读入对象,然后尝试打印它们的 ID:

private void getJsonData(String url) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url);
    try {
        HttpResponse response = client.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) { // success!
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            JSONObject jsonObject = new JSONObject(builder.toString());
            for (int i = 0; i < jsonObject.length(); i++) {
                Toast.makeText(context, jsonObject.getString("id"), Toast.LENGTH_LONG).show();
            }
        } else {
            Log.e(">>", "Failed to download file");
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

我收到此错误:http://i.imgur.com/CkFOJmk.jpg

有什么想法吗?它在该行出错:

HttpEntity entity = response.getEntity();

我的 php:

    $this->db->select('id, unit_number');
    $this->db->from('parts');
    $this->db->order_by('unit_number', 'desc');
    $query = $this->db->get();
    echo json_encode($query->result());

【问题讨论】:

  • 意味着你没有得到有效的 json 字符串形式的服务器请发布你从服务器得到的完整响应
  • @ρяσѕρєяK 看起来好像我的 JSON 由于某种原因中间对象在最后被截断。嗯..

标签: android json rest


【解决方案1】:

不确定,但您从服务器发送的 JSON 似乎与 android(?) 使用的标准 JSON 格式有所不同。无论如何,最好看到您从服务器获得的完整字符串响应。

确保您接收的是 JSON 而不是 HTML。 Error Android - received HTML instead of JSON

【讨论】:

    猜你喜欢
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多