この関数を使用して、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());