乙個簡單的Retrofit 2 0專案實現

2021-08-21 17:18:59 字數 1502 閱讀 5958

目前,主要的安卓網路框架有okhttp,retrofit,android-async-http,volley。其中,retrofit底層基於okhttp來實現,內部對okhtttp客戶端做了封裝。

首先,retrofit使用註解方式,簡化了我們的url拼寫形式,而且註解含義一目了然,簡單易懂;

其次,retrofit使用簡單,結構層次分明,每一步都能清晰的表達出之所以要使用的寓意;

再者,retrofit支援同步和非同步執行,使得請求變得簡單,只要呼叫enqueue/execute即可完成;

最後,retrofit更大自由度的支援我們自定義的業務邏輯,如自定義converters。

1.新增依賴庫

implementation'com.squareup.retrofit2:retrofit:2.4.0'

2. 請求網路

netapi.getmygoods(localdatamanager.getcurrentuser()!!.usermodel!!.poccustid, "1", "1", "20")

.subscribe()

3. 處理返回資料 

private fun handledata(data: mygoodslistmodel)
4. 寫getmygoods的api引數

fun getmygoods(poccustid: string, orderstatus: string, pagenum: string, pagesize: string): flowable>
5. 寫rxhelper

object rxhelper 

.dofinally }}

}

6.寫cdamservice層

}7.url最初位址

public final class buildconfig
8.寫url注入層

@post("order/order/querymyproductlist")

fun getmygoodslist(@body loginrequest: map): flowable>

9.寫引數model層

data class mygoodslistmodel(val currentpage: string,

val totalpage: string,

val records: list?

) : serializable

上述步驟完成使用retrofit進行網路請求,並對結果進行處理的操作。

Retrofit2 0的離線快取

大家在使用retrofit2.0時候就應該已近知道了。retrofit2.0是建立在okhttps的基礎上進行的,因此對於快取的設定我們也是對okhttps上進行設定 在訪問httpmethods時建立單例 private static class singletonholder 獲取單例 我們來看...

OpenCV20(flann乙個簡單的測試程式)

flann測試.cpp 定義控制台應用程式的入口點。於opencv官網,略有修改 file surf flannmatcher brief surf detector descriptor flann matcher author a.huaman include stdafx.h include ...

Retrofit2的簡單用法(一)

進來研究了一下比較火的請求框架 retrofit2 在retrofit的基礎上去其糟粕取其精華,具體的區別我在這就不一一舉例了,這篇文章只要是介紹一下retrofit2的簡單使用方法,和自己遇見的坑,不介紹深層次的東西。首先我用的是androidstduio 直接配置路徑就可以了,eclipse的同...