Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send raw post request using volley

I want to send post request with raw string rather than setting params using volley.

I have tried to override the getBody method in StringRequest like following:

@Override
public byte[] getBody() throws AuthFailureError {
    return rawString.getBytes();
}

It won't even send the request and gives the error: com.android.volley.TimeoutError

Any help will be appreciated.

like image 345
user1097940 Avatar asked May 05 '15 15:05

user1097940


People also ask

How to make get request using Volley in Android?

Use newRequestQueue RequestQueue queue = Volley. newRequestQueue(this); String url = "https://www.google.com"; // Request a string response from the provided URL. StringRequest stringRequest = new StringRequest(Request.

What is getParams in Android?

Overriding the getParams method allows you to build the HashMap and return the object to the Volley request for posting. Similarly, if you need to add any headers to the request, you override the getHeaders method and build/return your key,value pairs in a HashMap there as well.


1 Answers

I got so ...

   RequestQueue queue = Volley.newRequestQueue(this);

    queue.add(myReq);

    ...

        StringRequest myReq = new StringRequest(Request.Method.POST,
                        server+"Login",
                        createMyReqSuccessListener(),
                        createMyReqErrorListener()) {

                    @Override
                    public byte[] getBody() throws com.android.volley.AuthFailureError {
                        String str = "{\"login\":\""+login+"\",\"password\":\""+pass+"\"}";
                        return str.getBytes();
                    };

                    public String getBodyContentType()
                    {
                        return "application/json; charset=utf-8";
                    }

                };

        ...

            private Response.Listener<String> createMyReqSuccessListener() {
                return new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.i(TAG,"Ski data from server - "+response);
                    }
                };
            }


            private Response.ErrorListener createMyReqErrorListener() {
                return new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.i(TAG,"Ski error connect - "+error);
                    }
                };
            }
like image 80
gc986 Avatar answered Sep 19 '22 13:09

gc986