1
votes

Handle service call using volley library. Its working fine for jsonObject, jsonArray and String request but my application have one service different request and response. In this service have post params are Jsonobject, and return response is String.

I am trying many solution to handle response.but no one working. I am new android application developer.

post param and response:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url, params, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject response) {

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }

}) {
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        return getHeader();
    }
};

jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48, 2, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
VolleyRequest.volleyRequestInstance(mContext).addToRequestQueue(jsonObjectRequest);
2
Did you try with StringRequest? - SripadRaj
Thanks for reply sripad. yes have try StringRequest but return server error - Saravanan.S
Similar ansewr to your solution stackoverflow.com/a/31638943/4653447 - Krunal Dave
If its a server error. Check at server side then :) - SripadRaj
if i check in postman working fine - Saravanan.S

2 Answers

1
votes

Volley Parse error will occur in different scenarios

  1. If you call post method as to get, and also get as post
  2. You have tried to pass different payloads
  3. Return values are different in types that you are expected

Use a custom request to solve this issue in normal cases example here:

https://developer.android.com/training/volley/request-custom

in your code.

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url, params, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}

}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {

     Map<String,String> myHeader=new HashMap<>();
     myHeader.put("Accept","text");
    return myHeader;
}
};

 jsonObjectRequest.setRetryPolicy(new 
 DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48, 
2,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
 VolleyRequest.volleyRequestInstance(mContext).addToRequestQueue
(jsonObjectRequest);

also needs your 'params' value for further clarification

0
votes

Check on the response the URL returns the problem might be not the code but the URL response. In my case Volley failed to parse my response because it did not understand the response from the URL. hope this answer will help someone good luck :)