0

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 ?

mukeshsoni
  • 483
  • 8
  • 29

1 Answers1

3

I think you require something of this kind

 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);*/

                        }
ankur uniyal
  • 86
  • 11