0

使用此函数,我将 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());
4

1 回答 1

0

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

确保您接收的是 JSON 而不是 HTML。错误 Android - 收到 HTML 而不是 JSON

于 2013-03-28T20:25:20.107 回答