I added second response in my ApiService and I get null from second retrofit response

Viewed 16
## ApiService ##
        interface ApiService {
            @GET("recipes/random")
            suspend fun getRecipes(
                @Header("x-api-key") apiKey: String,
                @Query("number") number: Int
            ): Response<Recipes>
        
            @GET("recipes/complexSearch")
            suspend fun getMostPopularRecipe(
                @Header("x-api-key") apiKey: String,
                @Query("number") number: Int,
                @Query("sort") sort: String
            ): Response<Recipes>
        }
    
## Repository ##
        class MainRepository (private val apiHelper: ApiHelper) {
            suspend fun getRecipes() =  apiHelper.getRecipes()
            suspend fun getMostPopularRecipe() = apiHelper.getMostPopularRecipe()
        }
## AppModule ##
        const val BASE_URL = "https://api.spoonacular.com/"
        
        val appModule = module {
            factory { provideOkHttpClient() }
            factory { provideRetrofit(get(), BASE_URL) }
            factory{ get<Retrofit>().create(ApiService::class.java) }
            //factory { provideApiService(get()) }
            factory { provideNetworkHelper(androidContext()) }
        
            factory<ApiHelper> {
                return@factory ApiHelperImpl(get())
            }
        }
        
        private fun provideNetworkHelper(context: Context) = NetworkHelper(context)
        
        private fun provideOkHttpClient() = if (BuildConfig.DEBUG) {
            val loggingInterceptor = HttpLoggingInterceptor()
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
            OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .build()
        } else OkHttpClient
            .Builder()
            .build()
        
        private fun provideRetrofit(
            okHttpClient: OkHttpClient,
            BASE_URL: String
        ): Retrofit =
            Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .build()
        
        
## ViewModel ##
        class MostPopularRecipeViewModel(
            private val mainRepository: MainRepository,
            private val networkHelper: NetworkHelper
        ) : ViewModel() {
        
            private val _recipes = MutableLiveData<Resource<Recipes>>()
            val recipes: LiveData<Resource<Recipes>>
                get() = _recipes
        
            init {
                fetchRecipes()
            }
        
            private fun fetchRecipes() {
                viewModelScope.launch {
                    if (networkHelper.isNetworkConnected()) {
                        mainRepository.getMostPopularRecipe().let {
                            if (it.isSuccessful) {
                                _recipes.postValue(Resource.success(it.body()))
                                Log.d("test", it.body().toString())
                            } else _recipes.postValue(Resource.error(it.errorBody().toString(), null))
                        }
                    } else _recipes.postValue(Resource.error("No internet connection", null))
                }
            }
        }

How can i make the second response not null? Application works, first response not null. I checked response with postman and it isn't null. I am new with koin and retrofit, and i have no idea. I have thought problem from my AppModule? but i don't know, how to correct this................................

0 Answers
Related