Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly are MultiPartEntity and Form Data? How are they used to upload images in android?

I searched the web but i could only find code related to multipart form data, not explanation of what they are and how they are used?

like image 227
Kriti Sharma Avatar asked Dec 17 '15 05:12

Kriti Sharma


People also ask

What is MultiPartEntity?

Multipart/form data is used in html to send data with multiple parts(as the name suggests). Any kind of data may be added to it ie. string,file,etc. MultiPartEntity is the calss which dose the same thing on java. You can add more parts of data to this entity, for example reqEntity.

What is multipart in Android?

A multipart request is an HTTP request that HTTP clients create to send files and data to an HTTP server. A multipart message is made up of several parts. One part consists of a header and a body. The body can be any type of media and can contain text or binary data. A multipart media type can be included in a part.


1 Answers

Normally we are sending only string part of data,while in multipart file part is added with string ,so its called multipart.for example We can send multipart data using volley

 public class MultipartReq extends JsonObjectRequest {

        private static final String FILE_PART_NAME = "file";
        private static final String STRING_PART_NAME = "text";

        private final File mFilePart;
        //private final String mStringPart;


        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
        HttpEntity httpEntity;
        Context context;

        private Map<String, String> params;
        public MultipartReq(Context context, int method, String url, JSONObject jsonRequest, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener, File file, Map<String, String> params) {
            super(method, url, jsonRequest, listener, errorListener);

            this.context = context;
            mFilePart = file;
            entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);    
            this.params = params;
            buildMultipartEntity();
            httpEntity = entityBuilder.build();

        }


        private void buildMultipartEntity() {

            try {
                if (mFilePart.exists()) { entityBuilder.addBinaryBody(FILE_PART_NAME, mFilePart, ContentType.create(mimeType), mFilePart.getName());

                    }
                    try {
                        if(!params.isEmpty()){
                            for (String key: params.keySet()){
                                 entityBuilder.addPart(key, new StringBody(params.get(key),ContentType.TEXT_PLAIN));

                            }
                        }
                    } catch (Exception e) {
                        VolleyLog.e("UnsupportedEncodingException");
                    }


                } else {
                    ShowLog.e("no such file");
                }
            } catch (Exception e) {
                ShowLog.e("UnsupportedEncodingException");
            }
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> params = new HashMap<String, String>();

            return params;
        }


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

        @Override
        public byte[] getBody() {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            try {
                httpEntity.writeTo(bos);
            } catch (IOException e) {
                VolleyLog.e("IOException writing to ByteArrayOutputStream");
            }
            return bos.toByteArray();
        }


        @Override
        protected void deliverResponse(JSONObject response) {
            super.deliverResponse(response);
        }
    }
like image 173
Vishnu V S Avatar answered Oct 20 '22 02:10

Vishnu V S