drodsh7940
2018-05-17 11:19
浏览 59

使用服务器的信息加载图像

I'm showing image with description and other info from server. I'm using volley. Images are category wise. When user click on button, it post table name to server and server response with images. I'm using POST JSONobject request.

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 File

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 look like this .

{"item":{"0":{"image":"http:\/\/192.168.43.238\/djp\/aad\/aad002.jpg","weight":"15","date":""}}}

now how do i loop through JSONobject ?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题