user2145673 user2145673 - 2 years ago 2164
PHP Question

multiple file upload using retrofit 2.0

I am currently working on a part of the application where user need to choose some images from gallery and then upload those images to a server. for the upload i am using retrofit 2.0 using multipart.

Following the resource i managed to implement it successfully when using only one file, however i would like that on one session all images user chose to be uploaded to the server.

Looking around I found "Retrofit" multiple images attached in one multipart request where it discuss on @partmap annotation and it seems like the proper match however i am failing to understand how i will iterate over the images I need to upload?

can someone point me in to the right implementation?

Also checked:
1.Retrofit(2.0 beta2) Multipart file upload doesn't work
2.Upload multiple image with same name as array Retrofit 2.0.0-beta2

Answer Source

Since I didnt manage to resolve the issue using retrofit I ended up using okhttp library that gave me what i needed, here is an example of the code I use which I hope can help someone

build the request body:

                    okhttp3.RequestBody requestBody = new MultipartBody.Builder()
                        .addPart(okhttp3.Headers.of("Content-Disposition:", "form-data; name=\"myfile\"; filename=\"image" + ImageNumber + ".jpg\""),
                                okhttp3.RequestBody.create(MEDIA_TYPE_JPG, new File(ImagePath)))

prepare the request:

                         request = new okhttp3.Request.Builder()
                            .header("Authorization", "Client-ID " )
                            .url(YOUR URL)

initiate connection to server:

                    try {
                    okhttp3.Response response = client.newCall(request).execute();
                    if (!response.isSuccessful())
                        throw new IOException("Unexpected code " + response);
                } catch (IOException e) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download