Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moster_02 get data from web service #2

Merged
merged 7 commits into from
Apr 15, 2020

Conversation

AnelCC
Copy link
Owner

@AnelCC AnelCC commented Apr 15, 2020

Get data from web service

1. Acquire data in a repository class

Check the Respository Here

2. Share data with LiveData objects

The ViewModel is simply passing that LiveData object back to the user interface.
And the fragment, that is the user interface,
is only responsible for managing the presentation

private val dataRepo = MonsterRepository(app)
val monsterData = dataRepo.monsterData

3. Network permissions and status

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

4. Add Retrofit and coroutine dependencies

view models and data, coroutines and retrofit2

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
implementation 'com.squareup.retrofit2:converter-moshi:2.6.0'
implementation 'com.squareup.retrofit2:retrofit:2.6.0'

5. Define a Retrofit interface

Add the keyword suspend.
That means that this function is designed to be called from with a coroutine.
Coroutines can run either on the UI thread or in a background thread, but for a function to be part of a coroutine call, it must have the suspend keyword at the beginning of the function declaration.

6. Retrieve remote data with Retrofit

Screenshot_1586988410

@AnelCC AnelCC merged commit c0c9766 into master Apr 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant