具有冗余值的PHP解析错误JSON异常 - 无法获取数据通信

时间:2016-06-01 14:38:33

标签: java php json android-volley

我正在从我的服务器(XAMPP)获取数据,而且我对返回格式有疑问(我相信)。我收到了我的数据并将其放入cardView - recyclerView

这是我的php代码:orgList.php

<?php

//if($_SERVER['REQUEST_METHOD'] == ''){


     require('dbConnect.php');

     $SQLi_ORG_FEEDS = "SELECT organizationName,organizationDescription,organizationCategory,
                        organizationCurrentMembers,organizationMaxMembersNo,organizationType FROM org_information";

     $query = mysqli_query($dbConnect,$SQLi_ORG_FEEDS) or die("Error".mysqli_error($dbConnect));

     $checkRow = mysqli_num_rows($query);

     $response = array();
     $response["success"] = "success";
     if($checkRow > 0){
         while ($getRecord = mysqli_fetch_array($query)) {
            $response[] = $getRecord;

         }

          echo json_encode($response);
     }
     else {
         $response['failed'] = 'failed';
          echo json_encode($response);
     }

//} 

我的java:orgFragment.java

 public void parseJSONData(){
    RequestQueue requestQueue = Volley.newRequestQueue(getContext());
    JsonArrayRequest jsonArray = new JsonArrayRequest(ServerScripts.PHP_SCRIPT_PATH + ServerScripts.PHP_GET_FEEDS, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            if (response.length() > 0){
                try {
                    organizationDataList.clear();
                    for (int i = 0; i < response.length(); i++ ){
                        JSONObject jsonObject = response.getJSONObject(i);
                        OrganizationData organizationData = new OrganizationData();

                        //SQL TABLE NAME
                        if (!jsonObject.isNull("organizationName")){
                            organizationData.orgName = jsonObject.getString("organizationName");
                        }

                        if (!jsonObject.isNull("organizationType")){
                            organizationData.orgType = jsonObject.getString("organizationType");
                        }

                        if (!jsonObject.isNull("organizationDescription")){
                            organizationData.orgDesc = jsonObject.getString("organizationDescription");
                        }

                        if (!jsonObject.isNull("organizationCategory")){
                            organizationData.orgCategory = jsonObject.getString("organizationCategory");
                        }

                        if (!jsonObject.isNull("organizationCurrentMembers")){
                            organizationData.orgCurrentMembers = jsonObject.getInt("organizationCurrentMembers");
                        }

                        if (!jsonObject.isNull("organizationMaxMembersNo")){
                            organizationData.orgMaxMembers = jsonObject.getInt("organizationMaxMembersNo");
                        }

                        //ADD
                        organizationDataList.add(organizationData);
                    }
                        //NOTIFY
                        orgListAdapter.notifyDataSetChanged();

                }catch (Exception e){
                    e.printStackTrace();
                    Toast.makeText(getContext(),e.toString(),Toast.LENGTH_LONG).show();
                }
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            String getErrorMsg = error.toString();
            Toast.makeText(getContext(),"Failed to Fetch Data" + getErrorMsg,Toast.LENGTH_LONG).show();
        }

    });
    requestQueue.add(jsonArray);

}

Postman

如果在json中返回,我会在postman中检查我的格式。我得到了这个回应。有人可以帮我这个PHP编码。我在哪里出错JSON格式。

1 个答案:

答案 0 :(得分:1)

这是错误的:

      echo json_encode($response);
 }
 else {
      echo $response["failed"] = "failed";
      ^^^^^^^^

您的客户期待json。如果执行else子句,则脚本输出failed,这是无效/非法的json,这将导致解析错误。你可能想要更像

的东西
 $response['failed'] = 'failed';
 echo json_encode($response);

这也是错误的:

     while ($getRecord = mysqli_fetch_array($query)) {
                ^-- one row of result data as an array
        $response["organizationName"] = $getRecord;
                                             ^--stuff entire row array into response

您还从数据库中获取MULTIPLE记录(否则,为什么还要在循环中获取),并不断将整个结果行数组分配到响应的多个字段中,然后用下一行覆盖之前的结果。这意味着您只能从查询中获取LAST记录。

你想:

     while ($getRecord = mysqli_fetch_array($query)) {
        $response["organizationName"][] = $getRecord['organizationName'];
                                     ^-append to array
       etc..
     }

相反,或者只是

     while ($getRecord = mysqli_fetch_array($query)) {
        $response[] = $getRecord;
     }