简体   繁体   中英

Android volley does not calling getParams in POST request

friends. I have a problem with my volley POST request with parameters. I need fetch the data from my remote MySQL DB and display it in recycler view. For this, I have using volley network request. Without parameters, it loads all the data from my DB. But when I add getParams() method it returns a null array even though corresponding DB entries are available according to the passing parameters. Here is my code..

public void setDonorsList(final VolleyCallBack volleyCallBack) {
    if (Utility.isNetworkEnabled) {
        final ArrayList<Donor> listOfDonors = new ArrayList<>();
        final ProgressDialog progressDialog = new ProgressDialog(FindDonorResult.this);
        progressDialog.setMessage("Loading Donors List. Please wait");
        progressDialog.show();
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.GET_DONORS_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        progressDialog.dismiss();
                        try {
                            listOfDonors.clear();
                            JSONArray jsonArray = new JSONArray(response);
                            int count = 0;
                            while (count < 10) {
                                JSONObject jsonObject = jsonArray.getJSONObject(count);
                                Donor donor = new Donor(
                                        jsonObject.getString("fName"), jsonObject.getString("sName"), jsonObject.getString("emailid"),
                                        jsonObject.getString("pass"), jsonObject.getString("mobile"), jsonObject.getString("blood"),
                                        jsonObject.getString("age"), jsonObject.getString("gender"), jsonObject.getString("country"),
                                        jsonObject.getString("location"), jsonObject.getString("latitude"), jsonObject.getString("longitude"),
                                        jsonObject.getString("picname"), jsonObject.getString("pic")
                                );
                                int roundedDistance = (int) distance(Double.parseDouble(donor.getLatitude()),
                                        Double.parseDouble(latitude), Double.parseDouble(donor.getLongitude()),
                                        Double.parseDouble(longitude));
                                donor.setDistance(roundedDistance);
                                listOfDonors.add(donor);
                                count++;
                            }
                            Log.e("listOfDonors", String.valueOf(listOfDonors.size()));
                            volleyCallBack.onSuccess(listOfDonors);
                        } catch (JSONException e) {
                            e.getMessage();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.getMessage();
                    }
                }
        ){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("bloodGroup", "A+");
                return params;
            }
        };
        NetworkRequestSingleTon.getOurInstance(FindDonorResult.this).addToRequestQue(stringRequest);
    } else {
        Toast.makeText(FindDonorResult.this, "No active internet connection.", Toast.LENGTH_SHORT).show();
    }
}

try using this

 JSONObject jObj = new JSONObject(response)
JSONArray area = jObj.getJSONArray("");

maybe this will help

Finally, I figured it. The problem was in setDonorsList() method. I have hard coded the condition while(count < 10). This produces a JSON exception called "index out of range" since my DB doesn't have 10 entries. I changed the value like while(count < jsonArray.length()). Now everything is perfect. Guys thank you so much for your time and concern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM