0

I want to send multipart request so that image can be sent to the server through volley. I want to send two other paramters along with image list but i don't know how to pass images list in volley request params. My code for getting image from gallery and storing their paths in array list is:

List<String> imagePathList = imageFilePaths;
        List<MultipartBody.Part> partMap = new ArrayList<>();
        for (int i = 0; i < imagePathList.size(); i++) {
            Uri fileUri = Uri.parse(imagePathList.get(i));
            RequestBody requestFile = RequestBody.create(
                    MediaType.parse(getMimeTypee(FileUtils.getFile(getContext(), fileUri).getAbsolutePath())),
                    FileUtils.getFile(getContext(), fileUri)
            );

           MultipartBody.Part body = MultipartBody.Part.createFormData("court_image[" + i + "]", FileUtils.getFile(getContext(), fileUri).getName(), requestFile);
           partMap.add(body);
        }

where imageFilePaths is an array list. The server will receive images like court_image[0], court_image[1] and so on, depends on how many image paths i have in arraylist.

The volley request is here:

 RequestQueue queue = Volley.newRequestQueue(getContext());
        StringRequest postRequest = new StringRequest(Request.Method.POST, url1,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(mBaseAppCompatActivity, "Success", Toast.LENGTH_SHORT).show();
                   }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }
        ) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                String token = getToken();
                params.put("Authorization", "Bearer " + token);
                params.put("Content-Type", "multipart/form-data");
                return params;
            }

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("terms", "true");
                params.put("phone", "phoneNo");
                return params;
            }

        };
        queue.add(postRequest);

Now the thing is as I am new to the multi-part thing, with the help I am able to get the image from gallery and storing their path in ArrayList but I don't know how to pass the multipart data in this volley request. Please Help

Aarks
  • 117
  • 9
  • Please don't repeat questions. Simply editing your original post with any new information you have, any new code you've tried, or an explanation of why any posted answers aren't working, will bump it to the top of the active queue. – Mike M. Oct 18 '18 at 01:21
  • Ok thankyou for advice. I will keep this in mind :) – Aarks Oct 18 '18 at 05:42

0 Answers0