我想通過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);
}
致謝!對於輸入它發送有效的JSON(通過jsonlint.com測試),但在應用程序中,我仍然可以看到單個輸出:( –
然後,只有一行'customerEmail ='「。$ customerEmail。」'「' – RiggsFolly
我在DB中檢查了81行 –