2

I'm trying to wrap my head around the mentioned components and I can't get it right. I want to do something very simple: Fetch data from the network and present it to the user. Currently am not yet caching it as am still learning new Coroutine features in Architecture components. Every time app loads I get an empty model posted, which seems weird.

My API is get hit fine and response is 200 which is OK.

Below is what I have attempted:

POJO

data class Profile(@SerializedName("fullname") val fullName : String.....)

Repository

class UserRepo(val context: Context, val api: Api) {

    suspend fun getProfile(): Profile
    {
        val accessToken = ....
        return api.getUserProfile(accessToken)

    }
}

API

interface GatewayApi {
    @GET("users/profile")
    suspend fun getUserProfile(@Query("access-token") accessToken: String?): Profile
}

ViewModel

class UserViewModel(application: Application) : AndroidViewModel(application) {
    private val usersRepo = UserRepo(application.applicationContext, Apifactory.Api)
    val userProfileData = liveData{
        emit(usersRepo.getProfile())
    }

    fun getProfile() = viewModelScope.launch {
        usersRepo.getProfile()
    }
}

Finally my fragment's relevant code

val viewModel = ViewModelProviders.of(activity!!).get(UserViewModel::class.java)
viewModel.userProfileData.observe(this, Observer<UserProfile> {
     //it is having nulls
 })

//trigger change
viewModel.getProfile()
Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93
  • 1
    "Every time app loads I get an empty model posted" -- this suggests that Retrofit is having difficulty converting the server response into the designated object. You might consider adding an OkHttp logging interceptor to see what the raw response looks like and see if it matches your expectations. – CommonsWare Aug 07 '19 at 22:28
  • brilliant idea. I will add that. Do you find any fault with my current code? I will appreciate criticism also apart from what I asked – Stefano Mtangoo Aug 07 '19 at 22:41
  • The snippets that I see seem OK. – CommonsWare Aug 07 '19 at 22:45
  • Thanks for checking! – Stefano Mtangoo Aug 07 '19 at 22:46
  • I recommend wrapping `userRepo.getProfile()` in a try/catch and then see if an error is being caught. – user2836202 Aug 08 '19 at 02:14
  • @CommonsWare answer turned to be the cause. I used wrong model and GSON had hard time serializing it. Can you post as an answer so that I accept it? Thank you user2836202, thanks for the comment, but there were no exceptions thrown in this case – Stefano Mtangoo Aug 08 '19 at 09:48
  • I recommend that you answer your own question, as you can explain your circumstances and findings better than I can. I am glad to hear that you got it working! – CommonsWare Aug 08 '19 at 11:09
  • Thank you. I always love to give due credence to the owner. Thank you for help! – Stefano Mtangoo Aug 08 '19 at 12:29

1 Answers1

0

So I added HTTP requests and responses (thanks to @CommonsWare for pointing that out) and it happened I had used a different model than I was supposed to use. The correct model that mapped the JSON response was ProfileResponse and as you can see in my posted code, I used Profile instead. So all fields were empty as Gson could not correctly serialize JSON into Profile object.

All the credit goes to @CommonsWare for pointing that out in comment.

Stefano Mtangoo
  • 6,017
  • 6
  • 47
  • 93