Unable to create call, trying to fetch my firebase database into recyclerview via retrofit

Viewed 65

Had trouble even finding some useful resources on Firebase+Retrofit (contrary to overpopulated tutorials on other aspects of Android). First time using Firebase, i've imported a json:

{"Qoutes" :[
   {
      "quote":"I\u2019m sure you are tired counting the years of your age. May these years be endless and full of happiness!",
      "relation":"grandmother"
   },
   {
      "quote":"My sweet Grandma, I wish you to be always happy and healthy as today! Happy birthday my beloved old woman!",
      "relation":"grandmother"
   },
   {
      "quote":"May you stay healthy and happy for the years to come. We are all happy to have you here with us!",
      "relation":"grandmother"
   },
.
.
.
]}

Wish class

data class Wish(
    @SerializedName("quote")
    val quote: String?,
    @SerializedName("relation")
    val relation: String?
)

APIService interface

interface APIService {

    @GET("Qoutes")
    suspend fun getQuotes(): Response<Wish>

}

RetrofitInstance

object RetrofitInstance {

    private val retrofit: Retrofit by lazy {
        Retrofit.Builder()
            .baseUrl("https://birthdayzheimer-default-rtdb.firebaseio.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    val api: APIService by lazy {
        retrofit.create(APIService::class.java)
    }

}

Repository

class Repository @Inject constructor() {

    suspend fun getWishes(): Response<Wish> {
        return RetrofitInstance.api.getQuotes()
    }
}

ViewModel

private val repository: Repository
    ): ViewModel() {

    var myResponse: MutableLiveData<Response<Wish>> = MutableLiveData()

    fun getQuotes() {
        viewModelScope.launch {
            val response: Response<Wish> = repository.getWishes()
            myResponse.value = response
        }
    }

and the relevant part inside the fragment

viewModel.getQuotes()
        viewModel.myResponse.observe(viewLifecycleOwner, { response ->
            if(response.isSuccessful)
                Log.d("Response ->", response.body().toString())
            else
                Log.d("Response ->", response.errorBody().toString())

I hope that it's a noobish mistake somewhere as this is all fresh to me, and that it's not too bothersome to read all the classes. Thanks in advance to everyone reading.

P.s. i know i wrote quotes wrong in json, so i was consistent and wrote it wrongfully same everywhere else :D

1 Answers

What you're trying to do here is access the Firebase Realtime Database through its REST API. To make sure this works, you need to end the URL with .json

So:

Retrofit.Builder()
            .baseUrl("https://birthdayzheimer-default-rtdb.firebaseio.com/.json")

Note that there might be multiple problems, but this is certainly one of them.

Related