Can Korkmaz
04/19/2022, 5:35 AM@Module
@InstallIn(SingletonComponent::class)
object DependencyModule {
@Singleton
@Provides
fun provideMoshi(): Moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
@Singleton
@Provides
fun provideRetrofit(moshi: Moshi): BackendApi = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi))
.baseUrl(Constants.BASE_URL)
.build()
.create(BackendApi::class.java)
}
Can Korkmaz
04/19/2022, 5:36 AMinterface BackendApi {
@GET("/")
suspend fun defaultProductFetch(): Notebook
@GET("photos")
suspend fun getPhotos(): List<MarsPhoto>
}
Tried the codelab example url, that neither worked.Can Korkmaz
04/19/2022, 5:38 AM@HiltViewModel
class ProductsViewModel @Inject constructor(private val api: BackendApi): ViewModel(){
val notebookResults: MutableState<Notebook?> = mutableStateOf(null)
val mars: MutableState<List<MarsPhoto>> = mutableStateOf(listOf())
suspend fun getNotebooks(){
withContext(<http://Dispatchers.IO|Dispatchers.IO>){
notebookResults.value = api.defaultProductFetch()
}
}
suspend fun getMarss(){
withContext(<http://Dispatchers.IO|Dispatchers.IO>){
mars.value = api.getPhotos()
}
}
json link: https://jsonkeeper.com/b/6J8T/
@Composable
val jsonProducts: MutableState<Notebook?> = productsViewModel.notebookResults
Can Korkmaz
04/19/2022, 6:17 AMManish Jain
04/19/2022, 6:32 AMCan Korkmaz
04/19/2022, 6:36 AMManish Jain
04/19/2022, 7:22 AMCan Korkmaz
04/19/2022, 7:38 AMChrimaeon
04/19/2022, 7:47 AMNotebook
class definition!?Can Korkmaz
04/19/2022, 7:47 AMdata class Notebook(
val sku: String,
val brand: String,
val model: String,
val price: Int,
val screensize: String,
val resolution: String,
val os: String,
val cpufamily: String?,
val cpu: String,
val memory: Int,
val gpu: String,
val hdd: Any?,
val ssd: Int?,
val quantity: Int,
val warranty: Any?,
val warrantytype: Any?,
val distributor: String,
val adddate: String?
)
Can Korkmaz
04/19/2022, 7:50 AMChrimaeon
04/19/2022, 7:55 AMAny
is not a valid json type. I guess there is no adapter for Any
. Aren't you also missing the annotation?Can Korkmaz
04/19/2022, 8:02 AMManish Jain
04/19/2022, 8:25 AM