本文介绍了Kotlin(Android)中有Retrofit @PartMap错误的多部分请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在Java中使用此代码,则可以正常工作.当我在kotlin中转换该代码时,我得到了错误.

If I am using this code in Java then its working fine. When I convert that code in kotlin then I got Error.

fragment.kt

    var map:HashMap<String,RequestBody> = HashMap<String, RequestBody>()
    map.put("version",ApiClient.createRequestBody(AppConstants.API_VERSION))
    map.put("auth_token", ApiClient.createRequestBody(customer.authToken!!))
    map.put("customer_name",ApiClient.createRequestBody(profileName))
    map.put("email", ApiClient.createRequestBody(profileEmail))

    val apiInterface = ApiClient.client.create(ApiInterface::class.java)

    val updateCustomerCall: Call<UpdateCustomer> = apiInterface.updateCustomerDetail(map)
    updateCustomerCall.enqueue(object : Callback<UpdateCustomer> {
        override fun onResponse(call: Call<UpdateCustomer>?, response: Response<UpdateCustomer>?) {

        }

        override fun onFailure(call: Call<UpdateCustomer>?, t: Throwable?) {
            utilities!!.hideProgress(progress)
        }
    })

ApiClient.kt

val MULTIPART_FORM_DATA = "multipart/form-data"

fun createRequestBody(s: String): RequestBody {
    return RequestBody.create(
            MediaType.parse(MULTIPART_FORM_DATA), s)
}

ApiInterface,.kt

@Multipart
@POST("customer")
fun updateCustomerDetail(@PartMap map: Map<String,RequestBody >): Call<UpdateCustomer>

成绩文件

implementation "com.squareup.okhttp3:okhttp:3.8.1"
implementation "com.squareup.okhttp3:logging-interceptor:3.8.1"
implementation ("com.squareup.retrofit2:retrofit:2.3.0"){
     exclude module: 'okhttp'
}
implementation "com.squareup.retrofit2:converter-gson:2.3.0"

推荐答案

RequestBody

fun updateCustomerDetail(@PartMap map: Map<String, @JvmSuppressWildcards RequestBody >): Call<UpdateCustomer>

这篇关于Kotlin(Android)中有Retrofit @PartMap错误的多部分请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 05:19