Retrofit 源码版本为:2.7.2
一、Retrofit 网络请求流程
1,创建 interface
public interface MyApi {
/**
* Get 请求
* @param userName String类型
* @return 返回值 ‘Call<ResponseBody>’
*/
@GET("XXXX")
Call<ResponseBody> getUserInfo(@Query("userName") String userName);
}
2.Retrofit 通常使用流程
//1,创建 retrofit(可以通过 builder 模式, 对retrofit 进行配置不同参数)
Retrofit retrofit=new Retrofit.Builder()
.baseUrl("")
//.... 省略各种 配置
.client(new OkHttpClient())
.addConverterFactory(GsonConverterFactory.create())
.build();
//2. 拿到定义接口 ‘MyApi’
MyApi myApi = retrofit.create(MyApi.class);
//3. 执行异步请求
myApi.getUserInfo("").enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//请求成功
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
//请求失败
}
});
二、Retrofit 创建过程
Retrofit 创建 通过 Builder 设计模式
在构建过程中,需要注意几个地方
//Retrofit . java
public static final class Builder {
private final Platform platform; //平台, 通常是 Android
private @Nullable okhttp3.Call.Factory callFactory; //okhttp 请求工厂
private @Nullable HttpUrl baseUrl; //域名
private final List<Converter.Factory> converterFactories = new ArrayList<>(); //数据转换工厂
private final List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>();// 适配器工厂,将Call 转换成例如:Observable
//.......... 省略.......
}
注意:
这里有个异常需要关注一下 throw new IllegalArgumentException("baseUrl must end in /: " + baseUrl); , 并不是所有的 baseUrl 都需要以 / 结尾。具体逻辑在 if (!"".equals(pathSegments.get(pathSegments.size() - 1)))
中,只有像 http://host/a/b/c" 这类域名后面还有 segments 才必须以 / 结尾,源码如下。
//Retrofit . java
....
public Builder baseUrl(HttpUrl baseUrl) {
Objects.requireNonNull(baseUrl, "baseUrl == null");
List<String> pathSegments = baseUrl.pathSegments();
if (!"".equals(pathSegments.get(pathSegments.size() - 1))) {
throw new IllegalArgumentException("baseUrl must end in /: " + baseUrl);
}
this.baseUrl = baseUrl;
return this;
}
...
如果用户没有传入okhttp3.Call.Factory
,则默认使用OkHttp
//Retrofit.java
/**
* The HTTP client used for requests.
* <p>
* This is a convenience method for calling {@link #callFactory}.
*/
public Builder client(OkHttpClient client) {
return callFactory(Objects.requireNonNull(client, "client == null"));
}
public Retrofit build() {
if (baseUrl == null) {
throw new IllegalStateException("Base URL required.");
}
okhttp3.Call.Factory callFactory = this.callFactory;
if (callFactory == null) {//创建默认的 okhttp
callFactory = new OkHttpClient();
}
//......
}
下一篇文章,将对 Retrofit # create()
重点分析