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