Retrofit
Retrofit 是一个用于 Android 和 Java 平台的类型安全的网络请求框架。Retrofit 通过将 API 抽象成 Java 接口而让我们连接到 REST web 服务变得很轻松
如何集成
compile 'com.squareup.retrofit2:retrofit:2.5.0'
//这个玩意就是用来把json变成java中的实体类
compile 'com.squareup.retrofit2:converter-gson:2.6.0'
//retrofit底层通过okhttp操作的
compile 'com.squareup.okhttp3:okhttp:3.14.1'
//日志拦截器
compile 'com.squareup.okhttp3:logging-interceptor:3.14.1'
compile 'com.google.code.gson:gson:2.8.5'
init
public class RetrofitFactory {
private final OkHttpClient okHttpClient;
public RetrofitFactory(OkHttpClient okHttpClient) {
this.okHttpClient = okHttpClient;
}
public <T> T newInstance(String host, Class<T> cls) {
return newBuilder(okHttpClient, toBaseUrl(host))
.build()
.create(cls);
}
public Retrofit.Builder newBuilder(OkHttpClient okHttpClient,
String baseUrl) {
return new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(ToStringConverterFactory.create())
.addConverterFactory(FastJsonConverterFactory.create());
}
private static String toBaseUrl(String host) {
if (Strings.isNullOrEmpty(host)) {
throw new IllegalArgumentException("host 不可以为空");
}
if (!host.endsWith("/")) {
host = host + "/";
}
return host;
}
}
注解
Retrofit的注解非常简单,并容易理解,知道注解怎么用,这个框架就可以熟练掌握使用。
注解大致分为三各部分,Http请求方式,参数,上下文类型
####
Http请求方式
| 注解 | 作用 | 备注 |
|---|---|---|
| @GET | 对应http的get | 如果不配 base_url 可根据 @Url 传入添加 host |
| @POST | - | |
| @PUT | - | |
| @DELETE | - | |
| @PATCH | - | |
| @HEAD | - | |
| @OPTIONS | - | |
| @HTTP | 可代替前面所有的请求方式 (method,path,hasBody) | method 表示请求的方法,区分大小写 path表示路径 hasBody表示是否有请求体 |
请求参数
| 注解 | 作用 | 备注 |
|---|---|---|
| @Headers | 可添加多个请求头 | |
| @Header | 添加一个key不固定的请求头 | |
| @Field | 表单提交 | |
| @FieldMap | 表单提交 | 只能接收Map<String,String> |
| @Body | json | |
| @Part | 文件上传 | |
| @PartMap | 文件上传 | |
| @Path | url path占位符替换 | |
| @Query | url上的参数 | |
| @QueryMap | url上的参数 | |
| @Url | host |
上下文类型
| 注解 | 作用 | 备注 |
|---|---|---|
| @FormUrlEncoded | 表单请求 urlencoding | |
| @Multipart | 表单文件上传 | |
| @Streaming | 请求以流的方式回写 | 类似 out.print(resp) |
实践
下面列出一些平时用的比较多的方式,可以当做一个模版吧 C+V
- get_1
/**
* 获取菜单
*
* @param storeId
* @return
*/
@GET("Goods/GetDishes")
Call<JSONObject> getDishes(@Header("Authorization") String auth,
@Query("storeid") Integer storeId);
对应的url

- get_2
/**
* 获取台位信息
*
* @param map
* @return
*/
@GET("Store/GetDesk")
Call<JSONObject> getDesk(@Header("Authorization") String auth,
@QueryMap Map<String, String> map);
对应的url

- post_1
/**
* 获取token
*
* @param map
* @return
*/
@Headers({
"Content-Type:application/x-www-form-urlencoded"
})
@FormUrlEncoded
@POST("token")
Call<JSONObject> token(@FieldMap Map<String, String> map);
对应的url

- post_2
/**
* 获取token
*
* @param map
* @return
*/
@Headers({
"Content-Type:application/json"
})
@POST("token")
Call<JSONObject> tokenJson(@Body Map<String, String> map);
对应的url

- upload single file
/**
* 上传单文件
*
* @param description
* @param file
* @return
*/
@Multipart
@POST("upload")
Call<Response<String>> upload(@Part("description") String description,
@Part MultipartBody.Part file);
对应url


- download file
/**
* 下载文件
*
* @param fileUrl
* @return
*/
@Streaming
@GET
Call<ResponseBody> download(@Url String fileUrl);



