Home > database >  Nothing is in the index when I make the network call for Android. How do I fix that?
Nothing is in the index when I make the network call for Android. How do I fix that?

Time:08-22

So, following up from a previous question, I have made some corrections to call the actual object instead of calling it as a list, but now I have a new error.

E/VM: Index: 0, Size: 0

This is coming from the view model. I am assuming that there is no items that got to the list. Which would mean that the mapper class didn't work. Which means the database has nothing.

Here's the interface

@Singleton
interface AnywhereAPI {
    @GET(".")
    suspend fun getAnyInfo(
        @Query("q") query : String = "the wire characters&format=json",
        @Query("format") format : String = "json"
    ): Response<GetAnyResponse>
}

The Repo

class AnywhereRepository @Inject constructor(
    private val api: AnywhereAPI,
    private val anywhereDao: AnywhereDao
){


//    suspend fun getAllInfos(): DataOrException<GetAnyResponse, Boolean, Exception>{
//        val response = try {
//          api.getAnyInfo()
//        }catch (e: Exception){
//            Log.d("REPO", "getAllInfos: $e")
//            return DataOrException(e = e)
//        }
//        Log.d("REPO INSIDE", "getAllInfos: $response")
//        return DataOrException(data = response)
//    }

    val feeds: Flow<List<AnywhereListEntity>>
        get() = anywhereDao.getInfo()


    suspend fun anywhereInfo(): List<AnywhereListEntity>? {
        val request = api.getAnyInfo()
        if (request.isSuccessful){
            val anyItems = request.body()!!.let {
                AnywhereMapper.buildFrom(it)
            }
            anywhereDao.insertInfo(listOf(anyItems))
            return (listOf(anyItems))
        }
        return null
    }
}

The ViewModel

@HiltViewModel
class AnyViewModel @Inject constructor(
    private val repository: AnywhereRepository
): ViewModel() {
   val anyInfoResults = repository.feeds


    init {
        getAnyList()
    }

    private fun getAnyList(){
        viewModelScope.launch {
            try {
                repository.anywhereInfo()
            }catch (e: Exception){
                Log.e("VM",e.message, e.cause)
            }
        }
    }

}

The Main Screen

@Composable
fun MainScreen(anyViewModel: AnyViewModel = hiltViewModel()){
    val allItems by anyViewModel.anyInfoResults.collectAsState(initial = emptyList())

    Surface(modifier = Modifier.fillMaxSize(),
            color = MaterialTheme.colors.primary
    ) {
        AnyList(list = allItems)
    }
}

@Composable
fun AnyList(list: List<AnywhereListEntity>) {
    LazyColumn{
        items(list){item ->
            AnyCard(anyItems = item)
        }
    }
}

@Composable
fun AnyCard(anyItems: AnywhereListEntity) {
    Card  (modifier = Modifier
        .fillMaxWidth()
        .height(110.dp)
        .padding(16.dp),
        shape = RoundedCornerShape(size = 20.dp),
        backgroundColor = MaterialTheme.colors.primaryVariant,
        elevation = 11.dp
    ) {
        Row(horizontalArrangement = Arrangement.Center) {
            Text(text = anyItems.name)
        }
    }
}

Not sure how to get the info I need to save in the database, but any help is appreciated. I'll leave my updated GitHub project link here and the URL for the API. Thank you.

GitHub Link: https://github.com/OEThe11/AnywhereCE

API link: http://api.duckduckgo.com/?q=the wire characters&format=json

CodePudding user response:

You're passing the search (query) string incorrectly. Instead of

        @Query("q") query : String = "the wire characters&format=json",

it should be

        @Query("q") query : String = "the wire characters",
  • Related