How to parse multiple rows with jsonOject

前端 未结 3 1909
有刺的猬
有刺的猬 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:37

    You aren't getting just one row, you're creating a new ArrayList through every iteration. You'll only ever get the last row this way.

    ArrayList> wordList;
                           wordList = new ArrayList>();
    

    You need to initialize your list outside your for loop.

    ArrayList> wordList = new ArrayList>();
    for (int i = 0; i < response.length; i++) {
    
        // add your information here
    }
    

    Edit::

    I see what's happening. You're looping for the length of String. You need to convert your String response to a JSONArray and loop through that. This way you step through each JSONObject in the array

            List> wordList = new 
                ArrayList>();
            try {
               JSONArray array = new JSONArray(response);
               for (int i = 0; i < array.length(); i++) {
                    JSONObject jsonObject = array.getJSONObject(i);
                    // get user // do rest of work
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
    

    Edit 2::

    This should get you going. I haven't tested, so watch out for any missing brackets.

        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());
                        try {
                            JSONArray jsonArray = new JSONArray(response);
                            if (jsonArray != null && !jsonArray.length() > 0) {
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                                    if (!jsonObject.isNull("user")) {
                                        JSONObject user = jsonObject.getJSONObject("user");
                                        String paid_amount = user.getString("paid_amount");
                                        String parking_duration =
                                                user.getString("parking_duration");
                                        String parking_name = user.getString("parking_name");
                                        HashMap 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);
                                    }
                                }
                            }
                        } catch (JSONException e) {
                            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);
    }
    

    Edit 3::

    if ($usersArr != false) { 
        foreach($usersArr as $user){
            $subresponse["error"] = FALSE; 
            $subresponse["user"]["paid_amount"] = $user["paid_amount"];
            $subresponse["user"]["parking_duration"] = $user["parking_duration"]; 
            $subresponse["user"]["parking_name"] = $user["parking_name"];
            $response[] = $subresponse;
            $json = json_encode($response);
    

    how I think it should look

      if ($usersArr != false) { 
           foreach($usersArr as $user){
               $subresponse["error"] = FALSE; 
               $subresponse["user"]["paid_amount"] = $user["paid_amount"];
               $subresponse["user"]["parking_duration"] = $user["parking_duration"]; 
               $subresponse["user"]["parking_name"] = $user["parking_name"];
               $response[] = $subresponse;
          } 
          $json = json_encode($response);
    

提交回复
热议问题