【问题标题】:How to multipart data using Android Volley如何使用 Android Volley 多部分数据
【发布时间】:2013-08-19 18:40:14
【问题描述】:

我正在使用 Volley 为我的应用调用网络请求。但因为我是第一次排球。我只想知道如何使用多部分通过 volley 上传图像/视频媒体数据。

我搜索了很多网站,我得到了一些结果

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

但是,这些方法看起来并不好或效率不高。所以,请帮助我如何使用 volley 上传媒体数据。或者我不应该使用 Volley,而应该使用以前的手动方法

无论如何,所有的想法和答案都非常感谢。谢谢您的帮助。

【问题讨论】:

标签: android performance android-volley


【解决方案1】:

不知道你是否有答案,但如果你还没有尝试过:

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 到我写这个答案(02/07/14)时,我正在使用的版本是 4.3.2。

希望对你有帮助!

【讨论】:

  • 是的,确实如此。试试看!
  • 工作就像一个魅力,这个答案应该被接受!
  • 我在为我的 Android 项目添加库时遇到问题。请帮忙。我正在使用 Eclipse IDE。
  • 尝试在这里提出一个新问题来描述您的问题,您尝试过什么。
  • @maohieng 您应该考虑开始迁移到 Android Studio。 Google won't be supporting it much longer.
【解决方案2】:

您必须使用下面的代码使用多部分齐射上传图像。它在我的应用程序中就像一个魅力。

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

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

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

    @Override
    public byte[] getBody() throws AuthFailureError
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try
        {
            entity.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);
    }
}

【讨论】:

  • 使用已弃用的MultipartEntity
猜你喜欢
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
  • 2018-01-04
  • 2020-02-24
  • 1970-01-01
  • 2020-07-11
  • 1970-01-01
相关资源
最近更新 更多