Home > Software engineering >  Not gettng the full list of names from the API call in Android
Not gettng the full list of names from the API call in Android

Time:08-24

So after getting the API call to work, I only have one item out of a list of items that is inside the API.

Here's the interface

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

}

Here's the DAO

@Dao
interface AnywhereDao {
    @Query("SELECT * FROM any_name")
    fun getInfo(): Flow<List<AnywhereListEntity>>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertInfo(info: List<AnywhereListEntity>)
}

Here's the repository

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{
                it.RelatedTopics.forEach{item ->
                 AnywhereMapper.buildfrom(item)
              }
            }
            anywhereDao.insertInfo(listOf(anyItems))
            return listOf(anyItems)
        }
        return null
    }
}

I believe the problem is in the repository. And how it is not being translated properly to show the rest of the list. Particularly with the anywhereInfo(). I put the mapper in a forEach block so that it can look through that list and map it to the AnywhereListEntity data class. But now the insert function to insert the data into the database isn't accepting the anyItems as an input. Any help will be appreciated. I'll leave my GitHub link to the project and the API URL for reference. Thank You.

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

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

CodePudding user response:

I repaired your project, it now shows many of entities. Two files needs to be changed, first to change is AnywhereRepository:

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

There are two important changes:

  • usage of map method, instead of forEach -> map creates list of single element that you defined in curly braces, forEach does not.
  • not creating list of anyItems on insert and return instruction - variable anyItems is already a list

There is important change in AnywhereMapper too - it have to be written, othercase app will throw exception on last element from API response:

object AnywhereMapper {
    fun buildFrom(response: RelatedTopic): AnywhereListEntity {
        return AnywhereListEntity(
            name = response.parts[0],
            description = if (response.parts.size > 1)
                response.parts[1]
            else
                "" //last element not have this field
        )
    }
}
  • Related