如何使用 Volley 在 Android 中发送“multipart/form-data"POST [英] How to send a “multipart/form-data” POST in Android with Volley

查看:42
本文介绍了如何使用 Volley 在 Android 中发送“multipart/form-data"POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人能够使用 Volley 在 Android 中完成发送 multipart/form-data POST?我尝试使用 POST 请求将 image/png 上传到我们的服务器没有成功,我很好奇是否有人上传过.

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.

我相信这样做的默认方法是覆盖 Request.java 类中的 public byte[] getPostBody() 并附加 File 那里有一个空白的 Header 键作为边界.但是,将我的文件转换为 MapStringpostParams 然后再次对其进行编码似乎很笨拙而且不是很优雅.我的尝试也没有成功.这确实是阻止我们切换到这个库的唯一因素.

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.

推荐答案

我可能错了,但我认为您需要为此实现自己的 com.android.volley.toolbox.HttpStack因为默认的(HurlStack 如果 version > Gingerbread 或 HttpClientStack)不处理 multipart/form-data.

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 or HttpClientStack) don't deal with multipart/form-data.

我确实错了.我可以像这样在请求中使用 MultipartEntity 来做到这一点:

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

它很原始,但我用一个图像和一个简单的字符串尝试过它并且它有效.响应是一个占位符,在这种情况下返回响应字符串没有多大意义.我在使用 apache httpmime 来使用 MultipartEntity 时遇到问题,所以我使用了这个 https://code.google.com/p/httpclientandroidlib/ 不知道有没有更好的方法.希望有帮助.

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.

编辑

你可以使用httpmime而不使用httpclientandroidlib,唯一的依赖是httpcore.

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

这篇关于如何使用 Volley 在 Android 中发送“multipart/form-data"POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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