Upload Image in Custom Object using Multipart Form Data In Android

放肆的年华 提交于 2019-12-12 01:45:42

问题


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).

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.

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/


回答1:


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.

Here's the 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.



来源:https://stackoverflow.com/questions/20146011/upload-image-in-custom-object-using-multipart-form-data-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!