Posts

Showing posts with the label Retrofit

Offline Caching in Android Studio

Image
Overview Hello World Code plugins { id 'com.android.application' id 'kotlin-android' id 'kotlin-kapt' id 'dagger.hilt.android.plugin' } // Dagger Hilt def hilt_version = "2.38.1" implementation "com.google.dagger:hilt-android: $hilt_version " kapt "com.google.dagger:hilt-compiler: $hilt_version " // Retrofit def retrofit_version = "2.9.0" implementation "com.squareup.retrofit2:retrofit: $retrofit_version " implementation "com.squareup.retrofit2:converter-gson: $retrofit_version " implementation "com.squareup.okhttp3:logging-interceptor:4.9.1" // Coroutines def coroutines_version = "1.6.0" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core: $coroutines_version " implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android: $coroutines_version " // ViewModel and LiveData def lifecycle_version = "2.4.0...

MVVM Design Pattern in Android

Image
Overview MVVM is a Model-View-ViewModel design pattern architecture that removes the tight coupling between every application components. In the below image, you can clearly see that every children components don't have a reference to the parent components. They have a reference to an observable though which they can communicate with the parent components. Components of MVVM Model It is the data and business logic of an Android application. It's a data class where you can define the properties and methods too. View It is the user interface of an Android application, activities and fragments. These views send the user requests to ViewModel and to get the response, the application components has to subscribe to the observables which the ViewModel has exposed to it. ViewModel It is a bridge between the View and the Model in MVVM design pattern. ViewModel isn't aware which view is interacting with it. ViewModel only interacts with the Model and exposes the observable that can b...

Retrofit in Android

Image
Why are we using Retrofit? We are using Retrofit because we are not allowed to perform network calls on main thread after the Android version Honeycomb. What is Retrofit? Retrofit is a HTTP networking library that is used for Android and Java and it is type-safe . Type of converters used in Retrofit GSON Jackson Moshi Wire Simple XML etc. Can you create a Custom Converter? Yes we can create a custom converter with the help of Converter.Factory class. Once created we can use the instance this class in our adapter.  Follow these steps to use Retrofit. Add the required dependency in buld.gradle file (Module) Add internet permission in Android.Manifest.xml file. Design the user interface to show the response on the screen. Create a Model class. Establish a connection with Retrofit and create the request. Dependencies used for Retrofit: def retrofit_version = "2.9.0" implementation "com.squareup.retrofit2:retrofit: $retrofit_version " implementation "com.squareup...