2017-08-10 23 views
0

您好我想從mysql中檢索所有數據並通過排列和php顯示在android的listview中但我只收到一個響應對象中的記錄 並在列表視圖中只顯示一條記錄我不知道錯誤在哪裏。 我的Android代碼是不通過排序在android中檢索mysql的所有數據

JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(url, new Response.Listener<JSONArray>() { 
      @Override 
      public void onResponse(JSONArray response) { 
      hide(); 


       ////////get json data 
       for(int i=0; i<response.length(); i++){ 

        try { 
         Log.d("the ","response "+response); 
         JSONObject obj=response.getJSONObject(i); 
         item item=new item(); 
         item.setTitlwimage(obj.getString("Title_Image")); 
         item.setTitle(obj.getString("Vedio_Title")); 
         item.setDescription(obj.getString("Description")); 
         item.setCate(obj.getInt("S_category_Id")); 
         // item.setTime(obj.getDouble("Datetime")); 

        list.add(item); 

        } 
        catch (JSONException e) { 
         e.printStackTrace(); 
         Log.d("Error"," and "+String.valueOf(response)); 
       } 
       } 
       adapter.notifyDataSetChanged(); 
      } 
     }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
        error.printStackTrace(); 
      } 
     }); 


     Singleton.getinstance().addtorequest(jsonArrayRequest); 


     //System.out.println("vvvvvvvvvvvvvvvvvv"+t.getText()); 
    }  

和JSON甲是

[{"Vedio_Id":"16","Vedio_Title":"A touching commercial that rubs the deepest p","Title_Image":"192.168.43.2\/Library\/pimages\/download (5).jpg","Description":"2011 commercial by MOE (Ministry of Education","Link":"https:\/\/www.youtube.com\/watch?v=UIun5xGK86g","Datetime":"2017-08-09 16:58:23","S_category_Id":"1"}][{"Vedio_Id":"16","Vedio_Title":"A touching commercial that rubs the deepest p","Title_Image":"192.168.43.2\/Library\/pimages\/download (5).jpg","Description":"2011 commercial by MOE (Ministry of Education","Link":"https:\/\/www.youtube.com\/watch?v=UIun5xGK86g","Datetime":"2017-08-09 16:58:23","S_category_Id":"1"},{"Vedio_Id":"17","Vedio_Title":"Suli Breaks - Why I Hate School But Love Educ","Title_Image":"images (13).jpeg","Description":"Get tickets to my LIVE SHOW at THE ROCK TOWER","Link":"https:\/\/www.youtube.com\/watch?v=y_ZmM7zPLyI","Datetime":"2017-07-17 16:06:39","S_category_Id":"1"}][{"Vedio_Id":"16","Vedio_Title":"A touching commercial that rubs the deepest p","Title_Image":"192.168.43.2\/Library\/pimages\/download (5).jpg","Description":"2011 commercial by MOE (Ministry of Education","Link":"https:\/\/www.youtube.com\/watch?v=UIun5xGK86g","Datetime":"2017-08-09 16:58:23","S_category_Id":"1"},{"Vedio_Id":"17","Vedio_Title":"Suli Breaks - Why I Hate School But Love Educ","Title_Image":"images (13).jpeg","Description":"Get tickets to my LIVE SHOW at THE ROCK TOWER","Link":"https:\/\/www.youtube.com\/watch?v=y_ZmM7zPLyI","Datetime":"2017-07-17 16:06:39","S_category_Id":"1"},{"Vedio_Id":"18","Vedio_Title":"Funny children are just the best at entertain","Title_Image":"download (6).jpg","Description":"Children are awesome! These kids and toddlers","Link":"https:\/\/www.youtube.com\/watch?v=1-drtOAgH_o","Datetime":"2017-07-17 16:12:26","S_category_Id":"1"}][{"Vedio_Id":"16","Vedio_Title":"A touching commercial that rubs the deepest p","Title_Image":"192.168.43.2\/Library\/pimages\/download (5).jpg","Description":"2011 commercial by MOE (Ministry of Education","Link":"https:\/\/www.youtube.com\/watch?v=UIun5xGK86g","Datetime":"2017-08-09 16:58:23","S_category_Id":"1"},{"Vedio_Id":"17","Vedio_Title":"Suli Breaks - Why I Hate School But Love Educ","Title_Image":"images (13).jpeg","Description":"Get tickets to my LIVE SHOW at THE ROCK TOWER","Link":"https:\/\/www.youtube.com\/watch?v=y_ZmM7zPLyI","Datetime":"2017-07-17 16:06:39","S_category_Id":"1"},{"Vedio_Id":"18","Vedio_Title":"Funny children are just the best at entertain","Title_Image":"download (6).jpg","Description":"Children are awesome! These kids and toddlers","Link":"https:\/\/www.youtube.com\/watch?v=1-drtOAgH_o","Datetime":"2017-07-17 16:12:26","S_category_Id":"1"},{"Vedio_Id":"19","Vedio_Title":"TRY NOT TO LAUGH or GRIN Funny Fails Vines Co","Title_Image":"images (17).jpeg","Description":"This year is just flying by! This month's bes","Link":"https:\/\/www.youtube.com\/watch?v=no0u9MeK1XY","Datetime":"2017-07-19 11:11:17","S_category_Id":"5"}][{"Vedio_Id":"16","Vedio_Title":"A touching commercial that rubs the deepest p","Title_Image":"192.168.43.2\/Library\/pimages\/download (5).jpg","Description":"2011 commercial by MOE (Ministry of Education","Link":"https:\/\/www.youtube.com\/watch?v=UIun5xGK86g","Datetime":"2017-08-09 16:58:23","S_category_Id":"1"},{"Vedio_Id":"17","Vedio_Title":"Suli Breaks - Why I Hate School But Love Educ","Title_Image":"images (13).jpeg","Description":"Get tickets to my LIVE SHOW at THE ROCK TOWER","Link":"https:\/\/www.youtube.com\/watch?v=y_ZmM7zPLyI","Datetime":"2017-07-17 16:06:39","S_category_Id":"1"},{"Vedio_Id":"18","Vedio_Title":"Funny children are just the best at entertain","Title_Image":"download (6).jpg","Description":"Children are awesome! These kids and toddlers","Link":"https:\/\/www.youtube.com\/watch?v=1-drtOAgH_o","Datetime":"2017-07-17 16:12:26","S_category_Id":"1"},{"Vedio_Id":"19","Vedio_Title":"TRY NOT TO LAUGH or GRIN Funny Fails Vines Co","Title_Image":"images (17).jpeg","Description":"This year is just flying by! This month's bes","Link":"https:\/\/www.youtube.com\/watch?v=no0u9MeK1XY","Datetime":"2017-07-19 11:11:17","S_category_Id":"5"},{"Vedio_Id":"20","Vedio_Title":"Funny videos 2016 - funny videos 2016 funny v","Title_Image":"images (15).jpeg","Description":"funniest video ever you will die of laughter,","Link":"https:\/\/www.youtube.com\/watch?v=CZB9vLWyHtQ","Datetime":"2017-07-19 11:11:17","S_category_Id":"5"}] 

和PHP代碼是

<?php 

include('Connection.php'); 
$sql = "select * from vedio"; 
    $result = mysqli_query($con,$sql); 
     $response=array(); 
     if(mysqli_num_rows($result)>0){ 
     while($row=mysqli_fetch_assoc($result)){ 
      $response[]=$row;  
      echo json_encode($response); 
     } 

} 
else{ 

echo mysqli_error($con); 
} 
?> 

和響應的對象僅檢索

[{"Description":"2011 commercial by MOE (Ministry of `Education","Title_Image":"http:\/\/192.168.43.2\/Library\/pimages\/asd.jpg","Link":"https:\/\/www.youtube.com\/watch?v=UIun5xGK86g","Vedio_Title":"A touching commercial that rubs the deepest p","S_category_Id":"1","Vedio_Id":"16","Datetime":"2017-08-09 17:38:36"}]` 

回答

0

你是在每次讀取下一行時寫入數組。試試這個:

while($row=mysqli_fetch_assoc($result)) 
{ 
    array_push( $response,$row);      
} 
    echo json_encode($response); 
+0

AFAIK在陣列'$響應使用空索引[] = ...'不會覆蓋它,它會增加元件到它 – Yazan

+0

@Yazan只需將json_encode($響應)總分while循環。 – deejay

+0

@deejay是的,這是在我的答案:) – Yazan

0

只是把echo json_encode($response);跳出while循環 的,因爲你打印第一循環的反應,這就是服務器發回給你發送請求。

while($row=mysqli_fetch_assoc($result)){ 
    $response[]=$row;  
} 
echo json_encode($response); 

編碼並在循環完成所有迭代時發送響應。

相關問題