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

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测