Retrofit的使用

Retrofit也是Square公司的一个开源库,主要是对Http网络请求框架的封装,其本质上还是由OkHttp(默认)完成具体的网络请求,Retrofit只是对网络请求接口进行了封装(把每一个API网络请求都变成一个Java接口)。
具体流程如下:


具体流程图
  • App应用程序通过 Retrofit 请求网络,实际上是使用 Retrofit 接口层封装请求参数、Header、Url 等信息,之后由 OkHttp 完成后续的请求操作
  • 在服务端返回数据之后,OkHttp 将原始的结果交给 Retrofit,Retrofit根据用户的需求对结果进行解析

使用Retrofit的步骤:
1.添加Retrofit库的依赖,添加网络请求权限
2.创建接收服务器返回数据的类(JavaBean)
3.创建用于描述网络请求的接口

  • Retrofit把每一个http请求抽象成了Java接口,通过注解来描述请求方式和请求参数,还可以通过注解来配置网络请求参数 。
  • 内部的实现原理就是通过动态代理将接口的注解翻译成一个个http请求,再有线程池来执行这一个个的网络请求。

4.创建Retrofit实例(Builder构造者模式)
5.创建网络请求接口的实例
6.发送网络请求(异步enqueue/同步execute)
7.处理服务器返回的数据

PS: 这里具体说一下第三点,创建网络请求的接口时用到了大量的注解处理,包括请求头、请求方式、请求参数等等。

注解详情:
1.网络请求方式的注解:

网络请求方式

这里注意一下@HTTP的使用:

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.GET;
import retrofit2.http.HTTP;
import retrofit2.http.Path;

public interface ServiceApi {

    /**
     * method:网络请求的方法(区分大小写)
     * path:网络请求地址路径
     * hasBody:是否有请求体
     */
    @HTTP(method = "GET", path = "xxx/{id}", hasBody = false)
    Call<ResponseBody> getCall(@Path("id") int id);
    // {id} 表示是一个变量
    // method 的值 retrofit 不会做处理,所以要自行保证准确

}
  • 作用:替换@GET、@POST、@PUT、@DELETE、@HEAD等注解的作用及更多功能拓展
  • 具体使用:通过属性method、path、hasBody进行设置

2.标记具体作用的注解:


标记具体作用的注解

a. @FormUrlEncoded
作用:表示发送form-encoded的数据

每个键值对需要用@Filed来注解键名,随后的对象需要提供值。

b. @Multipart
作用:表示发送form-encoded的数据(适用于 有文件 上传的场景)

每个键值对需要用@Part来注解键名,随后的对象需要提供值。

 /**
     * 表明是一个表单格式的请求(Content-Type:application/x-www-form-urlencoded)
     * Field("username") 表示将后面的 String name 中name的取值作为 username 的值
     */
    @POST("/form")
    @FormUrlEncoded
    Call<ResponseBody> testFormUrl(@Field("username") String name, @Field("password") int pwd);

    /**
     * Part 后面支持三种类型,RequestBody、okHttp3.MultipartBody.Part 、任意类型
     * 除 okHttp3.MultipartBody.Part 以外,其它类型都必须带上表单字段(okHttp3.MultipartBody.Part 中已经包含了表单字段的信息),
     */
    @POST("/form")
    @Multipart
    Call<ResponseBody> testFileUpLoad(@Part("name") RequestBody name, @Part("password") RequestBody pwd, @Part MultipartBody.Part file);

具体使用:

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

        ServiceApi service = retrofit.create(ServiceApi.class);
        // @FormUrlEncoded 
        Call<ResponseBody> call = service.testFormUrl("Jack", 123456);

        // @Multipart
        MediaType textType = MediaType.parse("text/plain"); // 文本类型
        RequestBody name = RequestBody.create(textType, "Jack");
        RequestBody pwd = RequestBody.create(textType, "123456");
        RequestBody file = RequestBody.create(MediaType.parse("application/octet-stream"), "这里是模拟文件的内容");

        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", "test.txt", file);
//        Call<ResponseBody> call = service.testFileUpLoad(name, pwd, filePart);

        //发送网络请求(异步)
        call.enqueue(new Callback<ResponseBody>() {
            //请求成功时回调
            @Override
            public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
                //请求处理,输出结果
                assert response.body() != null;
                response.body().toString();
            }

            //请求失败时候的回调
            @Override
            public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable throwable) {
                System.out.println("连接失败");
            }
        });

3.网络请求参数的注解:

网络请求参数的注解

详解:
1.@Header 和 @Headers
作用:添加不固定的请求头 和 添加请求头
具体使用如下:

// @Header 使用
    @GET("xxx")
    Call<Test> getTest(@Header("Authorization") String authorization);

    // @Headers 使用
    @Headers("Authorization: authorization")
    @GET("xxx")
    Call<Test> getTest();

// 以上的效果是一致的。
// 区别在于使用场景和使用方式
// 1. 使用场景:@Header用于添加不固定的请求头,@Headers用于添加固定的请求头
// 2. 使用方式:@Header作用于方法的参数;@Headers作用于方法

2.@Body:
作用:以 Post方式 传递 自定义数据类型 给 服务器(Server)
特别注意:如果提交的是一个Map,那么作用相当于 @Field
不过Map要经过 FormBody.Builder 类处理成为符合 OkHttp 格式的表单,如下:

FormBody.Builder builder = new FormBody.Builder();
builder.add("key","value");

3.@Field 和 @FieldMap:
作用:发送 Post请求时提交请求的表单字段
具体使用:与 @FormUrlEncoded 注解配合使用

/**
     * 表明是一个表单格式的请求(Content-Type:application/x-www-form-urlencoded)
     * Field("username") 表示将后面的 String name 中name的取值作为 username 的值
     */
    @POST("/form")
    @FormUrlEncoded
    Call<ResponseBody> testFormUrl(@Field("username") String name, @Field("password") int pwd);

    /**
     * Map的key作为表单的键
     */
    @POST("/form")
    @FormUrlEncoded
    Call<ResponseBody> testFormUrl(@FieldMap Map<String, Object> map);

调用:

        // @Field
        Call<ResponseBody> call = service.testFormUrl("Jack", 123456);

        // @FieldMap
        // 实现的效果与上面相同,但要传入Map
        Map<String, Object> map = new HashMap<>();
        map.put("username", "Jack");
        map.put("password", 123456);
        Call<ResponseBody> call = service.testFormUrl(map);

        //发送网络请求(异步)
        call.enqueue(new Callback<ResponseBody>() {
            //请求成功时回调
            @Override
            public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
                //请求处理,输出结果
                assert response.body() != null;
                response.body().toString();
            }

            //请求失败时候的回调
            @Override
            public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable throwable) {
                System.out.println("连接失败");
            }
        });

4.@Part & @PartMap:
作用:发送 Post请求 时提交请求的表单字段

  • 与@Field的区别:功能相同,但携带的参数类型更加丰富,包括数据流,所以适用于 有文件上传 的场景
  • 具体使用:与 @Multipart 注解配合使用:
/**
     * Part 后面支持三种类型,RequestBody、okHttp3.MultipartBody.Part 、任意类型
     * 除 okHttp3.MultipartBody.Part 以外,其它类型都必须带上表单字段(okHttp3.MultipartBody.Part 中已经包含了表单字段的信息),
     */
    @POST("/form")
    @Multipart
    Call<ResponseBody> testFileUpLoad(@Part("name") RequestBody name, @Part("age") RequestBody age, @Part MultipartBody.Part file);

    /**
     * PartMap 注解支持一个Map作为参数,支持 RequestBody 类型,
     * 如果有其它的类型,会被retrofit2.Converter转换,如后面会介绍的 使用Gson的 retrofit2.converter.gson.GsonRequestBodyConverter
     * 文件只能用 @Part MultipartBody.Part 
     */
    @POST("/form")
    @Multipart
    Call<ResponseBody> testFileUpLoad(@PartMap Map<String, RequestBody> args, @Part MultipartBody.Part file);

    @POST("/form")
    @Multipart
    Call<ResponseBody> testFileUpLoad(@PartMap Map<String, RequestBody> args);

调用:

MediaType textType = MediaType.parse("text/plain");
        RequestBody name = RequestBody.create(textType, "Jack");
        RequestBody pwd = RequestBody.create(textType, "123456");
        RequestBody file = RequestBody.create(MediaType.parse("application/octet-stream"), "这里是模拟文件的内容");

        // @Part
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", "test.txt", file);
        Call<ResponseBody> call = service.testFileUpLoad(name, pwd, filePart);

        // @PartMap
        // 实现和上面同样的效果
        Map<String, RequestBody> mapParam = new HashMap<>();
        mapParam.put("name", name);
        mapParam.put("password", pwd);
        //这里并不会被当成文件,因为没有文件名(包含在Content-Disposition请求头中),但上面的 filePart 有
        //mapParam.put("file", file);
        Call<ResponseBody> call = service.testFileUpLoad(mapParam, filePart); //单独处理文件

        //发送网络请求(异步)
        call.enqueue(new Callback<ResponseBody>() {
            //请求成功时回调
            @Override
            public void onResponse(@NonNull Call<ResponseBody> call, @NonNull Response<ResponseBody> response) {
                //请求处理,输出结果
                assert response.body() != null;
                response.body().toString();
            }

            //请求失败时候的回调
            @Override
            public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable throwable) {
                System.out.println("连接失败");
            }
        });

5.@Query和@QueryMap:

  • 作用:用于 @GET 方法的查询参数(Query = Url 中 ‘?’ 后面的 key-value)

如:url = http://www.platfrom.net/?type=android其中,Query = type

  • 具体使用:
@GET("xxx/")
    Call<String> tag(@Query("type") String type);

    @GET("xxx/")
    Call<String> tag(@QueryMap Map<String, Object> args);

调用:

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

        ServiceApi service = retrofit.create(ServiceApi.class);
        // @Query
        Call<String> call = service.tag("android");

        // @QueryMap
        // 实现的效果与上面相同,但要传入Map
        Map<String, Object> map = new HashMap<>();
        map.put("type", "android");
        Call<String> call = service.tag(map);

        //发送网络请求(异步)
        call.enqueue(new Callback<String>() {
            //请求成功时回调
            @Override
            public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
                //请求处理,输出结果
                assert response.body() != null;
                response.body().toString();
            }

            //请求失败时候的回调
            @Override
            public void onFailure(@NonNull Call<String> call, @NonNull Throwable throwable) {
                System.out.println("连接失败");
            }
        });

6.@Path:
作用:URL地址的缺省值
具体使用:

@GET("xxx/{yyy}/zzz")
    Call<ResponseBody>  getGitHub(@Path("yyy") String yyy);
    // 访问的API是:https://api.github.com/xxx/{yyy}/zzz
    // 在发起请求时, {yyy} 会被替换为方法的第一个参数 yyy(被@Path注解作用)

7.@Url:
作用:直接传入一个请求的 URL变量 用于URL设置
具体使用:

@GET
        Call<ResponseBody> testUrlAndQuery(@Url String url, @Query("showAll") boolean showAll);
       // 当有URL注解时,@GET传入的URL就可以省略
       // 当GET、POST...HTTP等方法中没有设置Url时,则必须使用 @Url

总结:

注解总结

以上注解知识摘自网上及自己的一些总结,这里做记录纯属为了以后查阅方便。

下面创建一个完整的例子来走一遍Retrofit的网络请求:

1.添加Retrofit库的依赖,添加网络请求权限:

"com.squareup.retrofit2:retrofit:2.5.0",
"com.squareup.retrofit2:converter-gson:2.5.0",
"com.squareup.retrofit2:adapter-rxjava:2.5.0",
"com.squareup.retrofit2:converter-scalars:2.0.1",
"com.squareup.okhttp3:okhttp:3.12.1",
"com.squareup.okhttp3:logging-interceptor:3.10.0",
<uses-permission android:name="android.permission.INTERNET"/>

2.创建接收服务器返回数据的类(JavaBean)

public class WorksListVo {
    public WorksItem data;

    public class WorksItem {
        public ArrayList<Works> content;
    }

    public static class Works {
        public String tid;
        public String hits;
        public String utime;
        public String f_catalog_id;
        public String uid;
        public String content;
        public String province;
        public String avatar;
        public String sname;
        public String genderid;
        public String gender;
        public String intro;
    }
}

3.创建用于描述网络请求的接口

public interface ApiService {

    @POST("/xxx/thread/yyy")
    @FormUrlEncoded
    Call<WorksListVo> getWorkMoreData(@Field("last_id") String last_id,     
    @Field("utime") String utime, @Field("rn") String rn);
}

4.创建Retrofit实例(Builder构造者模式)

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

5.创建网络请求接口的实例

ApiService service = retrofit.create(ApiService.class);
Call<WorksListVo> call = service.getWorkMoreData("9526","2019-01-11",android");

6.发送网络请求(异步enqueue/同步execute)
7.处理服务器返回的数据(通过response类的 body()对返回的数据进行处理)

//发送网络请求(异步)
        call.enqueue(new Callback<String>() {
            //请求成功时回调
            @Override
            public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
                //请求处理,输出结果
                assert response.body() != null;
                response.body().toString();
            }

            //请求失败时候的回调
            @Override
            public void onFailure(@NonNull Call<String> call, @NonNull Throwable throwable) {
                System.out.println("连接失败");
            }
        });

Retrofit源码解读传送门:https://www.jianshu.com/p/cef4cfc4f756

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容