使用多表单数据在Android中上传图片的自定义对象 [英] Upload Image in Custom Object using Multipart Form Data In Android

查看:228
本文介绍了使用多表单数据在Android中上传图片的自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建数据的上传结构:
    {
        authentication_token:some_token,
        用户:{
            其中fname:FNAME
            LNAME:LNAME
            阿凡达:图像文件
            位置:位置
        }
    }

I need to create the uploaded structure of data as : { "authentication_token":"some_token", "user": { "fname": "fname", "lname": "lname", "avatar": "image-file" "location": "location" } }

阿凡达实际上是一个图像文件(用户的个人资料图片)。

The "avatar" is actually an Image File (profile image of the user).

我试图创建使用Apache的MultipartEntity类结构,增加FileBody&安培; StringBody对象。对于键值配对,我用的NameValuePair。但我不能够单独添加的用户对象。我不知道怎么样。

I've tried to create the structure using Apache's MultipartEntity class, adding FileBody & StringBody objects. For key-value pairing, I've used NameValuePair. But I'm not able to add the User object separately. I don't know how to.

我所期望的是这些链接的答案:
<一href=\"http://stackoverflow.com/questions/13081831/multipartentity-along-with-plain-text-in-android\">MultiPartEntity随着Android的纯文本
Android的多部分上传
<一href=\"http://stackoverflow.com/questions/12422541/how-to-send-multiple-images-to-server-using-multipartentity-from-android\">How使用MultipartEntity于Android 多个图像发送到服务器
<一href=\"http://www.$c$crzheaven.com/2011/08/16/how-to-upload-multiple-files-in-one-request-along-with-other-string-parameters-in-android/\" rel=\"nofollow\">http://www.$c$crzheaven.com/2011/08/16/how-to-upload-multiple-files-in-one-request-along-with-other-string-parameters-in-android/

I looked for answers in these links : MultiPartEntity along with plain text in android Android Multipart Upload How to send multiple images to server using MultipartEntity from android http://www.coderzheaven.com/2011/08/16/how-to-upload-multiple-files-in-one-request-along-with-other-string-parameters-in-android/

推荐答案

这么多的研究之后,我终于找到一个可行的解决方案,不知道是否是最好的之一。
我不得不使用Apache的MultipartEntity及其辅助类。

After so much research, I've finally found a working solution, don't know whether it is the best one. I had to use Apache's MultipartEntity and its helper classes.

这里的code:

ArrayList<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
BasicNameValuePair bn;

bn = new BasicNameValuePair("authentication_token", app.getSystemValue("authentication_token"));
nameValuePairs.add(bn);
bn = new BasicNameValuePair("fname", user.getFname());
nameValuePairs.add(bn);
bn = new BasicNameValuePair("lname", user.getLname());
nameValuePairs.add(bn);
bn = new BasicNameValuePair("location", user.getLocation());
nameValuePairs.add(bn);
bn = new BasicNameValuePair("avatar", user.getAvatar());
nameValuePairs.add(bn);

HttpClient client = new DefaultHttpClient();
client.getParams().setParameter("Connection", "Keep-Alive");
client.getParams().setParameter("Content-Type", "multipart/form-data;");
client.getParams().setParameter("http.socket.timeout", Integer.valueOf(TIMEOUT_WAIT_TO_CONNECT));
client.getParams().setParameter("http.connection.timeout", Integer.valueOf(TIMEOUT_WAIT_FOR_DATA));

MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
int i=0;

for (BasicNameValuePair nameValuePair : nameValuePairs) {
    if (nameValuePair.getName().equalsIgnoreCase("avatar")) {
        File imgFile = new File(nameValuePair.getValue());
        FileBody fileBody = new FileBody(imgFile, "profile.jpeg", "image/jpeg", "UTF-8");
        multipartEntity.addPart("user[avatar]", fileBody);
    } 
    else {
        if (nameValuePair.getValue()!=null) {
            if (i==0) {
                multipartEntity.addPart(nameValuePair.getName(), new StringBody(nameValuePair.getValue()));
            }
            else {
                multipartEntity.addPart("user[" + nameValuePair.getName() + "]", new StringBody(nameValuePair.getValue()));
            }
        }
    }
    i++;
}

HttpPost post = new HttpPost(serviceUri);
post.setEntity(multipartEntity);
HttpResponse response = client.execute(post);

我希望它可以帮助有需要的人。

I hope it helps someone in need.

这篇关于使用多表单数据在Android中上传图片的自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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