#mvvm #okhttp #listadapter
Вопрос:
Я получаю «NewsApi», там нужен апиКей, я это сделал, но данные не поступают в переработчик, и я не понимаю, в чем дело, код прилагается ниже, извините за мой английский, и код(я не англичанин, и это мой первый вопрос здесь!)
interface EverythingApiService { @GET("v2/everything?apiKey=a4abe61c730f4b39ab111c9630def7c4") suspend fun getEverything(@Query("q") query: kotlin.String): Responselt;NewsResponselt;Everythinggt;gt; }
==================================================================================================
class EverythingRepositoryImpl @Inject constructor( private val apiService: EverythingApiService ) : BaseRepository(), EverythingRepository { override suspend fun getEverything(query: String) = doRequest { apiService.getEverything(query) } }
==================================================================================================
@Module @InstallIn(SingletonComponent::class) object AppModule { @Provides @Singleton fun provideOkhttpClient(): OkHttpClient = OkHttpClient().newBuilder() //.addInterceptor(ApiKeyInterceptor()) .addInterceptor(provideLoggingInterceptor()) .connectTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .writeTimeout(30, TimeUnit.SECONDS) .build() @Provides @Singleton fun provideLoggingInterceptor(): HttpLoggingInterceptor { return HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY } } @Provides @Singleton fun provideRetrofit(): EverythingApiService = Retrofit.Builder() .baseUrl(Constants.BASE_URL) .client(provideOkhttpClient()) .addConverterFactory(GsonConverterFactory.create()) .build() .create(EverythingApiService::class.java) @Provides @Singleton fun provideEverythingRepository(apiService: EverythingApiService): EverythingRepository = EverythingRepositoryImpl(apiService) }
==================================================================================================
interface EverythingRepository { suspend fun getEverything(query: kotlin.String = "bitcoin"): Flowlt;Resourcelt;Responselt;NewsResponselt;Everythinggt;gt;gt;gt; }
==================================================================================================
class GetEverythingUseCase @Inject constructor(private val repository: EverythingRepository) { suspend fun execute(): Flowlt;Resourcelt;Responselt;NewsResponselt;Everythinggt;gt;gt;gt; { return repository.getEverything() } }
==================================================================================================
@AndroidEntryPoint class EverythingFragment : BaseFragmentlt;EverythingViewModel, FragmentEverythingBindinggt; (FragmentEverythingBinding::inflate), OnItemClickListener { override val viewModel: EverythingViewModel by viewModels() private val mAdapter = EverythingAdapter(this@EverythingFragment) override fun setupRecyclers() { binding.apply { everythingRecycler.layoutManager = LinearLayoutManager(requireContext()) everythingRecycler.adapter = mAdapter } } override fun setupObservers() { lifecycleScope.launch { viewModel.newsList.observe(viewLifecycleOwner, { mAdapter.submitList(it) Toast.makeText(requireContext(), it.toString(), Toast.LENGTH_SHORT).show() Log.d("you", it.toString()) }) } }
==================================================================================================
@HiltViewModel class EverythingViewModel @Inject constructor(private val getEverythingUseCase: GetEverythingUseCase) : BaseViewModel() { private val _newsList = MutableLiveDatalt;Listlt;Everythinggt;gt;() val newsList: LiveDatalt;Listlt;Everythinggt;gt; get() = _newsList init { getEverything() } private fun getEverything() { viewModelScope.launch { getEverythingUseCase.execute().collect { when (it) { is Resource.Loading -gt; { } is Resource.Error -gt; { } is Resource.Success -gt; { _newsList.value = it.data?.body()?.articles Log.d("your", it.data?.body().toString()) } } } } } }