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条回答 默认 最新

  • douhan5547 2018-05-17 11:34
    关注

    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);*/
    
                            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!