Android (Java), Development, Mobile, Mobile, Tech, Tutorials, Tutorials

Multipart Request Using Android Volley

multipart request

Http Multipart requests are used to send heavy data data or files like audio and video to the server. Android Volley gives you a very faster and optimized environment to send heavy data or files to the server.

How to send Multipart Request using volley?

Create a file named CustomMultipartRequest.java and copy the following code to it:

The above class accepts MultipartEntity object in its constructor and converts it into a byte array that is appended as body of the request. Now you must be wondering how to create a MultipartEntity object from your image, video or any other file? The following code shows how to do so:

In above code, we created a File object using the path of the file and then added that file to the entity object using the addPart() method. Note, you can add multiple parts to an MultipartEntity. For example in the above code we added upload_medium and file_name to our entity object. Now, after creating the MultipartEntity object will need to pass this object to our CustomMultipartRequest class we created above. The following code shows how to pass theentity object to the CustomMultipartRequest class to create a multipart request:

Above, we passed 4 parameters to the constructor of the CustomMultipartRequest class. First parameter is the url where you sending the request, second parameter is the instance of Response.Listener class, third parameter is instance of Response.ErrorListener class and final fourth parameter is our MultipartEntity object.

 

Hope this post helps you in successfully creating a multipart request using andorid volley. For any further quires on this please leave a comment below. We will be happy to help.

You Might Also Like

  • RohanKandwal

    Can you post github link for this project?

    • Taimur Hassan

      This is a working code. Thanks to Mansha Chuttani for this.
      You should have to replace all < with less than sign “”.
      1. compile(‘org.apache.httpcomponents:httpmime:4.2.3′)
      2. Import this class in your peoject. (CustomMultipartRequest )
      3. In your activity do the rest of the work.
      You are good to go.
      Thanks once again to miss mansha for this code. I was stuck on this for 3 days.

  • Jay Gohil

    can you please provide your imports;

  • Jay Gohil

    getting erro at line 56

    java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/message/BasicHeaderValueFormatter; in class Lorg/apache/http/message/BasicHeaderValueFormatter; or its superclasses (declaration of ‘org.apache.http.message.BasicHeaderValueFormatter’ appears in /system/framework/ext.jar)

    at org.apache.http.entity.ContentType.toString(ContentType.java:153)

    at org.apache.http.entity.mime.MultipartFormEntity.(MultipartFormEntity.java:56)

    at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:236)

    at org.apache.http.entity.mime.MultipartEntity.getEntity(MultipartEntity.java:119)

    at org.apache.http.entity.mime.MultipartEntity.writeTo(MultipartEntity.java:180)

    at com.project.publiko.CustomMultipartRequest.getBody(CustomMultipartRequest.java:56)

    at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:236)

    at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:210)

    at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:106)

    at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)

    at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:110)

  • Vadivel C

    Hi ,i use your code ,I could not know where i get response , got like this ���}�k�h:w��o$��T�Z|. and Response listener always execute Response listener error