使用Retrofit2和Kotlin将数据发布到正文中:无法创建呼叫适配器

时间:2018-11-15 17:57:10

标签: android rest kotlin retrofit2

我们在发送带有正文参数的发布请求时遇到问题。 JSON如下:

{
"userId": 123456,
"comment": "This is a comment"
}

我们通过以下接口调用后端:

interface ServiceDetailsService {

   @POST(API_CLOSE_EVENT)
   fun closeEvent(@Body data: servicePostData): Call<servicePostData>
}

我们通过以下方式调用该函数

fun serviceTaskDone(data: servicePostData) {
       val call: Call<servicePostData> = service.closeEvent(data)
       call.enqueue(object : Callback<servicePostData> {
           override fun onResponse(call: Call<servicePostData>, response: Response<servicePostData>) {
               Log.d("Output", response.toString())
           }

           override fun onFailure(call: Call<servicePostData>, t: Throwable) {
               Log.d("Output", t.toString())
           }

       })
   }

当我们现在执行代码时,将出现以下错误:

11-15 18:40:23.732 23168-23168/de.conlance.shannonapp E/AndroidRuntime: FATAL EXCEPTION: main
   Process: de.conlance.shannonapp, PID: 23168
   java.lang.IllegalArgumentException: Unable to create call adapter for retrofit2.Call<de.conlance.shannonapp.data.model.serviceDetails.Task>
       for method ServiceDetailsService.closeEvent
       at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:755)
       at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:240)
       at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:165)
       at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
       at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
       at java.lang.reflect.Proxy.invoke(Proxy.java:913)
       at $Proxy3.closeEvent(Unknown Source)
       at de.conlance.shannonapp.presentation.serviceDetails.ServiceDetailsViewModel.serviceTaskDone(ServiceDetailsViewModel.kt:37)
       at de.conlance.shannonapp.presentation.serviceDetails.ServiceDetailsFragment$showAcceptErrorDescDialog$1.onClick(ServiceDetailsFragment.kt:171)
       at android.view.View.performClick(View.java:6274)
       at android.view.View$PerformClick.run(View.java:24859)
       at android.os.Handler.handleCallback(Handler.java:789)
       at android.os.Handler.dispatchMessage(Handler.java:98)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6710)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
    Caused by: kotlin.TypeCastException: null cannot be cast to non-null type retrofit2.CallAdapter<kotlin.Any, kotlin.Any>
       at de.conlance.shannonapp.rx.RetrofitRxErrorHandlingCallAdapterFactory.get(RetrofitRxErrorHandlingCallAdapterFactory.kt:41)
       at retrofit2.Retrofit.nextCallAdapter(Retrofit.java:221)
       at retrofit2.Retrofit.callAdapter(Retrofit.java:205)
       at retrofit2.ServiceMethod$Builder.createCallAdapter(ServiceMethod.java:238)
       at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:165)
       at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
       at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
       at java.lang.reflect.Proxy.invoke(Proxy.java:913)
       at $Proxy3.closeEvent(Unknown Source)
       at de.conlance.shannonapp.presentation.serviceDetails.ServiceDetailsViewModel.serviceTaskDone(ServiceDetailsViewModel.kt:37)
       at de.conlance.shannonapp.presentation.serviceDetails.ServiceDetailsFragment$showAcceptErrorDescDialog$1.onClick(ServiceDetailsFragment.kt:171)
       at android.view.View.performClick(View.java:6274)
       at android.view.View$PerformClick.run(View.java:24859)
       at android.os.Handler.handleCallback(Handler.java:789)
       at android.os.Handler.dispatchMessage(Handler.java:98)
       at android.os.Looper.loop(Looper.java:164)
       at android.app.ActivityThread.main(ActivityThread.java:6710)
       at java.lang.reflect.Method.invoke(Native Method)
       at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

我们已经在Call中对Void参数进行了测试,并且还得到了与以下相同的错误消息。

对于任何建议,我们非常感谢。

最诚挚的问候

2 个答案:

答案 0 :(得分:0)

有些东西用@NonNull标注,而NULL则被传递到:

  

原因:kotlin.TypeCastException: null不能转换为非null类型 retrofit2.CallAdapter          在de.conlance.shannonapp.rx.RetrofitRxErrorHandlingCallAdapterFactory。获取(RetrofitRxErrorHandlingCallAdapterFactory.kt:41)

将其称为requestapi而不是call将使其更具可读性(call甚至可能会冲突)。

答案 1 :(得分:0)

@ martin-zeitler我们已经解决了这个问题。

  1. 我们添加了以下标头

@Headers(“ Content-Type:application / json; charset = urf-8”)

  1. 我们添加了

    之类的 compositDisposable

    compositDisposable.add(service.closeEvent(jsonObject.toString())

    .compose(RxSingleCallback.getSchedulersForSingle())
    .doOnSubscribe{ loadingStatusMutable.value = true }
    .doAfterTerminate { loadingStatusMutable.value = false }
    .subscribeWith(object : DisposableSingleObserver<ServicePostResponse>() {
        override fun onSuccess(response: ServicePostResponse) {
            Log.d("Response", response.toString())
        }
    
        override fun onError(t: Throwable) {
            Log.d("Response", t.toString())
            //snackBarMutable.value = throwable.localizedMessage
        }
    
    })