Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Volley PUT request

i'm new to Volley and Android in general. Below is code snippet (Android using Volley) which i'm trying to execute, however it's the server returns a 400. Using another REST Client works perfectly. It's a request to the server using PUT method.

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    sendRequest();
}

private void sendRequest() {
    RequestQueue queue = Volley.newRequestQueue(this);

    final JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("password", "ttttt");
        jsonObject.put("username", "tester3");
        jsonObject.put("token", "blah");
    } catch (JSONException e) {
        // handle exception
    }


    JsonObjectRequest putRequest = new JsonObjectRequest(Request.Method.PUT, url, jsonObject,
            new Response.Listener<JSONObject>()
            {
                @Override
                public void onResponse(JSONObject response) {
                    // response
                    Log.d("Response", response.toString());
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error
                    Log.d("Error.Response", error.toString());
                }
            }
    ) {

        @Override
        public Map<String, String> getHeaders()
        {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            headers.put("Accept", "application/json");
            return headers;
        }

        @Override
        public byte[] getBody() {

            try {
                Log.i("json", jsonObject.toString());
                return jsonObject.toString().getBytes("UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return null;
        }
    };

    queue.add(putRequest);
}

When i execute this code i always get a 400 Bad request back and i can't figure out why. Using another client like Postman, it works as expected. Here is the postman request:

Raw Request:
{ "token": "blah", "password": "ttttt", "username": "tester3" }

Headers: Content-Type: application/json

I can't see anything wrong with the request i'm hoping someone can point out what i'm doing wrong?

like image 337
SteedsOfWar Avatar asked Nov 02 '14 07:11

SteedsOfWar


1 Answers

Sometimes , adding header "Content-Type", "application/json" in getHeaders() will not work its better to also override getBodyContentType() and return the header here.

So along with ,

public Map<String, String> getHeaders()
    {
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json"); 
        return headers;
    }

also add,

    @Override
    public String getBodyContentType() {
        return "application/json";
    }

This worked for me while using POST.

like image 188
Siddarth G Avatar answered Oct 27 '22 00:10

Siddarth G