在Kotlin中使用Retrofit @PartMap错误的多部分请求(Android)

Jag*_*wla 15 android multipart kotlin retrofit android-studio-3.0

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

logcat的

08-20 23:46:51.003 3782-3782/com.qkangaroo.app W/System.err:java.lang.IllegalArgumentException:参数类型不能包含类型变量或通配符:java.util.Map(参数#1) 08-20 23:46:51.003 3782-3782/com.qkangaroo.app W/System.err:for方法ApiInterface.updateCustomerDetail 08-20 23:46:51.003 3782-3782/com.qkangaroo.app W/System.err :at retrofit2.ServiceMethod $ Builder.methodError(ServiceMethod.java:752)08-20 23:46:51.004 3782-3782/com.qkangaroo.app W/System.err:at retrofit2.ServiceMethod $ Builder.methodError(ServiceMethod. java:743)08-20 23:46:51.004 3782-3782/com.qkangaroo.app W/System.err:at retrofit2.ServiceMethod $ Builder.parameterError(ServiceMethod.java:761)08-20 23:46:51.004 3782-3782/com.qkangaroo.app W/System.err:at retrofit2.ServiceMethod $ Builder.build(ServiceMethod.java:195)08-20 23:46:51.004 3782-3782/com.qkangaroo.app W/System .err:at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)08-20 23:46:51.005 3782-3782/com.qkangaroo.app W/System.err:a t retrofit2.Retrofit $ 1.invoke(Retrofit.java:147)08-20 23:46:51.005 3782-3782/com.qkangaroo.app W/System.err:at $ Proxy0.updateCustomerDetail(Native Method)08-20 23 :46:51.005 3782-3782/com.qkangaroo.app W/System.err:at com.qkangaroo.app.Fragments.MoreScreen.MoreFragment.updateProfile(MoreFragment.kt:261)08-20 23:46:51.006 3782- 3782/com.qkangaroo.app W/System.err:at com.qkangaroo.app.Fragments.MoreScreen.MoreFragment $ clickListener $ 1.onClick(MoreFragment.kt:191)08-20 23:46:51.006 3782-3782/com .qkangaroo.app W/System.err:在android.view.View.performClick(View.java:3517)08-20 23:46:51.006 3782-3782/com.qkangaroo.app W/System.err:at android .view.View $ PerformClick.run(View.java:14155)08-20 23:46:51.006 3782-3782/com.qkangaroo.app W/System.err:at android.os.Handler.handleCallback(Handler.java) :605)08-20 23:46:51.007 3782-3782/com.qkangaroo.app W/System.err:at android.os.Handler.dispatchMessage(Handler.java:92)08-20 23:46:51.007 3782 -3782/com.qkangaroo.app W/System.err:在android.os.Looper.loop(Looper.java:154)08-20 23:46:51.007 3782-3782/com.qkangaroo.app W/System.err:at android.app.ActivityThread.main(ActivityThread.java:4624) 08-20 23:46:51.008 3782-3782/com.qkangaroo.app W/System.err:at java.lang.reflect.Method.invokeNative(Native Method)08-20 23:46:51.009 3782-3782/com .qkangaroo.app W/System.err:at java.lang.reflect.Method.invoke(Method.java:511)08-20 23:46:51.009 3782-3782/com.qkangaroo.app W/System.err:在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:809)08-20 23:46:51.010 3782-3782/com.qkangaroo.app W/System.err:at com.android.internal .os.ZygoteInit.main(ZygoteInit.java:576)08-20 23:46:51.011 3782-3782/com.qkangaroo.app W/System.err:at dalvik.system.NativeStart.main(Native Method)

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)
        }
    })
Run Code Online (Sandbox Code Playgroud)

ApiClient.kt

val MULTIPART_FORM_DATA = "multipart/form-data"

fun createRequestBody(s: String): RequestBody {
    return RequestBody.create(
            MediaType.parse(MULTIPART_FORM_DATA), s)
}
Run Code Online (Sandbox Code Playgroud)

ApiInterface,.kt

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

Gradle文件

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"
Run Code Online (Sandbox Code Playgroud)

Jag*_*wla 32

@JvmSuppressWildcards之前添加RequestBody

fun updateCustomerDetail(@PartMap map: Map<String, @JvmSuppressWildcards RequestBody >): Call<UpdateCustomer>
Run Code Online (Sandbox Code Playgroud)

  • 您是否希望通过增加一些解释来改进您的仅代码答案? (9认同)

Mas*_*ing 5

使用HashMapMutableMap代替Map <K,out V>作为PartMap

另一种对我有效的方法。tpom6ohRetrofi Kotlin问题中提到

我认为这是因为Map声明是公共接口Map <K,out V>,而out字使值类型通用。您可以尝试改用MutableMapHashMap

@Multipart
@POST("customer")
fun updateCustomerDetail(@PartMap map: HashMap<String, RequestBody>): Call<UpdateCustomer>
Run Code Online (Sandbox Code Playgroud)