简体   繁体   中英

Post data in body with Retrofit2 and Kotlin: Unable to create call adapter

we have the problem to send a post request with body parameters. The JSON looks like:

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

We call the backend with the following interface:

interface ServiceDetailsService {

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

We call the function with:

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())
           }

       })
   }

And when we now execute our code the following errors will come up:

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)

We have tested with a Void-Parameter in the Call and we get also the same error message as below.

For any suggestion we are very grateful.

Best regards

there is something annotated with @NonNull , while it gets NULL passed into:

Caused by: kotlin.TypeCastException: null cannot be cast to non-null type retrofit2.CallAdapter at de.conlance.shannonapp.rx.RetrofitRxErrorHandlingCallAdapterFactory. get(RetrofitRxErrorHandlingCallAdapterFactory.kt:41)

calling it request or api instead of call would make it more readable ( call might even clash).

@martin-zeitler we have fixed the problem.

  1. We added the following header

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

  1. We added the compositDisposable like

    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 } }) 

    )

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