naver / volley-extensions

Volley Extensions v2.0.0. ( Volleyer, Volley requests, Volley caches, Volley custom views )
134 stars 34 forks source link

Exception in Multipart Request with Nullable File #6

Open phangji opened 9 years ago

phangji commented 9 years ago

Hello. when I send a multipart request, I can't send a file as null with addFilePart. In my case, user can select to attach a photo or without a photo. so I initiate a file variable as a null first. and then, if the user set a image, the file become not null. If the user doesn't select a image, the file is still null. So my ideal code is down there.

File file = null;
// If user attach a image, the file become not null.
volleyer(requestQueue)
                        .post(url)
                        .addHeader("Authorization", authorizationKey)
                        .addFilePart("file", file)
                        .withListener(responseListener)
                        .withErrorListener(errorListener)
                        .execute();

but, It is impossible to send a file as a null with calling addFilePart Exception is threw when an instance of FilePart is created.

screen shot 2015-07-30 at 11 51 32 am If file is null, file.getName() can't be called. So exception is threw

So I had to write a code down there. I had to call addFilePart when I have a file which is not null.

// working code.
File file = null;
// if user attach a image, the file become not null.
if(file == null) {
                volleyer(requestQueue)
                        .post(url)
                        .addHeader("Authorization", authorizationKey)
                        .withListener(responseListener)
                        .withErrorListener(errorListener)
                        .execute();
            } else {
                volleyer(requestQueue)
                        .post(url)
                        .addHeader("Authorization", authorizationKey)
                        .addFilePart("file", file)  
                        .withListener(responseListener)
                        .withErrorListener(errorListener)
                        .execute();
            }

I think it's necessary for you to check that a file is null when creating an instance of FilePart or before making it.

ncoolz commented 9 years ago

Thanks for an issue. Volleyer should have a strategy that skips the upload if file is null. I will fix this bug. :)

ISKU commented 8 years ago

quick fix to this problem. please