简体   繁体   中英

Post form data params using Retrofit kotlin

Am new to retrofit and I would like to post form data using retrofit and coroutines in kotlin.

在此处输入图像描述

    val api = Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
      

    val formData = listOf(
        "function" to "getSubscriber" ,
        "phoneNumber" to "0100844789")

    lifecycleScope.launch(Dispatchers.IO) {
        
    }

how do I proceed from here

Some of The references is below I think it helps you

Article Link

Video tutorial link

So I have done what @Providerz has said and it has worked.

 val api = Retrofit.Builder()
      .baseUrl(BASE_URL)
      .addConverterFactory(GsonConverterFactory.create())
      .build()
      .create(MyAPI::class.java)

     lifecycleScope.launch(Dispatchers.IO) {
      val response = api.getSubscriber("getSubscriber","0100844789")
      Log.d("######", response.message.toString()) 
     }

MyAPI

    interface MyAPI {
    @FormUrlEncoded
    @POST("index.php")
    suspend fun getSubscriber(@Field("function") function: String, @Field("phoneNumber") phoneNumber: String): Json4Kotlin_Base
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM