朋友。我的带有参数的截击POST请求有问题。我需要从远程MySQL数据库中获取数据并将其显示在recycler视图中。为此,我使用了凌空网络请求。没有参数,它会加载我的数据库中的所有数据。但是当我添加getParams()方法时,它返回一个空数组,即使根据传递的参数有相应的DB条目可用。这是我的代码..
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();
}
}
答案 0 :(得分:0)
尝试使用此
JSONObject jObj = new JSONObject(response)
JSONArray area = jObj.getJSONArray("");
也许这会有所帮助
答案 1 :(得分: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.