Retrofit + OkHttp3 + coroutines + LiveData打造一款网络请求框架

Overview

Network-Demo

Retrofit + OkHttp3 + coroutines + LiveData打造一款网络请求框架

一个好的网络框架需要有那些特点呢?

  • 请求 当然这个请求不单单是发送请求这么简单,它包括请求相关的一系列配置是否简易、发送模式是否灵活切换、请求头信息是否易处理、请求参数是否易操作等等
  • 响应 一个好的网络请求框架肯定需要提高我们的生产力,保证我们程序的健壮性,那么响应体和这些又有什么关系呢? 容易转换成不同需求的实体 是否能返回不同类型的结构。xml、json、text等等

总的来说就是易使用、易扩展、可读性高


一、目的

  • 背景

市场上现在存在的网络请求不在少数,从原生的HttpClient 到 OkHttp、Volley、xUtils 无疑都是将网络请求简单化、方便、安全等用以提升开发的效率以及程序的质量。后面出现Retrofit ,将OkHttp3 进行了封装,将请求API 接口化,将返回的数据结合GSON等各种转换器转换为直接面向开发的对象,大大的提升了我们的开发效率,为了解决Android UI/子线程 负责的功能场景方便切换,大家开始结合了RxJava, 这一操作直接将Retrofit + OkHttp3 + RxJava 组合的网络框架推上了热门写法,

  • 现状

kotlin 的出现是Google 对于Android开发的重新定义,含糊不清的态度,既不丢弃Java又宣布kotlin 是首选语言。在次基础上结合jetpack框架,成了新时代开发的另一条选择路径,从始至终Google 没有对Android推出过官方的设计模式,大家都是从高内聚、低耦合等方面推行着自己的设计模式,让开发简单、维护简单、程序健壮。jetpack是Google 对设计模式的首次官方制造,重点可想而知,那在这个基础上我们还需要使用之前的网络框架吗?有没有更加优秀的框架以及框架的组合使得我们的程序健壮、开发简易呢?

  • jetpack

具体的jetpack不是这里解释的。其中ViewModel - LiveData 已经得到了广大开发者的认可,ViewModel 和LiveData 的结合,使得Android中数据可控性变得更好,耦合度更低,简单来说是官方将观察者模式用于到了真个数据结构中,在这里也是将要结合其他框架来完成网络框架的设计

  • coroutines

kotlin较Java的最大变化就是kotlin推出的coroutines (协程),协程完全可以替代RxJava, Thread、多级接口回调等,并且有上下文及各种模式来迎合各种场景,具体不再这里解释,

  • 使用上述出现的几个框架完成新的网络请求框架的封装
You might also like...
Kotlin-REST-Retrofit - Simple client to consume a REST API with Retrofit using Kotlin
Kotlin-REST-Retrofit - Simple client to consume a REST API with Retrofit using Kotlin

Kotlin REST Retrofit Sencillo cliente para consumir una API REST con Retrofit us

CoroutinesFlow and ViewModel Retrofit API - Practice with CoroutinesFlow, ViewModel and Retrofit API

Practice with Coroutines Flow, ViewModel and get data with Retrofit from two API

 🍲Foodium is a sample food blog Android application 📱 built to demonstrate the use of Modern Android development tools - (Kotlin, Coroutines, Flow, Dagger 2/Hilt, Architecture Components, MVVM, Room, Retrofit, Moshi, Material Components). Android News Reader app. Kotlin Coroutines, Retrofit and Realm
Android News Reader app. Kotlin Coroutines, Retrofit and Realm

News Reader Android News Reader app Code that follows Packt Publishing Kotlin in Practice Video Course Example of Kotlin Coroutine usage, with Realm a

 🍲Foodium is a sample food blog Android application 📱 built to demonstrate the use of Modern Android development tools - (Kotlin, Coroutines, Flow, Dagger 2/Hilt, Architecture Components, MVVM, Room, Retrofit, Moshi, Material Components). Android News Reader app. Kotlin Coroutines, Retrofit and Realm
Android News Reader app. Kotlin Coroutines, Retrofit and Realm

News Reader Android News Reader app Code that follows Packt Publishing Kotlin in Practice Video Course Example of Kotlin Coroutine usage, with Realm a

Android News Reader app. Kotlin Coroutines, Retrofit and Realm
Android News Reader app. Kotlin Coroutines, Retrofit and Realm

News Reader Android News Reader app Code that follows Packt Publishing Kotlin in Practice Video Course Example of Kotlin Coroutine usage, with Realm a

A sample project in Kotlin to demonstrate AndroidX, MVVM, Coroutines, Hilt, Room, Data Binding, View Binding, Retrofit, Moshi, Leak Canary and Repository pattern.

This repository contains a sample project in Kotlin to demonstrate AndroidX, MVVM, Coroutines, Hilt, Room, Data Binding, View Binding, Retrofit, Moshi, Leak Canary and Repository pattern

Android sample app following best practices: Kotlin, Compose, Coroutines and Flow, Hilt, JetPack Navigation, ViewModel, MVVM and MVI, Retrofit, Coil
Android sample app following best practices: Kotlin, Compose, Coroutines and Flow, Hilt, JetPack Navigation, ViewModel, MVVM and MVI, Retrofit, Coil

Foodies - Modern Android Architecture Foodies is a sample project that presents a modern 2021 approach to Android app development. The project tries t

Wallpaper app made using Hilt, Retrofit, Room, Navigation Components, MVI, Coroutines, Flows, ViewModel, LiveData, Datastore Preference.
Wallpaper app made using Hilt, Retrofit, Room, Navigation Components, MVI, Coroutines, Flows, ViewModel, LiveData, Datastore Preference.

Android Picture Engine Wallpaper app made using Hilt, Retrofit, Room, Navigation Components, MVI, Coroutines, Flows, ViewModel, LiveData, Datastore Pr

Sample news app using Kotlin, Hilt, Coroutines, Coil, Room, Retrofit

Tech stack & News App libraries Navigation component - navigation graph for navigating and replacing screens/fragments DataBinding - allows to more ea

A sample to showcase Kotlin, MVVM, Koin, Coroutines, StateFlow, Room, WorkManager, Retrofit and Unit test.

TVMaze-Cache A sample to showcase Kotlin, MVVM, Koin, Coroutines, StateFlow, Room, WorkManager, Retrofit and Unit test. Features MVVM Architecture + R

NewsApp - Modern Minimalistic Design, MVVM, Pagination, Retrofit, Coroutines, Room, Glide, Navigation Component (Clean Architecture)
NewsApp - Modern Minimalistic Design, MVVM, Pagination, Retrofit, Coroutines, Room, Glide, Navigation Component (Clean Architecture)

NewsApp is a modern news android application which features virtually ALL recent and recommended android development tech stack and tools used

The News App has been carried out within the framework of the MVVM architecture, information about news is obtained by consulting an API, it is built usisng Jetpack Copose, Coroutines, Dependency Injection with Hilt and Retrofit

Journalist The News App consists of an application that displays the latest news from EEUU from an API that provides official and updated information.

Finder Job simple app using (Retrofit , Dagger hilt , coroutines , navigation components)
Finder Job simple app using (Retrofit , Dagger hilt , coroutines , navigation components)

Job Finder I'm finished building a simple project Job Finder App technology used [dagger hilt, coroutines, navigation components, LiveData, Skelton pa

TzRecipes App With Retrofit,Coroutines,Flow
TzRecipes App With Retrofit,Coroutines,Flow

TzRecipesApp Main Stack : Retrofit,Coroutines,Flow,LiveData,Mvvm SOLID,OOP,ClenArchitecture Разбиение приложения на data, domain, presentation слои Ма

🔥Simple quote app using MVVM, Retrofit, Coroutines and Dagger Hilt 💉

🔥 simple quote app using MVVM, Retrofit, Coroutines and Dagger Hilt 💉 quote.mp4 📚 knowledges and technologies ViewBinding Retrofit Coroutines MVVM

🔥Application created with MVVM, Retrofit, Coroutines, SWAPI and shimmer effect.

🔥 Application created with MVVM, Retrofit, Coroutines, SWAPI and shimmer effect. gif.mp4 📚 knowledges and technologies ViewBinding Retrofit Coroutin

Android weather app using Hilt, Coroutines, Retrofit, Jetpack (Compose, Room, ViewModel) based on MVVM architecture
Android weather app using Hilt, Coroutines, Retrofit, Jetpack (Compose, Room, ViewModel) based on MVVM architecture

Atmostate A weather app written with Jetpack Compose using OpenWeatherMap Get the APK Features offline caching fetching data for current user location

Owner
图你怀中安稳
图你怀中安稳
Mock your datas for Okhttp and Retrofit in json format in just a few moves

okhttp-json-mock This simple library helps you mock your data for using with okhttp+retrofit in json format in just a few moves. it forwards the reque

Mad Mirrajabi 240 Nov 25, 2022
Latihan Networking dengan Retrofit

Latihan-Background-Process-dan-Networking-9 Latihan Networking dengan Retrofit Pada materi kali ini Anda akan belajar menggunakan Retrofit untuk menam

Ubean 0 Nov 25, 2021
Exercicio praticando Retrofit

MarsPhotos - Starter Code Starter code for Android Basics in Kotlin. Introduction Using this stater code you will create MarsPhotos is a demo app that

Lucas Caetano 1 Nov 26, 2021
기존 post-sample Repository 공부한 기술들로 다시 만들어보기 (MVVM, Hilt, Coroutine/Flow, DataBinding, Retrofit, Moshi, Jetpack)

post-sample-hilt-jetpack 기존 post-sample Repository에서 새로 학습한 부분 도입 기존 코드 변경 (Observe) LiveData -> Flow / StateFlow (DI) Dagger2 -> Hilt (Asynchronous)

훈성 9 Nov 15, 2022
Retrofit Flow Call Adapter Factory For Android

Summary Android Retrofit FlowCallAdapterFactory Retrofit 2 CallAdapter.Factory f

TaeHwan 12 Aug 3, 2022
A basic list application designed using MVVM pattern with Retrofit, OkHttp, and Hilt for dependency injection

FRExercise A basic list application designed using MVVM pattern with Retrofit, O

null 0 Dec 22, 2021
Ktorfit - a HTTP client/Kotlin Symbol Processor for Kotlin Multiplatform (Js, Jvm, Android, iOS, Linux) using KSP and Ktor clients inspired by Retrofit

Ktorfit is a HTTP client/Kotlin Symbol Processor for Kotlin Multiplatform (Js, Jvm, Android, iOS, Linux) using KSP and Ktor clients inspired by Retrofit

Jens Klingenberg 637 Dec 25, 2022
The JeTrivia is built on a modern Android Development tech stack with MVVM architecture. Kotlin, Coroutine, Flow, StateFlow, Jetpack Compose, Navigation, Room, Hilt, Retrofit2, OkHttp3, kotlinx.serialization, MockK, Truth

JeTrivia ?? In Progress ?? The JeTrivia application is sample based on MVVM architecture. Fetching data from the network via repository pattern and sa

Tolga Bolatcan 5 Mar 31, 2022
(Coroutine, Flow(+StateFlow), Hilt, JetPack, MVVM, Repository Pattern, Retrofit2 & OkHttp3, Moshi, Glide, Timber, Material-Components)

(Coroutine, Flow(+StateFlow), Hilt, JetPack, MVVM, Repository Pattern, Retrofit2 & OkHttp3, Moshi, Glide, Timber, Material-Components)

훈성 2 Nov 15, 2022