如何送“的multipart / form-data的”POST Android中与排球 [英] How to send a “multipart/form-data” POST in Android with Volley

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

问题描述

有没有人能够做到这一点了吗?我没有成功尝试使用POST请求到我们的服务器上传图像/ PNG和很好奇,如果任何人有。我相信,默认的方式做,这将是覆盖公共字节[]在Request.java类getPostBody()和附加的文件那里,一个空的头键的边界。但是,将我的文件,以一个字符串为地图<字符串,字符串> postParams ,然后让它连接codeD似乎再次钝无比算不上优雅(也是不成功的在我的尝试)。这是真正的唯一阻碍我们切换到这个库。

Has anyone been able to accomplish this yet? I have had no success trying to upload a 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 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 extremely not really elegant (also unsuccessful in my attempts). This is really the only thing holding us back from switching to this library.

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

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

推荐答案

我可能是错的,但我认为你需要的,如果要实现自己的com.android.volley.toolbox.HttpStack对于这一点,因为默认的(HurlStack版本>姜饼或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);
    }
}

这是pretty的原料,但我想它具有图像和简单的字符串和它的作品。响应是一个占位符,没有多大意义,返回响应字符串在这种情​​况下。我使用Apache httpmime使用MultipartEntity问题,所以我用这个<一href="https://$c$c.google.com/p/httpclientandroidlib/">https://$c$c.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.

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

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