google / volley

https://google.github.io/volley
Apache License 2.0
3.38k stars 754 forks source link

post request headers and body #262

Closed damsojilo closed 5 years ago

damsojilo commented 5 years ago

Hi, i dont found how i can configure my headers and body in my request, can someone help with that please this is my code, i know getHeaders its for the headers but i dont know how i can set up my body `

        private RequestQueue mQueue;

      private void send() {
     String url = "http://127.0.0.1:8080/~/in-cse";

    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            //Success Callback
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            //Failure Callback
        }
    })
    {
        @Override
        public  Map getHeaders() throws AuthFailureError{
            Log.i("rentrer dedans","Headers");
            HashMap headers = new HashMap();
            headers.put("X-M2M-Origin", "admin:admin");
            headers.put("Content-Type", "application/xml;ty=2");
            return headers;
        }

        @Override
        public Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put("X-M2M-Origin", "admin:admin");
            params.put("Content-Type", "application/xml;ty=2");
            return params;
        }
    }
    ;
    mQueue.add(getRequest);
}`

thats my body i need to insert in my code :

`

app-sensor
<lbl>Type/sensor Category/temperature Location/home</lbl>
<rr>false</rr>

</m2m:ae>`

jpd236 commented 5 years ago

JsonObjectRequest generally expects a JSON request body. Your API is a bit of an outlier in that it has an XML body but (apparently?) a JSON response. Are you sure this is how the server works?

Anyway, you can override Request#getBody() and Request#getBodyContentType to set the request body.

In the future, I might suggest a general help forum like Stack Overflow or our user group at volley-users@googlegroups.com. This is a bug tracker meant for reporting issues with Volley rather than support requests.