Home > Back-end >  I keep getting the error "E/Network: searchBooks: Failed Getting books" but I am not sure
I keep getting the error "E/Network: searchBooks: Failed Getting books" but I am not sure

Time:10-10

So I am using the Google's API and for some reason, I'm getting a generic error:

E/Network: searchBooks: Failed Getting books

When it initially loads up, the hard coded query "android" shows up with a list of books associated with the book topic. But when I search up a different topic like "shoes" for example, the error shows up. Even when you hard code a different topic other than "android", it still shows the error. I have checked the API and it is working properly with the different query searches.

Here's the Retrofit Interface:

@Singleton
interface BooksApi {
    @GET(BOOK_EP)
    suspend fun getAllBooks(
        //don't initialize the query, so that the whole api is available to the user
        @Query("q") query: String
    ): Book

    @GET("$BOOK_EP/{bookId}")
    suspend fun getBookInfo(
        @Path("bookId") bookId: String
    ): Item
}

The Repo

class BookRepository @Inject constructor(private val api: BooksApi) {

    suspend fun getBooks(searchQuery: String): Resource<List<Item>> {
        return try {
            Resource.Loading(data = true)

            val itemList = api.getAllBooks(searchQuery).items
            if(itemList.isNotEmpty()) Resource.Loading(data = false)
            Resource.Success(data = itemList)
        }catch (exception: Exception){
            Resource.Error(message = exception.message.toString())
        }
    }

    suspend fun getBookInfo(bookId: String): Resource<Item>{
        val response = try {
            Resource.Loading(data = true)
            api.getBookInfo(bookId)
        }catch (exception: Exception){
            return Resource.Error(message = "An error occurred ${exception.message.toString()}")
        }
        Resource.Loading(data = false)
        return Resource.Success(data = response)
    }

The ViewModel:

class SearchViewModel @Inject constructor(private val repository: BookRepository): ViewModel(){

    var list: List<Item> by mutableStateOf(listOf())
    var isLoading: Boolean by mutableStateOf(true)

    init {
        loadBooks()
    }

    private fun loadBooks() {
        searchBooks("android")
    }

     fun searchBooks(query: String) {
        viewModelScope.launch(Dispatchers.Default) {
            if (query.isEmpty()){
                return@launch
            }
            try {
                when(val response = repository.getBooks(query)){
                    is Resource.Success -> {
                        list = response.data!!
                        if (list.isNotEmpty()) isLoading = false
                    }
                    is Resource.Error -> {
                        isLoading = false
                        Log.e("Network", "searchBooks: Failed Getting books", )
                    }
                    else -> {isLoading = false}
                }
            }catch (exception: Exception){
                isLoading = false
                Log.d("Network", "searchBooks: ${exception.message.toString()}")
            }
        }

     }
}

I'll leave the project public so you guys can check it out for more of an understanding Github Link: https://github.com/OEThe11/ReadersApp

P.S. you would have to create a login (takes 30 sec), but once you do, you'll have access to the app immediately.

CodePudding user response:

This issue is occurring because of JsonSyntaxException java.lang.NumberFormatException while the JSON response is getting parsed from the API. This is because the averageRating field in the VolumeInfo data class is declared as Int but the response can contain floating point values.

If you change averageRating field type from Int to Double in the VolumeInfo data class, the exception would no longer occur.

I suggest you to debug your code in such cases.

  • Related