jamesdeperio/RetrofitKit

Not able to use this with moshi

Closed this issue · 11 comments

l2dev commented

Getting errors such as

Caused by: java.lang.IllegalArgumentException: Could not locate RequestBody converter for class

l2dev commented

@jamesdeperio I want to use one moshi converter with null serialization and another moshi converter without null serialization. Is that possible with this library?

@eurbon: yes, create your own annotation then addCustomConverterFactory

@Retention(AnnotationRetention.RUNTIME)
annotation class MoshiFormat

@Retention(AnnotationRetention.RUNTIME)
annotation class MoshiFormatWithNullSerialization



val multipleConverter = SerializationFormatFactory.Builder()
.addCustomConverterFactory(MoshiFormatWithNullSerialization::class.java, MoshiConverterFactory.create().withNullSerialization())
.addCustomConverterFactory(MoshiFormat::class.java, MoshiConverterFactory.create())
.build()


// request
    @GET("route")
    @MoshiFormat
    fun getResponse(): Observable<Response>


    @GET("route")
    @MoshiFormatWithNullSerialization
    fun getResponse(): Observable<Response>
l2dev commented

@jamesdeperio This is exactly what I did, and then I get the very same error:

Caused by: java.lang.IllegalArgumentException: Unable to create @Body converter for class greenely.greenely.push.data.models.RegisterDeviceRequest (parameter #2)
        for method Api.registerDevice

this is how api looks like

    @POST("/v1/user/devices")
    @MoshiFormat
    fun registerDevice(
            @Header("Authorization") jwt: String,
            @Body body: RegisterDeviceRequest
    ): Observable<Response<@JvmSuppressWildcards Any>>

It's working fine on my test. (v1.0.5)
Can your post your code or share a gist.

l2dev commented

I can work on creating some proof of concept repo but in the meanwhile this is how my RegisterDeviceRequest model looks like

data class RegisterDeviceRequest(@field:Json(name = "device_id") val deviceId: String)

this is request defined in a repo from my viewmodel

    fun setToken(gcmToken: String): Single<Boolean> {
        return api.registerDevice("JWT ${userStore.token}", RegisterDeviceRequest(gcmToken))
                .doOnNext { storeGcmToken(gcmToken) }
                .doOnError { clearToken() }
                .map { true }
                .single(false)
    }

It calls the api which I posted earlier

Hmm. I think this error occur when you misconfigured your retrofit

java.lang.IllegalArgumentException: Could not locate RequestBody converter for class
l2dev commented

Hmm. I think this error occur when you misconfigured your retrofit

java.lang.IllegalArgumentException: Could not locate RequestBody converter for class

Is it a must to configure retrofit with that lazyretroft stuff or I can still do it the normal way?

Also can you share your retrofit client configuration code?

For me this error happens when I add two converters and then try to set them on all of my api calls.

This is the setup of Retrofit Manager, same as normal way.

  val  okHttpClientBuilder= OkHttpClient.Builder()
        okHttpClientBuilder.cache(cache)
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                .connectTimeout(CONNECT_TIMEOUT), TimeUnit.SECONDS)
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)

        if (DEBUG_MODE) {
            val logging = HttpLoggingInterceptor()
            logging.level = HttpLoggingInterceptor.Level.BODY
            okHttpClientBuilder.addInterceptor(logging)
        }
        retrofit = Retrofit.Builder()
                .baseUrl(URL)
                .client(okHttpClientBuilder.build())
                .addConverterFactory(CONVERTER_FACTORY)
                .addCallAdapterFactory(CALL_ADAPTER_FACTORY)
                .build()
`
l2dev commented

where do you provide initConverterFactory() ? In the same file?

@jamesdeperio This is exactly what I did, and then I get the very same error:

Caused by: java.lang.IllegalArgumentException: Unable to create @Body converter for class greenely.greenely.push.data.models.RegisterDeviceRequest (parameter #2)
        for method Api.registerDevice

this is how api looks like

    @POST("/v1/user/devices")
    @MoshiFormat
    fun registerDevice(
            @Header("Authorization") jwt: String,
            @Body body: RegisterDeviceRequest
    ): Observable<Response<@JvmSuppressWildcards Any>>

Wrong parameter for body (parameter # 2).
The library seems to work because you've already encountered retrofit error due to invalid parameter format. Cheers!!