【Android 進階】Retrofit2 目前最優雅的網絡請求框架

版權聲明:本文爲博主原創文章,轉載請註明出處。 https://blog.csdn.net/leaf_130/article/details/55805680

身爲 Geek 的我們,面對不斷更新換代的技術是不是有點感到迷茫呢?其實只要掌握正確的學習方法,新的技術也就不畏懼了。
福利來了
推薦一位大牛【人稱:面哥】嘔心瀝血的一篇經驗分享:
程序員之路-學習經驗總結分享
正是面哥的鞭策,我決定開始看英文的技術文檔,學習新技術也是直接看官網的技術文檔,本篇文章就是我在 Retrofit2 的官網看完英文文檔 Retrofit 以及參考多篇博文之後總結出來的。

歡迎關注我的微信公衆號
不只是原創技術文章,更多的是對生活的思考總結
這裏寫圖片描述

Retrofit

Retrofit
是一個 Square 開發的類型安全的 REST 安卓客戶端請求庫。這個庫爲網絡認證、API 請求以及用 OkHttp 發送網絡請求提供了強大的框架 。
Retrofit 把 REST API 返回的數據轉化爲 Java 對象,就像 ORM 框架那樣,把數據庫內的存儲的數據轉化爲相應的 Java
bean對象。 那麼我們知道 Retrofit 是一個類型安全的網絡框架,而且它是使用 REST API 的.

REST :

Resources Representational State Transfer
資源表現層狀態轉化 每一個 URI 代表一種資源
客戶端和服務器之間,傳遞這種資源的某種 表現層(“資源”具體呈現出來的形式,比如.txt,.png,.jpg)
客戶端通過四個 HTTP 動詞(GET 用來獲取資源,POST 用來新建或更新資源,PUT 用來更新資源,DELETE 用來刪除資源)對服務器端資源進行操作,實現”表現層狀態轉化”

Retrofit 的簡單使用

這裏使用官方的例子介紹,也是以github的api做測試

第零步:

添加依賴:

//okHttp
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
//retrofit
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
    compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'

準備 api 接口:

https://api.github.com/users/wu-leaf/repos

需要封裝的 javabean 類

其中我抽取出用來測試的屬性如下4個:

public class Repo {
    private int id;
    private String name;
    private String full_name;
    private String fork;

    //get\set方法

第一步:

改造你的 HTTP API 變成一個 Java 接口。

public interface GitHubService {
  @GET("users/{user}/repos")
  Call<List<Repo>> listRepos(@Path("user") String user);
}

第二步:

Retrofit 生成一個 GitHubService 接口的實現

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com/")
    .build();

GitHubService service = retrofit.create(GitHubService.class);

來自創建的 GitHubService 的每個調用都可以向遠程Web服務器發出同步或異步 HTTP 請求。

Call<List<Repo>> repos = service.listRepos("wu-leaf");

第三步:

異步調用

repos.enqueue(new Callback<List<Repo>>() {
            @Override
            public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) {
                Log.d("TAG",response.body().toString());
                response_tv.setText(response.body().toString());
            }

            @Override
            public void onFailure(Call<List<Repo>> call, Throwable t) {
                 if (call.isCanceled()){
                     Log.d("TAG",t.toString());
                 }else{
                     Log.e("TAG",t.toString());
                 }
            }
        });

同步調用:
當然不能在主線程調用

Call<Repo> clone = repos.clone();

    Response<Repo> response = clone.execute();
    Repobody = response.body();

這個例子是不是挺簡單的,現在現在正式瞭解下常用 api 吧

常用 API 介紹

接口方法及其參數的註釋指示如何處理請求。

請求方法

每個方法必須具有提供請求方法和相對 URL 的 HTTP 註釋。 有五個內置註釋:GET,POST,PUT,DELETE和HEAD。 資源的相對 URL 在註釋中指定。

@GET("users/list")

您還可以在 URL 中指定查詢參數。

@GET("users/list?sort=desc")

URL 相關操作

可以使用替換塊(佔位符)和方法上的參數動態更新請求 URL。 替換塊是由{and}包圍母數字字符串。 相應的參數必須使用相同的字符串用 @Path 註釋。

@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId);

也可以添加查詢參數

@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId, @Query("sort") String sort);

對於複雜的查詢參數組合,可以使用 Map

@GET("group/{id}/users")
Call<List<User>> groupList(@Path("id") int groupId, @QueryMap Map<String, String> options);

請求體

可以指定一個對象用作帶有 @Body 註釋的 HTTP 請求體。

@POST("users/new")
Call<User> createUser(@Body User user);

該對象也將使用 Retrofit 實例上指定的轉換器進行轉換。 如果沒有添加轉換器,則只能使用 RequestBody。

提交表單和多部分數據

方法也可以聲明爲發送提交表單和多部分數據。

當方法上存在 @FormUrlEncoded 時,將發送表單編碼的數據。 每個鍵值對都使用包含名稱的@Field和提供值的對象進行註釋。

@FormUrlEncoded
@POST("user/edit")
Call<User> updateUser(@Field("first_name") String first, @Field("last_name") String last);

多部分請求時使用 @Multipart 存在的方法。部分使用 @Part 註釋聲明。

@Multipart
@PUT("user/photo")
Call<User> updateUser(@Part("photo") RequestBody photo, @Part("description") RequestBody description);

多部分使用 Retrofit 的轉換器,或者它們可以實現 RequestBody 來處理自己的序列化。

請求頭操作

您可以使用 @Headers 註釋爲方法設置靜態頭。

@Headers("Cache-Control: max-age=640000")
@GET("widget/list")
Call<List<Widget>> widgetList();
@Headers({
    "Accept: application/vnd.github.v3.full+json",
    "User-Agent: Retrofit-Sample-App"
})
@GET("users/{username}")
Call<User> getUser(@Path("username") String username);

請注意,請求頭不會相互覆蓋。 具有相同名稱的所有請求頭將包含在請求中。

請求頭可以使用 @Header 註釋動態更新。 必須向 @Header 提供相應的參數。 如果值爲 null,則將省略請求頭。 否則,toString 將被調用的值,並使用結果。

@GET("user")
Call<User> getUser(@Header("Authorization") String authorization)

需要添加到每個請求的標頭可以使用 OkHttp 攔截器指定。

同步 vs 異步

Call 實例可以同步或異步執行。 每個實例只能使用一次,但調用 clone()將創建一個可以使用的新實例。

在 Android 上,回調將在主線程上執行。 在 JVM 上,回調將發生在執行 HTTP 請求的同一線程上。

Retrofit 配置

Retrofit 是將 API 接口轉換爲可調用對象的類。 默認情況下,Retrofit 將爲您的平臺提供正常默認值,但它允許自定義。

轉換器:

默認情況下,Retrofit 只能將 HTTP 主體反序列化爲 OkHttp 的 ResponseBody 類型,並且它只能接受 @Body 的 RequestBody 類型。

可以添加轉換器以支持其他類型。 六個同級模塊適應流行的序列化庫爲您方便使用。
- Gson: com.squareup.retrofit2:converter-gson
- Jackson: com.squareup.retrofit2:converter-jackson
- Moshi: com.squareup.retrofit2:converter-moshi
- Protobuf: com.squareup.retrofit2:converter-protobuf
- Wire: com.squareup.retrofit2:converter-wire
- Simple XML: com.squareup.retrofit2:converter-simplexml
- Scalars (primitives, boxed, and String):
- com.squareup.retrofit2:converter-scalars

下面是一個使用GsonConverterFactory類來生成GitHubService接口的實現的例子,它使用Gson進行反序列化。

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("https://api.github.com")
    .addConverterFactory(GsonConverterFactory.create())
    .build();

GitHubService service = retrofit.create(GitHubService.class);

自定義轉換器

如果您需要與使用 Retrofit 不支持開箱即用的內容格式(例如 YAML,txt,自定義格式)的API進行通信,或者希望使用其他庫來實現現有格式,則可以輕鬆創建 你自己的轉換器。 創建一個擴展 Converter.Factory 類的類,並在構建適配器時傳遞實例。

依賴方式

MAVEN

<dependency>
  <groupId>com.squareup.retrofit2</groupId>
  <artifactId>retrofit</artifactId>
  <version>2.2.0</version>
</dependency>

GRADLE

compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'

Retrofit 需要至少 Java 7 或 Android 2.3。

PROGUARD 混淆

如果在項目中使用 Proguard,請在配置中添加以下行:

# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions

推薦文章:

retrofit 介紹:
Retrofit 用法詳解
國外的一系列教程
Retrofit 2.0: The biggest update yet on the best HTTP Client Library for Android
Android 網絡框架 Retrofit2.0 介紹、使用和封裝
你真的會用 Retrofit2 嗎? Retrofit2 完全教程
Retrofit2 完全解析 探索與 okhttp 之間的關係

設置緩存:
Retrofit2.0+okHttp3 緩存機制以及遇到的問題
使用 Retrofit 和 OkHttp 實現網絡緩存。無網讀緩存,有網根據過期時間重新請求
瀏覽器 HTTP 緩存原理分析
瀏覽器緩存

Restful API 介紹

理解 RESTful 架構
深入淺出 REST

版權印爲您的作品印上版權68298168

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章