How to Send Multimedia Files in APIS Using Kotlin/Android.

Hello all,

we will see how to send Multimedia files to API body using Kotlin.

As firstly we have three 3 steps to call any API, They are

  1. Response Modal
  2. API calling Interface
  3. Class where we call interface.

Before these steps we have to conform that do we have API client file and OKhttp is calling.

1.Response Modal:

As we can create the modal from any JSON response just by using the Kotlin modal by using JSON option on right clicking the modal folder in Android studio.

2.API Calling Interface:

We just have to write the interface to use in interface file as show below.

    @Multipart
    @NonNull
    @POST("URL PATH")
    fun inteface_name(
        @HeaderMap headers: Map<String, String>,// header data
        @Part("type")type: RequestBody,//text data
        @Part image: MultipartBody.Part //file
    ): Call<response modal name>

As after the interface we have to call this interface in class we need.

3.Class where we call interface

We have to create and call this function in the same class and required Activity.

 private fun function_name(file1: File ) { 
           try {

               val uploadBillService = ApiClient.buildService(ApiInterface::class.java) //Calling Api service

               val requestFile1= file1.asRequestBody("image/*".toMediaTypeOrNull())
               val body1 = MultipartBody.Part.createFormData("pan_image", file1.name, requestFile1) //Adding file to post


               val headers: MutableMap<String, String> = HashMap()
           headers["SessionToken"] = sharedPreference.getValueString("Auth_token")!! // Adding Session data

               val type: RequestBody = "String".toRequestBody("text/plain".toMediaTypeOrNull()) // Adding text data


               val requestCall = uploadBillService.pandetails(
                   headers,
                   type,
                   body1
               )// calling interface
               requestCall.enqueue(object : Callback<response modal name> {
                   override fun onResponse(
                       call: Call<response modal name>,
                       response: Response<response modal name>
                   ) {
                       when {
                           response.code() == 200 -> {//status code between 200 to 299 
                                // Response and Action to response
                           }
                           response.code() == 401 -> {
                               showToast(getString(R.string.session_exp)) 
                           }
                           else -> {//Application-level failure
                               //status code in the range of 300's, 400's, and 500's
                               showToast(getString(R.string.server_error))
                           }
                       }
                   }

                   //invoked in case of Network Error or Establishing connection with Server
                   //or Error Creating Http Request or Error Processing Http Response
                   override fun onFailure(call: Call<response modal name>, t: Throwable) {
                       showToast(getString(R.string.server_error))
                   }
               })

           } catch (e: java.lang.Exception) {
               showToast(e.message.toString())
           }
       }

Hope it will helps you,

Thank you.

Tagged:
Sign In or Register to comment.