RxHttp - 輕量級、可擴(kuò)展、易使用、完美兼容MVVM、MVC架構(gòu)的網(wǎng)絡(luò)封裝類庫

前言

RxHttp是基于RxJava2+Retrofit 2.9.0+OkHttp 4.9.0實(shí)現(xiàn)的輕量級,完美兼容MVVM架構(gòu)的網(wǎng)絡(luò)請求封裝類庫,小巧精致,簡單易用,輕輕松松搞定網(wǎng)絡(luò)請求。

GitHub

https://github.com/kongpf8848/RxHttp

亮點(diǎn)

  • 代碼量極少,類庫大小不足100kb,但足以勝任大部分APP的網(wǎng)絡(luò)請求任務(wù),濃縮的都是精華啊^

  • 完美兼容MVVM,MVC架構(gòu),兼容Kotlin和Java,Kotlin+MVVM+RxHttp組合使用更酸爽,MVVM官方推薦,抱緊Google大腿就對了

  • 完美解決泛型類型擦除的棘手問題,還原泛型的真實(shí)類型

  • 天生支持網(wǎng)絡(luò)請求和Activity,F(xiàn)ragment生命周期綁定,界面銷毀時自動取消網(wǎng)絡(luò)請求回調(diào)

  • 天生支持多BaseUrl,支持動態(tài)傳入Url

  • 支持自定義OkHttpClient.Builder,可高度自定義網(wǎng)絡(luò)請求參數(shù)

  • 支持Glide等和網(wǎng)絡(luò)請求公用一個OkHttpClient,充分利用OkHttpClient的線程池和連接池,大部分情況下一個App一個OkHttpClient就夠了

  • 支持GET,POST,PUT,DELETE等請求方式,支持文件上傳及進(jìn)度監(jiān)聽,支持同時上傳多個文件,支持Uri上傳

  • 支持文件下載及進(jìn)度監(jiān)聽,支持大文件下載,支持?jǐn)帱c(diǎn)下載

使用要求

項(xiàng)目基于AndroidX,Java8+,minSdkVersion>=21

使用

implementation 'com.github.kongpf8848:RxHttp:1.0.11'

配置(可選)

  RxHttpConfig.getInstance()
    /**
     * 失敗重試次數(shù)
     */
    .maxRetries(3)
    /**
     * 每次失敗重試間隔時間
     */
    .retryDelayMillis(200)
    /**
     * 自定義OkHttpClient.Builder(),RxHttp支持自定義OkHttpClient.Builder(),
     * 如不定義,則使用RxHttp默認(rèn)的OkHttpClient.Builder()
     */
    .builder(OkHttpClient.Builder().apply {
        connectTimeout(60, TimeUnit.SECONDS)
        readTimeout(60, TimeUnit.SECONDS)
        writeTimeout(60, TimeUnit.SECONDS)
        /**
         * DEBUG模式下,添加日志攔截器,建議使用RxHttp中的FixHttpLoggingInterceptor,使用OkHttp的HttpLoggingInterceptor在上傳下載的時候會有IOException問題
         */
        if (BuildConfig.DEBUG) {
            addInterceptor(FixHttpLoggingInterceptor().apply {
                level = FixHttpLoggingInterceptor.Level.BODY
            })
        }
    })

基礎(chǔ)使用

  • GET/POST/PUT/DELETE/上傳請求
   RxHttp.getInstance()
    /**
     * get:請求類型,可為get,post,put,delete,upload,分別對應(yīng)GET/POST/PUT/DELETE/上傳請求
     * context:上下文,可為Context,Activity或Fragment類型,當(dāng)context為Activity或Fragment時網(wǎng)絡(luò)請求和生命周期綁定
     */
    .get(context)
    /**
     * 請求url,如https://www.baidu.com
     */
    .url("xxx")
    /**
     *請求參數(shù)鍵值對,類型為Map<String, Any?>?,如hashMapOf("name" to "jack")
     */
    .params(map)
    /**
     *每個網(wǎng)絡(luò)請求對應(yīng)的tag值,可為null,用于后續(xù)手動根據(jù)tag取消指定網(wǎng)絡(luò)請求
     */
    .tag("xxx")
    /**
     * HttpCallback:網(wǎng)絡(luò)回調(diào),參數(shù)xxx為返回?cái)?shù)據(jù)對應(yīng)的數(shù)據(jù)模型,
     * 類似RxJava中的Observer,onComplete只有在onNext回調(diào)之后執(zhí)行,如發(fā)生錯誤則只會回調(diào)onError而不會執(zhí)行onComplete
     */
    .enqueue(object : HttpCallback<xxx>() {
        /**
         * http請求開始時回調(diào)
         */
        override fun onStart() {

        }

        /**
         * http請求成功時回調(diào)
         */
        override fun onNext(response: xxx?) {

        }

        /**
         * http請求失敗時回調(diào)
         */
        override fun onError(e: Throwable?) {

        }

        /**
         * http請求成功完成時回調(diào)
         */
        override fun onComplete() {

        }

        /**
         * 上傳進(jìn)度回調(diào),請求類型為upload時才會回調(diào)
         */
        override fun onProgress(readBytes: Long, totalBytes: Long) {
        
        }
    })
  • 下載請求
   RxHttp.getInstance()
      /**
       * download:請求類型,下載請求
       * context:上下文,如不需要和生命周期綁定,應(yīng)該傳遞applicationContext
       */
      .download(context)
      /**
       * 保存路徑
       */
      .dir(dir)
      /**
       *保存文件名稱
       */
      .filename(filename)
      /**
       * 是否為斷點(diǎn)下載,默認(rèn)為false
       */
      .breakpoint(true)
      /**
       * 下載地址,如http://study.163.com/pub/ucmooc/ucmooc-android-official.apk
       */
      .url(url)
      /**
       * 請求Tag
       */
      .tag(null)
      /**
       * 下載回調(diào)
       */
      .enqueue(object: DownloadCallback() {
          /**
           * 下載開始時回調(diào)
           */
          override fun onStart() {
    
          }
    
          /**
           * 下載完成時回調(diào)
           */
          override fun onNext(response: DownloadInfo?) {
    
          }
    
          /**
           * 下載失敗時回調(diào)
           */
          override fun onError(e: Throwable?) {
    
          }
    
          /**
           * 下載完成之后回調(diào)
           */
          override fun onComplete() {
    
          }
    
          /**
           * 下載進(jìn)度回調(diào)
           */
          override fun onProgress(readBytes: Long, totalBytes: Long) {
    
          }
    
      })

  • 取消請求
    /**
     * tag:Any?,請求Tag,對應(yīng)網(wǎng)絡(luò)請求里的Tag值
     * 如不為null,則取消指定網(wǎng)絡(luò)請求,
     * 如為null,則取消所有網(wǎng)絡(luò)請求
     */
    RxHttp.getInstance().cancelRequest(tag)

項(xiàng)目實(shí)戰(zhàn)

此處假設(shè)服務(wù)端返回的數(shù)據(jù)格式為{"code":xxx,"data":T,"msg":""},其中code為響應(yīng)碼,整型,等于200時為成功,其余為失敗,data對應(yīng)的數(shù)據(jù)類型為泛型(boolean,int,double,String,對象{ },數(shù)組[ ]等類型)

{
   "code": 200,
   "data":T,
   "msg": ""
}

對應(yīng)的Response類為

class TKResponse<T>(val code:Int,val msg: String?, val data: T?) : Serializable {
    companion object{
        const val STATUS_OK=200
    }
    fun isSuccess():Boolean{
        return code== STATUS_OK
    }
}
  • MVC項(xiàng)目

    • 定義MVCHttpCallback,用于將網(wǎng)絡(luò)請求結(jié)果回調(diào)給UI界面
    abstract class MVCHttpCallback<T> {
    
        private val type: Type
    
        init {
            val arg = TypeUtil.getType(javaClass)
            type = TypeBuilder
                    .newInstance(TKResponse::class.java)
                    .addTypeParam(arg)
                    .build()
        }
    
        fun getType(): Type {
            return this.type
        }
    
        /**
         * 請求開始時回調(diào),可以在此加載loading對話框等,默認(rèn)為空實(shí)現(xiàn)
         */
        open fun onStart() {}
       
        /**
         * 抽象方法,請求成功回調(diào),返回內(nèi)容為泛型,對應(yīng)TKResponse的data
         */
        abstract fun onSuccess(result: T?)
    
        /**
         * 抽象方法,請求失敗回調(diào),返回內(nèi)容為code(錯誤碼),msg(錯誤信息)
         */
        abstract fun onFailure(code: Int, msg: String?)
    
        /**
         * 上傳進(jìn)度回調(diào),默認(rèn)為空實(shí)現(xiàn)
         */
        open fun onProgress(readBytes: Long, totalBytes: Long) {}
    
        /**
         * 請求完成時回調(diào),請求成功之后才會回調(diào)此方法,默認(rèn)為空實(shí)現(xiàn)
         */
        open fun onComplete() {}
    
    }
    
    • 定義網(wǎng)絡(luò)接口,封裝GET/POST等網(wǎng)絡(luò)請求
    object MVCApi {
    
        /**
         * GET請求
         * context:上下文
         * url:請求url
         * params:參數(shù)列表,可為null
         * tag:標(biāo)識一個網(wǎng)絡(luò)請求
         * callback:網(wǎng)絡(luò)請求回調(diào)
         */
        inline fun <reified T> httpGet(
                context: Context,
                url: String,
                params: Map<String, Any?>?,
                tag: Any? = null,
                callback: MVCHttpCallback<T>
        ) {
            RxHttp.getInstance().get(context)
                    .url(url)
                    .params(params)
                    .tag(tag)
                    .enqueue(simpleHttpCallback(callback))
        }
    
        /**
         * POST請求
         * context:上下文
         * url:請求url
         * params:參數(shù)列表,可為null
         * tag:標(biāo)識一個網(wǎng)絡(luò)請求
         * callback:網(wǎng)絡(luò)請求回調(diào)
         */
        inline fun <reified T> httpPost(
                context: Context,
                url: String,
                params: Map<String, Any?>?,
                tag: Any? = null,
                callback: MVCHttpCallback<T>
        ) {
            RxHttp.getInstance().post(context)
                    .url(url)
                    .params(params)
                    .tag(tag)
                    .enqueue(simpleHttpCallback(callback))
        }
        
        ......
        
         inline fun <reified T> simpleHttpCallback(callback: MVCHttpCallback<T>): HttpCallback<TKResponse<T>> {
            return object : HttpCallback<TKResponse<T>>(callback.getType()) {
                override fun onStart() {
                    super.onStart()
                    callback.onStart()
                }
    
                override fun onNext(response: TKResponse<T>?) {
                    if (response != null) {
                        if (response.isSuccess()) {
                            callback.onSuccess(response.data)
                        } else {
                            return onError(ServerException(response.code, response.msg))
                        }
    
                    } else {
                        return onError(NullResponseException(TKErrorCode.ERRCODE_RESPONSE_NULL, TKErrorCode.ERRCODE_RESPONSE_NULL_DESC))
                    }
    
                }
    
                override fun onError(e: Throwable?) {
                    handleThrowable(e).run {
                        callback.onFailure(first, second)
                    }
                }
    
                override fun onComplete() {
                    super.onComplete()
                    callback.onComplete()
                }
    
                override fun onProgress(readBytes: Long, totalBytes: Long) {
                    super.onProgress(readBytes, totalBytes)
                    callback.onProgress(readBytes, totalBytes)
                }
            }
        }
    
    • 在View層如Activity中調(diào)用網(wǎng)絡(luò)接口
    MVCApi.httpGet(
        context = baseActivity,
        url = TKURL.URL_GET,
        params = null,
        tag = null, 
        callback = object : MVCHttpCallback<List<Banner>>() {
        override fun onStart() {
            LogUtils.d(TAG, "onButtonGet onStart() called")
        }
    
        override fun onSuccess(result: List<Banner>?) {
            Log.d(TAG, "onButtonGet onSuccess() called with: result = $result")
        }
    
        override fun onFailure(code: Int, msg: String?) {
            Log.d(TAG, "onButtonGet onFailure() called with: code = $code, msg = $msg")
        }
    
        override fun onComplete() {
            Log.d(TAG, "onButtonGet onComplete() called")
        }
    
    })
    

    具體使用可以參考demo代碼,demo中有詳細(xì)的示例演示MVC項(xiàng)目如何使用RxHttp

  • MVVM項(xiàng)目

    • 定義Activity基類BaseMvvmActivity
    abstract class BaseMvvmActivity<VM : BaseViewModel, VDB : ViewDataBinding> : AppCompatActivity(){
    
       lateinit var viewModel: VM
       lateinit var binding: VDB
    
       protected abstract fun getLayoutId(): Int
    
       final override fun onCreate(savedInstanceState: Bundle?) {
           onCreateStart(savedInstanceState)
           super.onCreate(savedInstanceState)
           binding = DataBindingUtil.setContentView(this, getLayoutId())
           binding.lifecycleOwner = this
           createViewModel()
           onCreateEnd(savedInstanceState)
       }
    
       protected open fun onCreateStart(savedInstanceState: Bundle?) {}
       protected open fun onCreateEnd(savedInstanceState: Bundle?) {}
    
       /**
        * 創(chuàng)建ViewModel
        */
       private fun createViewModel() {
           val type = findType(javaClass.genericSuperclass)
           val modelClass = if (type is ParameterizedType) {
               type.actualTypeArguments[0] as Class<VM>
           } else {
               BaseViewModel::class.java as Class<VM>
           }
           viewModel = ViewModelProvider(this).get(modelClass)
       }
    
       private fun findType(type: Type): Type?{
           return when(type){
               is ParameterizedType -> type
               is Class<*> ->{
               findType(type.genericSuperclass)
               }
               else ->{
               null
               }
           }
       }
    
    }
    
    • 定義ViewModel的基類BaseViewModel
    open class BaseViewModel(application: Application) : AndroidViewModel(application) {
    
       /**
        * 網(wǎng)絡(luò)倉庫
        */
       protected val networkbaseRepository: NetworkRepository = NetworkRepository.instance
       
       /**
        * 上下文
        */
       protected var context: Context = application.applicationContext
    
    }
    
    • 定義網(wǎng)絡(luò)倉庫,封裝網(wǎng)絡(luò)接口
    /**
    * MVVM架構(gòu)網(wǎng)絡(luò)倉庫
    * UI->ViewModel->Repository->LiveData(ViewModel)->UI
    */
    class NetworkRepository private constructor() {
    
       companion object {
           val instance = NetworkRepository.holder
       }
    
       private object NetworkRepository {
           val holder = NetworkRepository()
       }
    
       inline fun <reified T> wrapHttpCallback(): MvvmHttpCallback<T> {
           return object : MvvmHttpCallback<T>() {
    
           }
       }
    
       inline fun <reified T> newCallback(liveData: MutableLiveData<TKState<T>>): HttpCallback<TKResponse<T>> {
           val type = wrapHttpCallback<T>().getType()
           return object : HttpCallback<TKResponse<T>>(type) {
               override fun onStart() {
               liveData.value = TKState.start()
               }
    
               override fun onNext(response: TKResponse<T>?) {
               liveData.value = TKState.response(response)
               }
    
               override fun onError(e: Throwable?) {
               liveData.value = TKState.error(e)
               }
    
               override fun onComplete() {
    
               /**
                * 親,此處不要做任何操作,不要給LiveData賦值,防止onNext對應(yīng)的LiveData數(shù)據(jù)被覆蓋,
                * 在TKState類handle方法里會特別處理回調(diào)的,放心好了
                */
               }
    
               override fun onProgress(readBytes: Long, totalBytes: Long) {
               liveData.value = TKState.progress(readBytes, totalBytes)
               }
           }
       }
    
       inline fun <reified T> httpGet(
           context: Context,
           url: String,
           params: Map<String, Any?>?,
           tag: Any? = null
       ): MutableLiveData<TKState<T>> {
           val liveData = MutableLiveData<TKState<T>>()
           RxHttp.getInstance()
               .get(context)
               .url(url)
               .params(params)
               .tag(tag)
               .enqueue(newCallback(liveData))
           return liveData
       }
    
    
       inline fun <reified T> httpPost(
           context: Context,
           url: String,
           params: Map<String, Any?>?,
           tag: Any? = null
       ): MutableLiveData<TKState<T>> {
           val liveData = MutableLiveData<TKState<T>>()
           RxHttp.getInstance().post(context)
               .url(url)
               .params(params)
               .tag(tag)
               .enqueue(newCallback(liveData))
           return liveData
       }
    
       inline fun <reified T> httpPostForm(
           context: Context,
           url: String,
           params: Map<String, Any?>?,
           tag: Any? = null
       ): MutableLiveData<TKState<T>> {
           val liveData = MutableLiveData<TKState<T>>()
           RxHttp.getInstance().postForm(context)
               .url(url)
               .params(params)
               .tag(tag)
               .enqueue(newCallback(liveData))
           return liveData
       }
    
       inline fun <reified T> httpPut(
           context: Context,
           url: String,
           params: Map<String, Any?>?,
           tag: Any? = null
       ): MutableLiveData<TKState<T>> {
           val liveData = MutableLiveData<TKState<T>>()
           RxHttp.getInstance().put(context)
               .url(url)
               .params(params)
               .tag(tag)
               .enqueue(newCallback(liveData))
           return liveData
       }
    
       inline fun <reified T> httpDelete(
           context: Context,
           url: String,
           params: Map<String, Any?>?,
           tag: Any? = null
       ): MutableLiveData<TKState<T>> {
           val liveData = MutableLiveData<TKState<T>>()
           RxHttp.getInstance().delete(context)
               .params(params)
               .url(url)
               .tag(tag)
               .enqueue(newCallback(liveData))
           return liveData
       }
    
    
       /**
        *上傳
        *支持上傳多個文件,map中對應(yīng)的value類型為File類型或Uri類型
        *支持監(jiān)聽上傳進(jìn)度
           val map =Map<String,Any>()
           map.put("model", "xiaomi")
           map.put("os", "android")
           map.put("avatar",File("xxx"))
           map.put("video",uri)
        */
       inline fun <reified T> httpUpload(
           context: Context,
           url: String,
           params: Map<String, Any?>?,
           tag: Any? = null
       ): MutableLiveData<TKState<T>> {
           val liveData = MutableLiveData<TKState<T>>()
           RxHttp.getInstance().upload(context)
               .url(url)
               .params(params)
               .tag(tag)
               .enqueue(newCallback(liveData))
           return liveData
       }
    
    
       /**
        * 下載
        * context:上下文,如不需要和生命周期綁定,應(yīng)該傳遞applicationContext
        * url:下載地址
        * dir:本地目錄路徑
        * filename:保存文件名稱
        * callback:下載進(jìn)度回調(diào)
        * md5:下載文件的MD5值
        * breakpoint:是否支持?jǐn)帱c(diǎn)下載,默認(rèn)為true
        */
       fun httpDownload(context: Context, url: String, dir: String, filename: String, callback: DownloadCallback, md5: String? = null, breakPoint: Boolean = true, tag: Any? = null) {
           RxHttp.getInstance().download(context).dir(dir).filename(filename).breakpoint(breakPoint).md5(md5).url(url).tag(tag).enqueue(callback)
       }
    
    }
    
    • 定義TKState類,用于將網(wǎng)絡(luò)回調(diào)轉(zhuǎn)化為LiveData
    /**
     *將HttpCallback回調(diào)轉(zhuǎn)化為對應(yīng)的LiveData
    */
    class TKState<T> {
    
       var state: Int = 0
       var code = TKErrorCode.ERRCODE_UNKNOWN
       var msg: String? = null
       var data: T? = null
       var progress: Long = 0
       var total: Long = 0
    
       @JvmOverloads
       constructor(state: Int, data: T? = null, msg: String? = "") {
           this.state = state
           this.data = data
           this.msg = msg
       }
    
       constructor(state: Int, throwable: Throwable?) {
           this.state = state
           handleThrowable(throwable).run {
               this@TKState.code = first
               this@TKState.msg = second
           }
       }
    
       constructor(state: Int, progress: Long, total: Long) {
           this.state = state
           this.progress = progress
           this.total = total
       }
    
       fun handle(handleCallback: HandleCallback<T>.() -> Unit) {
           val callback = HandleCallback<T>()
           callback.apply(handleCallback)
           when (state) {
               START -> {
               callback.onStart?.invoke()
               }
               SUCCESS -> {
               callback.onSuccess?.invoke(data)
               }
               FAIL -> {
               callback.onFailure?.invoke(code, msg)
               }
               PROGRESS -> {
               callback.onProgress?.invoke(progress, total)
               }
           }
           if (state == SUCCESS || state == FAIL) {
               callback.onComplete?.invoke()
           }
       }
    
       open class HandleCallback<T> {
           var onStart: (() -> Unit)? = null
           var onSuccess: ((T?) -> Unit)? = null
           var onFailure: ((Int, String?) -> Unit)? = null
           var onComplete: (() -> Unit)? = null
           var onProgress: ((Long, Long) -> Unit)? = null
    
           fun onStart(callback: (() -> Unit)?) {
               this.onStart = callback
           }
    
           fun onSuccess(callback: ((T?) -> Unit)?) {
                this.onSuccess = callback
           }
    
           fun onFailure(callback: ((Int, String?) -> Unit)?) {
               this.onFailure = callback
           }
    
           fun onComplete(callback: (() -> Unit)?) {
               this.onComplete = callback
           }
    
           fun onProgress(callback: ((Long, Long) -> Unit)?) {
               this.onProgress = callback
           }
       }
    
       companion object {
           const val START = 0
           const val SUCCESS = 1
           const val FAIL = 2
           const val PROGRESS = 3
    
           fun <T> start(): TKState<T> {
            return TKState(START)
           }
    
           fun <T> response(response: TKResponse<T>?): TKState<T> {
               if (response != null) {
               if (response.isSuccess()) {
                   return TKState(SUCCESS, response.data, null)
               } else {
                   return error(ServerException(response.code, response.msg))
               }
    
               } else {
               return error(NullResponseException(TKErrorCode.ERRCODE_RESPONSE_NULL, TKErrorCode.ERRCODE_RESPONSE_NULL_DESC))
               }
    
           }
    
           fun <T> error(t: Throwable?): TKState<T> {
               return TKState(FAIL, t)
           }
    
           fun <T> progress(progress: Long, total: Long): TKState<T> {
               return TKState(PROGRESS, progress, total)
           }
       }
    
    }
    
    • 經(jīng)過一系列封裝,最后在View層如Activity中ViewModel調(diào)用Repository中的接口
     viewModel.testPost(hashMapOf(
               "name" to "jack",
               "location" to "shanghai",
               "age" to 28)
       )
       .observeState(this) {
           onStart {
             LogUtils.d(TAG, "onButtonPost() onStart called")
           }
           onSuccess {
             LogUtils.d(TAG, "onButtonPost() onSuccess called:${it}")
           }
           onFailure { code, msg ->
             ToastHelper.toast("onButtonPost() onFailure,code:${code},msg:${msg}")
           }
           onComplete {
             LogUtils.d(TAG, "onButtonPost() onComplete called")
           }
       }
    

    具體使用還要參考demo代碼,demo中有詳細(xì)的示例演示MVVM項(xiàng)目如何使用RxHttp

強(qiáng)烈建議下載Demo代碼,Demo中有詳細(xì)的示例,演示MVVM及MVC架構(gòu)如何使用RxHttp,如果本文對你有幫助,可以考慮給我點(diǎn)贊哦

Demo

https://github.com/kongpf8848/RxHttp

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容