Android volley does not calling getParams in POST request
Asked Answered
S

2

0

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();
    }
}
Sobersided answered 14/7, 2017 at 14:57 Comment(2)
Your request seems to be OK, I can't see anything wrong with it. You should debug the code in your server to know why it's returning a null array in the response.Cheapen
but my response does not have a null array. It contains fetched data from DB. I used postman to verify the result...Sobersided
S
0

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.

Sobersided answered 19/7, 2017 at 14:25 Comment(0)
B
0

try using this

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

maybe this will help

Brasil answered 17/7, 2017 at 12:1 Comment(0)
S
0

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.

Sobersided answered 19/7, 2017 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.