0

I have registration page and in this page i have to send person image along with person details to my server using Volley library, the problem is I'm looking for a how to do it but still haven't found. can some one help me please

MainActivity:-

 JSONObject request = new JSONObject();
        try {
            request.put("username", "ramu");
            request.put("password", "12345");
            request.put("firstname", "rk");
            request.put("lastname", "ram");
             //Along with this parameters i have to send person image also
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String url = "MYURL";
    VolleyBackgroundCode.getJsonRequest(MainActivity.this, url, request.toString());

VolleyBackgroundCode:-

    public static void getJsonRequest(Context _Context, String url, final String body) {
                    RequestQueue queue = Volley.newRequestQueue(_Context.getApplicationContext());
                    StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {

                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                            return;
                        }
                    }) {

                        @Override
                        public Map<String, String> getHeaders() throws AuthFailureError {

                            HashMap<String, String> headers = new HashMap<String, String>();
                            return headers;
                        }

                        @Override
                        public byte[] getBody() {

                            return body.toString().getBytes();
                        }

                        @Override
                        public String getBodyContentType() {

                            return "application/json";
                        }

                        @Override
                        protected Response<String> parseNetworkResponse(NetworkResponse response) {
                            return super.parseNetworkResponse(response);
                        }
                    };

                    request.setRetryPolicy(new DefaultRetryPolicy(2000, 3, 1.5f));
                    queue.add(request);
                }
}
mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
AbhiRam
  • 2,033
  • 7
  • 41
  • 94

1 Answers1

0

Use Multipart Request

HashMap<String, String> params = new HashMap<String, String>();

String url = "YOUR POST URL";
String image_path = "your local image path";

params.put("your_extra_params", "value");

MultipartRequest multipartRequest =

        new MultipartRequest(url, params, image_path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.e(TAG, "Success Response: " + response.toString());

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

                if (error.networkResponse != null) {
                    Log.e(TAG, "Error Response code: " +
                            error.networkResponse.statusCode);

            }
        });

requestQueue.add(multipartRequest);
SaravInfern
  • 3,338
  • 1
  • 20
  • 44