如何使用Volley从mysql到JSON获取多行数据

时间:2016-09-05 13:28:39

标签: php android mysql json android-volley

我试图通过JSON将我下面提到的php文件的输出到我的Android应用程序中,但我只得到我的Android应用程序中的第一个条目。我尝试了以下步骤,我可以但我能够只得到第一个输出,但输出的其余部分在我的应用程序中不可见

<?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);
   }

2 个答案:

答案 0 :(得分: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); 
    }
?>

如果使用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));
    }
?>

答案 1 :(得分:0)

在while循环之外定义In [21]: df['is_weekday'] = np.where(df['weekday'] >= 5, 0, df['weekday'] + 1) df Out[21]: dates weekday is_weekday 0 2016-01-01 4 5 1 2016-01-02 5 0 2 2016-01-03 6 0 3 2016-01-04 0 1 4 2016-01-05 1 2 5 2016-01-06 2 3 6 2016-01-07 3 4 7 2016-01-08 4 5 8 2016-01-09 5 0 9 2016-01-10 6 0