1

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);
James Z
  • 12,209
  • 10
  • 24
  • 44

2 Answers2

1

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

Muhammed Fasil
  • 7,909
  • 2
  • 19
  • 28
0

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 :)

Ronald Saunfe
  • 561
  • 6
  • 20