Android框架式編程之Retrofit

1、Retrofit 簡介

Retrofit 官網地址: https://github.com/square/retrofitjava

Retrofit(即Retrofit,目前最新版本爲2.6.0版本),是目前很是流行的網絡請求框架,底層是基於okHttp實現的。準確來講Retrofit是對okHttp的進一步封裝,它功能強大,支持同步和異步,支持多種數據的解析方式(默認爲Gson),支持RxJava。android

Retrofit 最大的優點就是簡潔易用,它經過註解配置網絡請求的參數,採用大量的設計模式來簡化咱們的使用。並且它的拓展性也作的至關的好,Retrofit 的功能模塊高度封裝,高內聚低耦合,咱們能夠自定義本身想要的組件,好比說咱們能夠本身選擇解析工具而不用默認的Gson。除此以外,Retrofit 還有諸如性能好,處理速度快,代碼簡化等優點。git

2、Retrofit 註解

 使用 Retrofit 前咱們不得不提一下註解,正是這些註解極大的方便了咱們的代碼編寫及邏輯流程的控制。github

1. 請求方法註解

  • @GET:GET請求
  • @POST:POST請求
  • @DELETE:DELETE請求
  • @HEAD:HEAD請求
  • @OPTIONS:OPTIONS請求
  • @PATCH:PATCH請求

2. 請求參數

  • @Headers:添加請求頭
  • @Path:替換路徑
  • @Query:替代參數值,一般是結合get請求的
  • @FormUrlEncoded:用表單數據提交
  • @Field:替換參數值,是結合post請求的

2、Retrofit 使用

Retrofit代碼實現步驟以下:json

1)建立Retrofit 實例。設計模式

2)定義接口,使用註解的形式封裝請求地址和請求參數api

3)經過Retrofit實例,獲取一個接口服務對象服務器

4)經過接口服務對象調用接口中的方法,獲取call對象網絡

5)Call對象執行請求(異步請求、同步請求)app

0. 準備工做

因爲Retrofit是基於OkHttp,因此還須要添加OkHttp庫依賴,須要在build.grale添加以下依賴:

dependencies {
    // Okhttp庫
    compile 'com.squareup.okhttp3:okhttp:3.10.0'
    // Retrofit庫
    compile 'com.squareup.retrofit2:retrofit:2.6.0'
}

添加網絡權限

<uses-permission android:name="android.permission.INTERNET"/>

建立接收服務器返回數據的類:

public class Blog {
    // 根據返回數據的格式和數據解析方式(Json、XML等)定義
    ...
}

1. 建立Retrofit實例

建立Retrofit實例時須要經過 Retrofit.Builder,並調用baseUrl方法設置URL。

Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://localhost:4567/")
        .build();

注意:Retrofit 的 baseUlr 必須以 /(斜線) 結束,否則會拋出一個IllegalArgumentException。

2. 定義接口

定義請求接口的方式示例以下:

public interface BlogService {
    @GET("blog/{id}")
    Call<ResponseBody> getBlog(@Path("id") int id);
}

注意這裏的 interface不是class,因此咱們是沒法直接調用該方法,咱們須要用Retrofit建立一個BlogService的代理對象。

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

拿到代理對象以後,就能夠調用了。

3. 接口調用

調用方式的示例以下:

Call<ResponseBody> call = service.getBlog(2);
// 經過call對象執行網絡請求(同步請求execute,異步請求enqueue)
call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        try {
            System.out.println(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        t.printStackTrace();
    }
});

至此,使用Retrofit 就可以進行基本的網絡請求了,若是須要更深層的使用,能夠參考一下資料進行使用,後續咱們在對這些高級的使用方法進行一一整理。

3、Retrofit 添加header參數

添加Header有如下幾種方式:

1. 使用註解的方式

添加一個Header參數

public interface UserService { 
    @Headers("Cache-Control: max-age=640000")
    @GET("/tasks")
    Call<List<Task>> getTasks();
}

添加多個Header參數

public interface UserService { 
  @Headers({
    "Accept: application/vnd.yourapi.v1.full+json",
    "User-Agent: Your-App-Name"
  })
  @GET("/tasks/{task_id}")
  Call<Task> getTask(@Path("task_id") long taskId);
}

2. 使用代碼的方式,則須要使用攔截器:

OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
httpClient.addInterceptor(new Interceptor() { 
  @Override
  public Response intercept(Interceptor.Chain chain) throws IOException {
  Request original = chain.request();
  Request request = original.newBuilder().header("User-Agent", "Your-App-Name")
       .header("Accept", "application/vnd.yourapi.v1.full+json")
       .method(original.method(), original.body()).build();
  return chain.proceed(request);
  }
}

OkHttpClient client = httpClient.build(); 
Retrofit retrofit = new Retrofit.Builder().baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create()).client(client).build();

3. 使用註解的方式,可是Header參數每次提交的都不一樣,也就是動態的Header:

public interface UserService { 
@GET("/tasks")
Call<List<Task>> getTasks(@Header("Content-Range") String contentRange);
}

4. 使用註解Map方式,也就是將多個Header參數進行封裝:

@GET("/search")
Call<ResponseBody> list(@HeaderMap Map<String, String> headers);

4、Retrofit 提交數據方式

1. Retrofit實現Form提交

 Form表單提交,後臺服務器是以 鍵值對 的形式來接受參數的,因此Retrofit也很聰明,把接口參數經過Map的形式來提交。使用@FieldMap,@POST註解。

    /**
     * 登陸
     * post
     * 表單提交
     *
     * @param map
     * @return
     */
    @FormUrlEncoded
    @POST("login")
    Flowable<BaseDto<LoginDto>> login(@FieldMap Map<String, String> map);

2. Retrofit實現Json提交

Json提交就是能夠把對象直接轉換成Json字符串,提交到後臺服務器。因此咱們直接傳遞整個對象便可。Retrofit會幫咱們把對象轉換成Json,而後傳遞給後臺服務器。使用@Body ,@POST註解。

    /**
     * 簽到詳情
     *
     * @param vo
     * @return
     */
    @POST("sign/getDate")
    Flowable<BaseDto<SignDetailDto>> signGetDate(@Body SignDetailVo vo);

 

參考資料:

1. 博文:https://www.jianshu.com/p/308f3c54abdd  

2. 實例代碼:https://github.com/ikidou/Retrofit2Demo/tree/master/client/src/main/java/com/github/ikidou

相關文章
相關標籤/搜索