How to parse multiple rows with jsonOject

前端 未结 3 1907
有刺的猬
有刺的猬 2021-01-23 15:49

I am fetching data from mysql using php API and i pass results to android with Json_encode but when i populate results to android

3条回答
  •  温柔的废话
    2021-01-23 16:33

    This solution will help anyone who needds to fetch data from mysql using where clause based to android input with volley library .

    DBHandler which handles php functions :

                         .....
                   public function getDriverHistory($plate_no) {
                 $stmt = $this->conn->prepare("SELECT * from drivers_history 
               where  drivers_history.plate_no = ?");
    
              $stmt->bind_param("s", $plate_no);
               if ($stmt->execute()) {
    
               //  $user = $stmt->get_result()->fetch_assoc();       
               //  $stmt->close();
               // return $user;
    
             $result = $stmt->get_result();
              $usersArr = array();
             while ($user = $result->fetch_assoc()){
            $usersArr[] = $user;
             }
              $stmt->close();
             return $usersArr;
    
    
             } else {
            return NULL;
             }
             }
                 ....
    

    GetdriverFunction.php used to access function above :

                                          ...........
    
                FALSE); 
                if (isset($_POST['plate_no'])) {
                  // receiving the post params
                     $plate_no = $_POST['plate_no']; 
                      $usersArr = $db->getDriverHistory($plate_no);
                  if ($usersArr != false) {
                $response["error"]= FALSE;
              $response["user"] = $usersArr;
             echo json_encode($response); 
                  }
                 }
             ......
    

    finally Java part method to fetch all results where plate_no equals to something plate_no :

                  private void GetParkingInfo(final String plate_no) {
               // Tag used to cancel the request
                 String tag_string_req = "req_Verfication";
    
       progressBarList.setVisibility(View.VISIBLE);
           //        myList.setVisibility(View.GONE);
    
        StringRequest strReq = new StringRequest(Request.Method.POST,
            Urls.URL_driver_parking_information, new 
         Response.Listener() {
    
        @Override
        public void onResponse(String response) {
            Log.d(TAG, "cerfication Response: " + response.toString());             
            // Parsing json
            JSONArray jsonArrayResult ;
            for (int i = 0; i < response.length(); i++) {
    
                 try {
    
                     JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");
                    // Check for error node in json
                    if (!error) {
    
    
                        // user successfully exist in database
    
                          jsonArrayResult = jObj.getJSONArray("user");
    
                    for(int x=0; x prodHashMap = new 
                    HashMap();
    
                        prodHashMap.put("paid_amount", paid_amount);
                        prodHashMap.put("parking_duration", parking_duration);
                        prodHashMap.put("parking_name", parking_name);
    
                        ArrayList> wordList;
                           wordList = new ArrayList>();
                        wordList.add(prodHashMap);
                        userList = wordList ;
                        }
                        ShowListData();
    
                        progressBarList.setVisibility(View.GONE);
    
                         }
    
                      else{
    
                        // Error in login. Get the error message
              //                       // hiding the progress bar
                        progressBarList.setVisibility(View.GONE);
                         myList.setVisibility(View.VISIBLE);
                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(getApplicationContext(), errorMsg, 
               Toast.LENGTH_LONG).show();
    
                    }
    
                   } catch (JSONException e) {
                    // JSON error
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Json error: " + 
             e.getMessage(), Toast.LENGTH_LONG).show();
        //                     // hiding the progress bar
                    progressBarList.setVisibility(View.GONE);
                    myList.setVisibility(View.VISIBLE);
                   }
                }
            }
              }, new Response.ErrorListener() {
    
           @Override
           public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Verfication error Error: " + error.getMessage());
    
            Toast.makeText(getApplicationContext(),
                    "response error", Toast.LENGTH_LONG).show();
          //                Toast.makeText(getApplicationContext(),
        //                        error.getMessage(), Toast.LENGTH_LONG).show();
            // hiding the progress bar
            progressBarList.setVisibility(View.GONE);
            myList.setVisibility(View.VISIBLE);
            }
             }) {
            @Override
             protected Map getParams() {
    
            // Posting parameters to verfication url
            Map params = new HashMap();
            params.put("plate_no", plate_no);
            return params;
           }
        };
    //        // Adding request to request queue
    
        AppController.getInstance().addToRequestQueue(strReq,tag_string_req);
       }   
    

提交回复
热议问题