目錄:
1-- 關(guān)于Retrofit
2--前提
3--建立一個Retrofit客戶端
4--Retrofit 轉(zhuǎn)換器和適配器
5--Retrofit 授權(quán)
6--在Java中用Retrofit 去查詢Gerrit
7--用Retrofit 轉(zhuǎn)換從RSS得到到XML的響應(yīng)
8--做一個可以在StackOverflow搜索的程序
9--在Android中用Retrofit去訪問Github API
1. 關(guān)于Retrofit:
1.1 什么是Retfofit
Retrofit 是一個用來服務(wù)于Java和Android的REST 客戶端雄家。 用它可以非常容易的去接收和上傳json數(shù)據(jù)(或是其他數(shù)據(jù))坚嗜,通過一個建立在REST上面的web服務(wù)器。在Retrofit中可以配置轉(zhuǎn)換器來進行數(shù)據(jù)序列化蜻势。特別是用GSon來轉(zhuǎn)換json數(shù)據(jù),同時一個你也定制你的轉(zhuǎn)換器丸逸,別入來進行解析XML數(shù)據(jù)或是其他相關(guān)協(xié)議的數(shù)據(jù)超陆。Retrofit的HTTP 請求部分用的是OkHttp 庫。
1.2 Retrofit的用法:
一般情況下蟀俊,你會跟Retrofit的三個類打交道:
- JSON 模塊
- 定義一個接口,在接口里面做跟HTTP相關(guān)的操作
- Retrofit.Builder Class 订雾,它的實例接口和其相關(guān)的API可以允許你去定義用URL只想的HTTP操作
接口中的每個方法代表一個API調(diào)用肢预。他們必須有一個HTTP注解(GET,POST等)去指定一個請求類型和相關(guān)的URL.返回的值包裝了一個response 結(jié)果(如果不清楚注解,請參考我的博文)
@GET("users")
Call<List<User>> getUsers();
你可以用"替換塊"和查詢參數(shù)去調(diào)整你的URL.替換塊必須由{}來包裹洼哎。在函數(shù)參數(shù)中的@Path的值會和你指定的替換塊綁定到一起烫映。
@GET("users/{name}/commits")//{name}即使替換塊
Call<List<Commit>> getCommitsByName(@Path("name") String name);
查詢參數(shù)在函數(shù)參數(shù)中用注解@Query來進行包裝沼本,他們會自動的被添加到URL的末端。
@GET("users")
Call<User> getUserById(@Query("id") Integer id);
函數(shù)參數(shù)中的@Body注解來告訴Retrofit窑邦,用這個對象來作為請求體(注意HTTP的三部分:請求頭擅威,請求行,請求體)
@POST("users")
Call<User> postUser(@Body User user)//這里冈钦,User的對象user就是請求體
2. 前提
以下例子需要你了解Gradle build 系統(tǒng)或是在Eclipse中使用Gradle,當(dāng)然你在Visual Studio中用Gradle那也沒有啥錯李请。
3. 建立一個Retrofit客戶端(小例子)
在這個例子中你會知道如何創(chuàng)建一個標準的REST 客戶端瞧筛。我們接收Resopose的服務(wù)器為mock server。
3.1 創(chuàng)建項目并設(shè)置
增加遠程依賴导盅,創(chuàng)建一個新的Project较幌,名字是com.vogella.retrofitgerrit.
并在src/main/java
下創(chuàng)建一個名字為com.vogella.retrofitgerrit
的包.
// retrofit
//Android studio setup--最新版本
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
// Junit
testImplementation("org.junit.jupiter:junit-jupiter-api:5.0.0")
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.0.0")
// to run JUnit 3/4 tests:
testImplementation("junit:junit:4.12")
testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0")
3.2 定義API和Retrofit適配器:
從Gerrit收到的JSON回復(fù),我們僅僅需要關(guān)注它的主題變化白翻,因此在默認的包中建立如下類:
package com.vogella.java.retrofitgerrit;
public class Change {
String subject;
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
在以下接口中定義Retrofit的REST API:
package com.vogella.java.retrofitgerrit;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface GerritAPI {
@GET("changes/")
Call<List<Change>> loadChanges(@Query("q") String status);
}
創(chuàng)建一個controller控制器類乍炉,這個類創(chuàng)造了Retrofit client, 調(diào)用Gerrit API以及處理結(jié)果(打印調(diào)用后返回的結(jié)果到console)
package com.vogella.java.retrofitgerrit;
import java.util.List;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class Controller implements Callback<List<Change>> {
static final String BASE_URL = "https://git.eclipse.org/r/";
public void start() {
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
GerritAPI gerritAPI = retrofit.create(GerritAPI.class);
Call<List<Change>> call = gerritAPI.loadChanges("status:open");
call.enqueue(this);
}
@Override
public void onResponse(Call<List<Change>> call, Response<List<Change>> response) {
if(response.isSuccessful()) {
List<Change> changesList = response.body();
changesList.forEach(change -> System.out.println(change.subject));
} else {
System.out.println(response.errorBody());
}
}
@Override
public void onFailure(Call<List<Change>> call, Throwable t) {
t.printStackTrace();
}
}
創(chuàng)建一個包含Java程序入口的類滤馍,從而啟動這個控制器類
package com.vogella.java.retrofitgerrit;
public class Main {
public static void main(String[] args) {
Controller controller = new Controller();
controller.start();
}
}
4.Retrofit 轉(zhuǎn)換器和適
4.1 Retrofit 轉(zhuǎn)換器
Retrofit 可以配置一個特殊的轉(zhuǎn)換器岛琼。用這個轉(zhuǎn)換器來處理數(shù)據(jù)序列化。目前幾個轉(zhuǎn)換器已經(jīng)可以使用于數(shù)據(jù)序列化巢株。
- 從JSON轉(zhuǎn)換
Gson:com.squareup.retrofit:converter-gson
Jackson:com.squareup.retrofit:converter-jackson
Moshi:com.squareup.retrofit:converter-moshi
除了上面列出的槐瑞,當(dāng)然你自己也可以通過Converter.Factory
類的子集來定制自己的轉(zhuǎn)換器,通過特殊的協(xié)議阁苞。
4.2 Retrofit 適配器
Retrofit本身也可以被繼承困檩,從而去和一些其他的庫(RxJava2.x,Java8那槽,Guava)一起協(xié)同工作(這點很重要悼沿,例如okHttp就不太適合去和RxJava去協(xié)同工作,這也是為什么既然選擇okHttp就不如直接用Retrofit的一個原因)
關(guān)于適配器的一個概述骚灸,請參考Adaptersquare/retrofit/retrofit-adapters/.
例如糟趾,RxJava 2.下 adapter 可以從Gradle獲得:
compile 'com.squareup.retrofit2:adapter-rxjava2:latest.version'
用Apache的遠程倉庫Maven:
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>adapter-rxjava2</artifactId>
<version>latest.version</version>
</dependency>
如果你要添加一個適配器(Adapter),那么要用到以下這個方法
retrofit2.Retrofit.Builder.addCallAdapterFactory(Factory)
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.example.com")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加適配器
.build()
再Retrofit添加這個適配器后就會返回一個RxJava2.x 的一個類型逢唤。Observable拉讯,F(xiàn)lowable和Single 等等。
@GET("users")
Observable<List<User>> getUsers();
Retrofit 授權(quán)
回頭完善內(nèi)容多...