我正在显示包含服务器描述和其他信息的图片。我正在使用凌空抽射。图像是类别明智的。当用户单击按钮时,它会将表名发布到服务器,并将服务器响应发布到图像。我正在使用POST JSONobject请求。
public void getJsonResponsePost(View v){
JSONObject json = new JSONObject();
try {
json.put("table","aad");
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, json,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error: " + error.getMessage());
serverResp.setText("String Response : "+ error.getMessage());
}
});
jsonObjectRequest.setTag(REQ_TAG);
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest);
}
PHP 文件
header('Content-Type: application/json');
include '../../script/database.php';
$response =array();
$data = json_decode(file_get_contents('php://input'), true);
$table = $data["table"];
$sql = "select * from $table WHERE (dp='1') LIMIT 1";
$query = mysqli_query($db, $sql);
$post_data_array = array();
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$img = $row['image'];
$id_image = $row['id'];
$weight = $row['weight'];
$row_date = $row['date'];
$image_path = $row['path'];
$image = "http://192.168.43.138/djp/" . $image_path . $row['image'];
$post_data_array[] = array(
'image' => $image,
'weight' => $weight,
'date' => $date
);
}
$post_data = json_encode(array('item' => $post_data_array),
JSON_FORCE_OBJECT);
echo $post_data;?>
JSONObject看起来像这样。
{"item":{"0":{"image":"http:\/\/192.168.43.238\/djp\/aad\/aad002.jpg","weight":"15","date":""}}}
现在我如何循环JSONobject?
答案 0 :(得分:3)
我认为你需要这样的东西
JSONArray heroArray = response.getJSONArray("item");
//now looping through all the elements of the json array
for (int i = 0; i < heroArray.length(); i++) {
//getting the json object of the particular index inside the array
/* JSONObject heroObject = heroArray.getJSONObject(i);
JSONObject geoObj= heroObject.getJSONObject("geometry");
JSONObject locObj= geoObj.getJSONObject("location");
LocationData loc=new LocationData( heroObject.getString("name"), heroObject.getString("vicinity"),locObj.getString("lat"),locObj.getString("lng"));
placesData.add(loc);*/
}