mercredi 6 juillet 2016

Android : Sending form-data as body Google Volley

In my Android application, I am using google's volley for network operations. There is a case where I need to make a request but send body as form-data. I have tried everything else, but I am not able to make the request as form-data.

Here is a curl

curl -X POST -H "Content-Type: multipart/form-data" -F "mobile_number=<XXXX>" "<server_url>"

How can I achieve that -F part in volley? The Server is throwing bad request.

This is what I have done :

final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, URLFactory.OTP_URL,
                null, listener, errorListener){

            @Override
            public byte[] getBody() {
                final JSONObject  jsonObject = new JSONObject();
                try {
                    jsonObject.put("mobile_number", mobileNumber);
                } catch (JSONException e) {
                    e.printStackTrace();
                    return null;
                }
                return jsonObject.toString().getBytes();
            }


            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                final HashMap<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "multipart/form-data");
                return headers;
            }
        };

Please help me in this.

Aucun commentaire:

Enregistrer un commentaire