Asked  7 Months ago    Answers:  5   Viewed   65 times

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 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.



I might be wrong on this but I think you need to implement your own for this because the default ones (HurlStack if version > Gingerbread or HttpClientStack) don't deal with multipart/form-data.


And indeed I was wrong. I was able to do it using MultipartEntity in Request like this:

public class MultipartRequest extends Request<String> {

    private MultipartEntity entity = new MultipartEntity();

    private static final String FILE_PART_NAME = "file";
    private static final String STRING_PART_NAME = "text";

    private final Response.Listener<String> mListener;
    private final File mFilePart;
    private final String mStringPart;

    public MultipartRequest(String url, Response.ErrorListener errorListener, Response.Listener<String> listener, File file, String stringPart)
        super(Method.POST, url, errorListener);

        mListener = listener;
        mFilePart = file;
        mStringPart = stringPart;

    private void buildMultipartEntity()
        entity.addPart(FILE_PART_NAME, new FileBody(mFilePart));
            entity.addPart(STRING_PART_NAME, new StringBody(mStringPart));
        catch (UnsupportedEncodingException e)

    public String getBodyContentType()
        return entity.getContentType().getValue();

    public byte[] getBody() throws AuthFailureError
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        catch (IOException e)
            VolleyLog.e("IOException writing to ByteArrayOutputStream");
        return bos.toByteArray();

    protected Response<String> parseNetworkResponse(NetworkResponse response)
        return Response.success("Uploaded", getCacheEntry());

    protected void deliverResponse(String response)

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 don't know if there's a better way. Hope it helps.


You can use httpmime without using httpclientandroidlib, the only dependency is httpcore.

Tuesday, June 1, 2021
answered 7 Months ago
$ git clone
$ cd volley
$ android update project -p .
$ ant jar

Then, copy bin/volley.jar into your libs/ folder and off you go!


Wednesday, June 9, 2021
answered 6 Months ago

You have to draw the circle view, and after that you should create an animation to it.

Creating the circle view:

public class Circle extends View {

    private static final int START_ANGLE_POINT = 90;

    private final Paint paint;
    private final RectF rect;

    private float angle;

    public Circle(Context context, AttributeSet attrs) {
        super(context, attrs);

        final int strokeWidth = 40;

        paint = new Paint();
        //Circle color

        //size 200x200 example
        rect = new RectF(strokeWidth, strokeWidth, 200 + strokeWidth, 200 + strokeWidth);

        //Initial Angle (optional, it can be zero)
        angle = 120;

    protected void onDraw(Canvas canvas) {
        canvas.drawArc(rect, START_ANGLE_POINT, angle, false, paint);

    public float getAngle() {
        return angle;

    public void setAngle(float angle) {
        this.angle = angle;

Creating the animation class to set the new angle:

public class CircleAngleAnimation extends Animation {

    private Circle circle;

    private float oldAngle;
    private float newAngle;

    public CircleAngleAnimation(Circle circle, int newAngle) {
        this.oldAngle = circle.getAngle();
        this.newAngle = newAngle; = circle;

    protected void applyTransformation(float interpolatedTime, Transformation transformation) {
        float angle = oldAngle + ((newAngle - oldAngle) * interpolatedTime);


Put circle into your layout:

    android:layout_height="300dp" />

And finally starting the animation:

Circle circle = (Circle) findViewById(;

CircleAngleAnimation animation = new CircleAngleAnimation(circle, 240);

The result is: enter image description here

Saturday, June 12, 2021
answered 6 Months ago
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);

Then send this encodedImage as a String and in your server you will need to decode it in order to get the image itself.

Wednesday, July 28, 2021
answered 4 Months ago

Miguel- Isn't this method called only if its a success response?

For all status codes <200 or status code >200 volley invokes parseNetworkError(VolleyError volleyError) instead of parseNetworkResponse(NetworkResponse response)method. Look here -

Line number -118-120

  if (statusCode < 200 || statusCode > 299) {
                throw new IOException();

and the corresponding catch block Line number - 128 -151

catch (IOException e) {
            int statusCode = 0;
            NetworkResponse networkResponse = null;
            if (httpResponse != null) {
                statusCode = httpResponse.getStatusLine().getStatusCode();
            } else {
                throw new NoConnectionError(e);
            VolleyLog.e("Unexpected response code %d for %s", statusCode, request.getUrl());
            if (responseContents != null) {
                networkResponse = new NetworkResponse(statusCode, responseContents,
                        responseHeaders, false);
                if (statusCode == HttpStatus.SC_UNAUTHORIZED ||
                        statusCode == HttpStatus.SC_FORBIDDEN) {
                            request, new AuthFailureError(networkResponse));
                } else {
                    // TODO: Only throw ServerError for 5xx status codes.
                    throw new ServerError(networkResponse);
            } else {
                throw new NetworkError(networkResponse);

If you want to override this behavior you can add your status code specific implementation inside>performRequest method.

Edit : So its not because of status code but because of the empty response. Well I think you are doing the right thing implementing your custom Request class. Volley comes with a few predefined popular types of requests for ease of use, but you can always create your own. Instead of a status code based implementation i'd rather simply check if the following string is empty before deserialzing it -

String jsonString = new String(,
if (!jsonString .isEmpty()) {
                 return Response.success(new JSONObject(jsonString),
else {
return Response.success(new JSONObject(),

**haven't tested this, but you get the point :)

Thursday, July 29, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :