如何使用Android的排球为多数据 [英] How to multipart data using Android Volley

查看:121
本文介绍了如何使用Android的排球为多数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的凌空调用Web请求我的应用程序。但是,由于我对排球第一次。我只是想知道,如何使用多通过抽射上传图像/视频媒体数据。

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

<一个href="http://stackoverflow.com/questions/16797468/how-to-send-a-multipart-form-data-post-in-android-with-volley">How送的multipart / form-data的POST Android中与排球

不过,这些方法一点儿也不好看或efficients。所以,请帮助我,如何使用凌空上传的媒体数据。或者我不应该用排枪,应该去为previous手动方法

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

总之,所有的心思和答案都非常AP preciated。感谢您的帮助。

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

推荐答案

不知道,如果你有一个答案,但如果你还没有尝试这样的:

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);
}
   }

为了使它工作,你必须使用此Java httpclients: http://hc.apache.org/downloads.cgi 到时候我写这个答案(14年2月7日),我使用的版本是4.3.2。

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.

希望它能帮助!

这篇关于如何使用Android的排球为多数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆