Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

performance - How to multipart data using Android Volley

I am using Volley for calling web request for my app. But as i am Volley for first time. I just want to know that how to upload image/video media data via volley using multipart.

I searched for it many sites, I got some results over

How to send a “multipart/form-data” POST in Android with Volley

But, these methods does'nt look good or efficients. So, Please help me that how to upload media data using volley. Or i should not use Volley, and should go for the previous manual approach

Anyway, all thoughts and answers are extremely appreciated. Thank you for your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Don't know if you have got an answer, but if you haven't try this:

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyLog;

public class MultipartRequest extends Request<String> {

// private MultipartEntity entity = new MultipartEntity();

MultipartEntityBuilder entity = MultipartEntityBuilder.create();
HttpEntity httpentity;
private static final String FILE_PART_NAME = "file";

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

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

    mListener = listener;
    mFilePart = file;
    this.mStringPart = mStringPart;
    entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    buildMultipartEntity();
}

public void addStringBody(String param, String value) {
    mStringPart.put(param, value);
}

private void buildMultipartEntity() {
    entity.addPart(FILE_PART_NAME, new FileBody(mFilePart));
    for (Map.Entry<String, String> entry : mStringPart.entrySet()) {
        entity.addTextBody(entry.getKey(), entry.getValue());
    }
}

@Override
public String getBodyContentType() {
    return httpentity.getContentType().getValue();
}

@Override
public byte[] getBody() throws AuthFailureError {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        httpentity = entity.build();
        httpentity.writeTo(bos);
    } catch (IOException e) {
        VolleyLog.e("IOException writing to ByteArrayOutputStream");
    }
    return bos.toByteArray();
}

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

@Override
protected void deliverResponse(String response) {
    mListener.onResponse(response);
}
   }

In order to make it work you must use this java httpclients: http://hc.apache.org/downloads.cgi By the time I'm writing this answer (02/07/14) the version I'm using is 4.3.2.

Hope it helps !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...