2016-09-05 81 views
0

我想通過JSON將我下面提到的php文件輸出到我的android應用程序中,但我只獲取了我的android應用程序中的第一個條目。做下面的步驟,那麼我可以,但我就能得到的只有第一輸出,但輸出的其餘部分是不是在我的應用程序如何通過使用Volley從MySQL通過JSON獲取多行數據

<?php 
    define('HOST','XXXX'); 
    define('USER','XXXX'); 
    define('PASS','XXXX'); 
    define('DB','XXXX'); 
    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect'); 
    if($_SERVER['REQUEST_METHOD']=='GET'){ 
     $customerEmail = $_GET['customerEmail']; 
     $sql = "SELECT `amount`, `customerEmail`, `CCAvenueOrder_id` FROM `     OrderAborted` WHERE customerEmail='".$customerEmail."'"; 
     $r = mysqli_query($con,$sql); 
     while($res = mysqli_fetch_array($r)) 
     { 
      $result = array(); 
      array_push($result,array(
          "customerEmail"=>$res['customerEmail'], 
          "CCAvenueOrder_id"=>$res['CCAvenueOrder_id'], 
          "amount"=>$res['amount'] 
          ) 
      ); 
      echo json_encode(array("result"=>$result)); 
     } 
     mysqli_close($con); 
    } 
?> 

JSON代碼可見

 private void getData() { 
    String id = editTextId.getText().toString().trim(); 
    if (id.equals("")) { 
     Toast.makeText(this, "Please enter an id",   Toast.LENGTH_LONG).show(); 
     return; 
    } 
    loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false); 

    String url = Constants.DATA_URL+editTextId.getText().toString().trim(); 

    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      loading.dismiss(); 
      showJSON(response); 
     } 
       }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(OrderHistory.this,error.getMessage().toString(),Toast.LENGTH_LONG).show(); 
       } 
      }); 

    RequestQueue requestQueue = Volley.newRequestQueue(this); 
    requestQueue.add(stringRequest); 
} 

private void showJSON(String response){ 
    String name=""; 
    String address=""; 
    String vc = ""; 
    try { 
     JSONObject jsonObject = new JSONObject(response); 
     JSONArray result = jsonObject.getJSONArray(Constants.JSON_ARRAY); 
     JSONObject collegeData = result.getJSONObject(0); 
     name = collegeData.getString(Constants.KEY_NAME); 
     address = collegeData.getString(Constants.KEY_ADDRESS); 
     vc = collegeData.getString(Constants.KEY_VC); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    textViewResult.setText("Name:\t"+name+"\nAddress:\t" +address+ "\nVice  Chancellor:\t"+ vc); 
    } 

回答

1

很簡單不,直到你已經完成了循環發送數據,並移動數組初始化外循環也

<?php 
    define('HOST','XXXX'); 
    define('USER','XXXX'); 
    define('PASS','XXXX'); 
    define('DB','XXXX'); 
    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect'); 
    if($_SERVER['REQUEST_METHOD']=='GET'){ 
     $customerEmail = $_GET['customerEmail']; 
     $sql = "SELECT `amount`, `customerEmail`, `CCAvenueOrder_id` FROM `OrderAborted` WHERE customerEmail='".$customerEmail."'"; 
     $r = mysqli_query($con,$sql); 

     $result = array(); 
     while($res = mysqli_fetch_array($r)) 
     { 
      //$result = array(); 
      $result[] = array(
          "customerEmail"=>$res['customerEmail'], 
          "CCAvenueOrder_id"=>$res['CCAvenueOrder_id'], 
          "amount"=>$res['amount'] 
          ); 
      //echo json_encode(array("result"=>$result)); 
     } 

     echo json_encode(array("result"=>$result)); 
     mysqli_close($con); 
    } 
?> 

您還可以簡化環路以及你得到完全陣列您手動構建從結果集,如果使用mysqli_fetch_assoc()代替mysqli_fetch_array()

<?php 
    define('HOST','XXXX'); 
    define('USER','XXXX'); 
    define('PASS','XXXX'); 
    define('DB','XXXX'); 
    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect'); 
    if($_SERVER['REQUEST_METHOD']=='GET'){ 
     $customerEmail = $_GET['customerEmail']; 
     $sql = "SELECT `amount`, `customerEmail`, `CCAvenueOrder_id` FROM `OrderAborted` WHERE customerEmail='".$customerEmail."'"; 
     $r = mysqli_query($con,$sql); 

     $result = array(); 
     while($res = mysqli_fetch_assoc($r)) 
     { 
      $result[] = $res; 
      //echo json_encode(array("result"=>$result)); 
     } 

     echo json_encode(array("result"=>$result)); 
     mysqli_close($con); 
    } 
?> 

即使再次simpify,如果你使用mysqli_fetch_all()

<?php 
    define('HOST','XXXX'); 
    define('USER','XXXX'); 
    define('PASS','XXXX'); 
    define('DB','XXXX'); 
    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect'); 
    if($_SERVER['REQUEST_METHOD']=='GET'){ 
     $customerEmail = $_GET['customerEmail']; 
     $sql = "SELECT `amount`, `customerEmail`, `CCAvenueOrder_id` FROM `OrderAborted` WHERE customerEmail='".$customerEmail."'"; 
     $r = mysqli_query($con,$sql); 

     $result = mysqli_fetch_all($r, MYSQLI_ASSOC); 

     echo json_encode(array("result"=>$result)); 
    } 
?> 

而且擺脫了SQL注入矢量

<?php 
    define('HOST','XXXX'); 
    define('USER','XXXX'); 
    define('PASS','XXXX'); 
    define('DB','XXXX'); 
    $con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect'); 
    if($_SERVER['REQUEST_METHOD']=='GET'){ 
     //$customerEmail = $_GET['customerEmail']; 

     $sql = "SELECT `amount`, `customerEmail`, `CCAvenueOrder_id` 
       FROM `OrderAborted` 
       WHERE customerEmail=?"; 

     $stmt = mysqli_prepare($con,$sql); 

     $stmt = mysqli_stmt_bind_param ($stmt, 's', $_GET['customerEmail']); 

     $status = mysqli_execute($stmt); 

     $r = mysqli_stmt_get_result ($stmt); 

     $result = mysqli_fetch_all($r, MYSQLI_ASSOC); 

     echo json_encode(array("result"=>$result)); 
    } 
?> 
+0

致謝!對於輸入它發送有效的JSON(通過jsonlint.com測試),但在應用程序中,我仍然可以看到單個輸出:( –

+0

然後,只有一行'customerEmail ='「。$ customerEmail。」'「' – RiggsFolly

+0

我在DB中檢查了81行 –

0

定義$result = array();外而循環。