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.
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.
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.
but, It is impossible to send a file as a null with calling addFilePart Exception is threw when an instance of FilePart is created.
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.
I think it's necessary for you to check that a file is null when creating an instance of FilePart or before making it.