2

I am using volley for simple REST request and passing some post parameters in it. But It always giving response BasicNetwork.performRequest: Unexpected response code 400.

I have tried following custom request.

   public class GsonRequestRest<T> extends Request<T>{

   private static final String PROTOCOL_CHARSET = "utf-8";
   private static final String PROTOCOL_CONTENT_TYPE = String.format("application/json; charset=%s", PROTOCOL_CHARSET);
   private final Listener<T> mListener;
   private final String mRequestBody;
   private Gson mGson;
   private Class<T> mJavaClass;
   private Map<String, String> params;
   public GsonRequestLoginRest(int method, String url, Class<T> cls, String requestBody, Map<String, String> params,Listener<T> listener,ErrorListener errorListener) {
        super(method, url, errorListener);
        mGson = new Gson();
        mJavaClass = cls;
        mListener = listener;
        mRequestBody = requestBody;
        this.params = params;
   }
   @Override
   protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
        return params;
   };
   @Override
   protected void deliverResponse(T response) {
        mListener.onResponse(response);
   }
   private Map<String, String> headers = new HashMap<String, String>();
   @Override
   public Map<String, String> getHeaders() throws AuthFailureError {
        return headers;
   }
    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            T parsedGSON = mGson.fromJson(jsonString, mJavaClass);

            return Response.success(parsedGSON,HttpHeaderParser.parseCacheHeaders(response));            
        } catch (IOException e) {
                Log.d("tag", e.getMessage());
                return Response.error(new ParseError(e));
            } catch (JsonSyntaxException je) {
                return Response.error(new ParseError(je));
            }
    }
    @Override
    public String getBodyContentType() {
        return PROTOCOL_CONTENT_TYPE;
    }
    @Override
    public byte[] getBody() {
        try {
            return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
        } catch (UnsupportedEncodingException uee) {
            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",mRequestBody, PROTOCOL_CHARSET);
            return null;
        }
    }
    //In your extended request class
    @Override
    protected VolleyError parseNetworkError(VolleyError volleyError){
        if(volleyError.networkResponse != null && volleyError.networkResponse.data != null){
            VolleyError error = new VolleyError(new String(volleyError.networkResponse.data));
            volleyError = error;
        }
        return volleyError;
    }
}

I did try to override getParams() and pass parameters in it. But it was not passing parameters and throwing error 400. I tried many of suggestions here and here but nothing worked for me. Is there any bug in Volley. ?

Please guide.

Community
  • 1
  • 1
Dory
  • 7,462
  • 7
  • 33
  • 55
  • make sure you are sending all mandatory params to the sever. You got 400 ie. BAD_REQUEST, means missing something which is necessary for that request. – Bharatesh Jul 24 '15 at 06:00
  • I am sending all required params to server, but they are not passed to server using `getParams()` nor in `request body`. Volley is not sending params. – Dory Jul 24 '15 at 06:41
  • If I am not wrong getParams() executes only for `POST` Request. – Bharatesh Jul 24 '15 at 06:55
  • I am requesting a `POST` request only. – Dory Jul 24 '15 at 08:12

1 Answers1

0

it working for me when use getBody() instead of getParams() in Request class.

another thing you should use JSONObject instead of HashMap to put your params

. then convert the JSONObject to string and convert string to byte in the getBody method.

use this link for more explanation. How to send a POST request using volley with string body?

Hope my answer clarified enough to you.

Community
  • 1
  • 1
Mina Samir
  • 1,527
  • 14
  • 15