I'm using dagger to inject retrofit for google directions api. The error seems to suggest I have 1 more parameter than I think I should have.
If I add another parameter in the interface, I will get error "No Retrofit annotation found. (parameter #7)" instead.
Edit: see Nicholas' comment. I found another issue afterwards, that due to the suspend function, in the interface GoogleMapServices, the Call<> wrappers should be removed or changed per: Retrofit 2.0 + GSON Unable to invoke no-args constructor for interface
Dagger module
@Module
object NetworkModule {
// @Provides
// @Singleton
// fun provideOkHttpClient(): OkHttpClient {
// return OkHttpClient.Builder()
// .addInterceptor(RequestInterceptor())
// .build()
// }
@Provides
@Singleton
fun provideRetrofit(@NonNull okHttpClient: OkHttpClient): Retrofit {
return Retrofit.Builder()
//.client(okHttpClient) removed to narrow down the issues
.baseUrl("https://maps.googleapis.com")
.addConverterFactory(MoshiConverterFactory.create())
//.addCallAdapterFactory(LiveDataCallAdapterFactory())
.build()
}
@Provides
@Singleton
fun provideGoogleMapsService(@NonNull retrofit: Retrofit): GoogleMapsService {
return retrofit.create(GoogleMapsService::class.java)
}
}
Google Maps Directions interface
interface GoogleMapsService {
@GET("maps/api/directions/json")
suspend fun fetchDirection(
@Query("origin") origin: String,
@Query("destination") destination: String,
@Query("mode") travelMode: String,
@Query("arrival_time") arrivalTime: String
@Query("key") apiKey: String
): Call<DirectionResponses>
ViewModel
class detailViewModel @Inject constructor(
private val repository: MyRepository,
private val service: GoogleMapsService
) : ViewModel() {
...
fun test() {
uiScope.launch {
suspendCall()
}
}
private suspend fun suspendCall() {
withContext(Dispatchers.IO) {
direction.value = service.fetchDirection("aaa","bbb", "ccc","1600000000", "apikey").toString()
}
}
...
}
Error Message
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.example, PID: 21142
java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #6)
for method GoogleMapsService.fetchDirection
at retrofit2.Utils.methodError(Utils.java:52)
at retrofit2.Utils.methodError(Utils.java:42)
at retrofit2.Utils.parameterError(Utils.java:61)
at retrofit2.RequestFactory$Builder.parseParameter(RequestFactory.java:311)
at retrofit2.RequestFactory$Builder.build(RequestFactory.java:182)
at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:65)
at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:25)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:168)
at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
at $Proxy2.fetchDirection(Unknown Source)
at com.example.example.<...>.DetailViewModel$suspendCall$2.invokeSuspend(DetailViewModel.kt:72)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)