Unable to create call adapter for class example.Simple
In case of Kotlin and coroutines this situation happened when I forgot to mark api service function as suspend
when I call this function from CoroutineScope(Dispatchers.IO).launch{}
:
Usage:
val apiService = RetrofitFactory.makeRetrofitService()
CoroutineScope(Dispatchers.IO).launch {
val response = apiService.myGetRequest()
// process response...
}
ApiService.kt
interface ApiService {
@GET("/my-get-request")
suspend fun myGetRequest(): Response<String>
}
Short answer: return Call<Simple>
in your service interface.
It looks like Retrofit 2.0 is trying to find a way of creating the proxy object for your service interface. It expects you to write this:
public interface SimpleService {
@GET("/simple/{id}")
Call<Simple> getSimple(@Path("id") String id);
}
However, it still wants to play nice and be flexible when you don't want to return a Call
. To support this, it has the concept of a CallAdapter
, which is supposed to know how to adapt a Call<Simple>
into a Simple
.
The use of RxJavaCallAdapterFactory
is only useful if you are trying to return rx.Observable<Simple>
.
The simplest solution is to return a Call
as Retrofit expects. You could also write a CallAdapter.Factory
if you really need it.
add dependencies:
compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta1'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
create your adapter this way:
Retrofit rest = new Retrofit.Builder()
.baseUrl(endpoint)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
addCallAdapterFactory ()
and addConverterFactory ()
both need to be called.
Service:
public interface SimpleService {
@GET("/simple/{id}")
Call<Simple> getSimple(@Path("id") String id);
}
Modify Simple
to Call<Simple>
.
With the new Retrofit(2.+) you need to add addCallAdapterFactory which can be a normal one or a RxJavaCallAdapterFactory(for Observables). I think you can add more than both too. It automatically checks which one to use. See a working example below. You can also check this link for more details.
Retrofit retrofit = new Retrofit.Builder().baseUrl(ApiConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build()