2
votes

I'm using this simple code :

JsonObjectRequest req = new JsonObjectRequest(Request.Method.PUT, url, null, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        delegate.postNotificationSucced();
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        VolleyLog.d(TAG, "Error: " + error.getMessage());
        Log.d(TAG, ""+error.getMessage()+","+error.toString());
        delegate.postNotificationFailed((APIErrors) error);
    }
}){
    @Override
    public Map<String, String> getParams() throws AuthFailureError {
        Map<String,String> headers = new HashMap<String, String>();
        headers.put("Content-Type","application/x-www-form-urlencoded");
        headers.put("state", Boolean.toString(isChecked));
        return headers;
    }

    @Override
    protected VolleyError parseNetworkError(VolleyError volleyError){
        APIErrors error = null;

        if(volleyError.networkResponse != null && volleyError.networkResponse.data != null){
            error = new APIErrors(new String(volleyError.networkResponse.data));
        }
        return error;
    }
};

When I use the Request.Method.GET or POST instead of PUT, the getParams() method is called but not with the PUT method. My problem is that my API requires a PUT method.

How can I pass my parameters using the PUT method.

Edit :

With the PUT method the getHeaders() method is called, so can I pass my arguments through it ?

Solution :

In my case I solved the problem changing my JsonObjecRequest to StringRequest like suggested by @ishmaelMakitla. And now I pass inside getParams().

I noticed that you are not using the response object for anything - so perhaps change the request from JsonObjectRequest to a StringRequest - remember that you then need to change Response.Listener() to Response.Listener() and the onResponse(JSONObject response) to onResponse(String response)....shout if you need further help. I hope this leads to some workable solution.

The reason is that with JsonObjectRequest, parameters must be pass through the third parameters of the constructor

jsonRequest - A JSONObject to post with the request. Null is allowed and indicates no parameters will be posted along with request.

you can read the doc for more informations.

1
I noticed that you are not using the response object for anything - so perhaps change the request from JsonObjectRequest to a StringRequest - remember that you then need to change Response.Listener<JSONObject>() to Response.Listener<String>() and the onResponse(JSONObject response) to onResponse(String response)....shout if you need further help. I hope this leads to some workable solution.ishmaelMakitla
Thanks for the remark @ishmaelMakitla !panic
Cool - by the way, I have created a quick Gist for you that you can "plug-and-play" in your code and let me know if it helps. The Gist is here - you can use the code in the Gist to replace your code above (maybe comment it out) - test it and let me know if it helps.ishmaelMakitla
You previous comment my problem with JsonObjectRequest we must pass our parameter with the third argument of the JsonObjectRequest constructor, with StringRequest the getParams method is callpanic
Please explain what you mean by "we must pass our parameter with the third argument of the JsonObjectRequest constructor" - does changing to StringReuest solve your getParams problem?ishmaelMakitla

1 Answers

2
votes

Since you are not using the response object for anything - you should change the request from JsonObjectRequest to a StringRequest - remember that you then need to change Response.Listener<JSONObject>() to Response.Listener<String>() and the onResponse(JSONObject response) to onResponse(String response).... here is StringRequest Gist I created - you can simply "plug-and-play" in your code and replace the JsonObjectRequest code.