retrofit2.HttpException: HTTP 404, at retrofit2.KotlinExtensions$await$2$2.onResponse error

52 views Asked by At

I want to implement use-case for my project; this is my retrofit implemantation;

`@Module @InstallIn(SingletonComponent::class) object AppModule {

@Singleton
@Provides
fun provideRetrofitInstance(okHttpClient: OkHttpClient): Retrofit {
    return Retrofit.Builder()
        .baseUrl(Constants.BASE_URL)
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
}

@Singleton
@Provides
fun provideApi(retrofit:Retrofit):ProfileApiService{
    return retrofit.create(ProfileApiService::class.java)
}

@Singleton
@Provides
fun provideOkhttp():OkHttpClient{

    return createUnsafeOkHttpClient()

}


fun createUnsafeOkHttpClient(): OkHttpClient {
    try {
        // Trust all certs
        val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
            override fun checkClientTrusted(chain: Array<X509Certificate>, authType: String) {
            }

            override fun checkServerTrusted(chain: Array<X509Certificate>, authType: String) {
            }

            override fun getAcceptedIssuers(): Array<X509Certificate> {
                return arrayOf()
            }
        })

        // Install the all-trusting trust manager
        val sslContext = SSLContext.getInstance("SSL")
        sslContext.init(null, trustAllCerts, SecureRandom())
        // Create an ssl socket factory with our all-trusting manager
        val sslSocketFactory = sslContext.socketFactory

        return OkHttpClient.Builder().apply {
            sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
            hostnameVerifier { _, _ -> true }
        }.build()
    } catch (e: Exception) {
        throw RuntimeException(e)
    }
}

}`

this is my api service

interface ProfileApiService { @GET suspend fun getUsers(@Url url:String): ProfileResponse }

this is my use case;

`class GetUserProfile @Inject constructor( private val profileRepository: ProfileRepository ): MutableLiveData(){

companion object {
    private const val URL =
        "https://jsonplaceholder.typicode.com/users/1"
}

data class Params(
    val id: Int,
    val name: String,
    val username: String,
    val email: String,
    val phone: String,
)


 fun execute(
    viewModel: ProfileDetailViewModel,
    input: Params?
): MutableLiveData<ProfileResponse> {
    return MutableLiveData<ProfileResponse>().apply {
        input?.let {
            viewModel.viewModelScope.launch {
                value =
                    profileRepository.getUser(URL+it.id+it.name+it.username+it.email+it.phone)



            }
        }
    }
}

}`

I looked Postman and values comes correctly. But I'm having same problem. I couldnt find the solution.

I looked stackoverflow and their problems not like mine. They say the problem occurs when base url doesnt end with "/" but mine is ending with it.

0

There are 0 answers