Has anyone been able to accomplish sending a multipart/form-data
POST in Android with Volley yet? I have had no success trying to upload an image/png
using a POST request to our server and am curious if anyone has.
I believe the default way to do this would be to override public byte[] getPostBody()
in the Request.java
class and attach the File
there with a blank Header key for the boundary. However, converting my file to a String
for the Map<String, String> postParams
and then having it encoded again seems obtuse and not really elegant. Also I've been unsuccessful in my attempts. This is really the only thing holding us back from switching to this library.
Anyway, all thoughts and answers are extremely appreciated. Thank you for your help.
UPDATE 2015/08/26:
If you don't want to use deprecated HttpEntity, here is my working sample code (tested with ASP.Net WebAPI)
MultipartActivity.java
BaseVolleyRequest.java:
END OF UPDATE
This is my working sample code (only tested with small-size files):
I might be wrong on this but I think you need to implement your own
com.android.volley.toolbox.HttpStack
for this because the default ones (HurlStack
if version > Gingerbread orHttpClientStack
) don't deal withmultipart/form-data
.Edit:
And indeed I was wrong. I was able to do it using
MultipartEntity
in Request like this:It's pretty raw but I tried it with an image and a simple string and it works. The response is a placeholder, doesn't make much sense to return a Response String in this case. I had problems using apache httpmime to use MultipartEntity so I used this https://code.google.com/p/httpclientandroidlib/ don't know if there's a better way. Hope it helps.
Edit
You can use httpmime without using httpclientandroidlib, the only dependency is httpcore.
Here is Simple Solution And Complete Example for Uploading File Using Volley Android
1) Gradle Import
2)Now Create a Class RequestManager
3)Now Create a Class to handle Request for uploading File WebService
4) And Now Call The method Like This to Hit the service