Android Retrofit详解(retrofit:2.3.0)

本文详细介绍了Retrofit 2.3.0的使用,包括创建HTTP请求接口、注解详解、GSON与Converter、RxJava与CallAdapter的运用,以及如何自定义Converter和CallAdapter。同时,还提到了Retrofit.Builder的配置和URL组合规则,帮助开发者深入理解Retrofit在网络请求中的应用。
摘要由CSDN通过智能技术生成

目录

1.Retrofit是什么?

2.Retrofit如何使用?

2.1创建HTTP请求的API接口

2.2请求执行

3.注解详情

3.1请求方法注解

3.2标记请求数据类型

3.3注解参数

4.GSON和Converter

5.RxJava和CallAdapter

6.自定义Converter

7.自定义CallAdapter

8.其他说明

8.1Retrofit.Builder

8.2Retrofit的Url组合规则


retrofit网络请求相关依赖包说明

implementation 'com.google.code.gson:gson:2.8.0'(gson生成和解析库)
implementation 'com.squareup.okhttp3:okhttp:3.9.1'(开源的网络请求库)
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'(支持okhttp跟踪到一个网络请求的所有状态,包括请求头、请求体、响应行、 响应体,方便调试)
implementation 'com.squareup.retrofit2:retrofit:2.3.0'(实现将HTTP请求转换为Java接口)
implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'(配合Rxjava 使用)
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'(转换器,请求结果转换成Model)
implementation 'io.reactivex:rxandroid:1.2.1'
implementation 'io.reactivex:rxjava:1.2.1'(一种帮助你做异步的框架. 类似于 AsyncTask. 但其灵活性和扩展性远远强于前者. 从能力上讲, 如果说 AsycnTask 是 DOS 操作系统, RxJava 是 Window 操作系统。)

网络请求的大概分为四部分,retrofit主要负责封装网络请求,okhttp负责完成网络请求,logging-interceptor负责网络请求和响应的输出日志方便调试,rxjava一种帮助你做异步的框架,gson负责json的生成和解析,将请求结果转为相应的Model;

Retrofit官网:https://square.github.io/retrofit/#restadapter-configuration

1.Retrofit是什么?

Retrofit将您的HTTP API转换为Java接口;Retrofit 是一个 RESTful 的 HTTP 网络请求框架的封装,网络请求的工作本质上是 OkHttp 完成,而 Retrofit 仅负责网络请求接口的封装;

2.Retrofit如何使用?

    public static final ApiService service = new Retrofit.Builder()
            .baseUrl("http://www.baidu.com")
            .build()
            .create(ApiService.class);

2.1创建HTTP请求的API接口

1)通过Retrofit.Builder内部类创建Retrofit实例,需要调用baseUrl(url)指定请求的跟路径和调用build()方法执行Retrofit实例创建,在build()方法内部会创建Retrofit需要的其他默认参数;

注意:下面三种方式设置URL都可以,但是推荐第三种方式设置,当把url传入给Retrofit对URL进行解析,HttpUrl会解析请求的协议(http和https,或者其他协议),域名,端口等信息,实际上HttpUrl会为域名后面加上“/”;

"http://www.baidu.com/?key=value"
"http://www.baidu.com"
"http://www.baidu.com/"  //推荐格式

最终请求的时候跟路径会处理为"http://www.baidu.com/"格式;

http://www.baidu.com/?c=api&a=getList&p=1&model=1&page_id=0&create_time=0&client=android&version=1.3.0&time=1584694243&device_id=000000000000000&show_sdv=1

2)调用Retrofit类create()创建接口ApiService实例

创建接口ApiService实例时借助Java提供Proxy动态代理类实现ApiService代理和实例的创建;

public interface ApiService {
    @GET("group/{id}/users")
    Call<ResponseBody> groupList(@Path("id") int groupId);
}

2.2请求执行

//请求回调在UI线程执行
apiService.groupList(23)
                .enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        try {
                            response.body().string();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

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

3.注解详情

3.1请求方法注解

既然涉及HTTP的请求接口,则需要涉及HTTP请求的方法;

* The relative path for a given method is obtained from an annotation on the method describing
* the request type. The built-in methods are {@link retrofit2.http.GET GET},
* {@link retrofit2.http.PUT PUT}, {@link retrofit2.http.POST POST}, {@link retrofit2.http.PATCH
* PATCH}, {@link retrofit2.http.HEAD HEAD}, {@link retrofit2.http.DELETE DELETE} and
* {@link retrofit2.http.OPTIONS OPTIONS}. You can use a custom HTTP method with
* {@link HTTP @HTTP}. For a dynamic URL, omit the path on the annotation and annotate the first
* parameter with {@link Url @Url}.
请求方法 备注说明
GET retrofit2.http.GET

 

HTTP的请求方法,请求路径是baseurl和GET等方法指定相对路径组成一个完整路径;通过@Path方法可以动态设置路径中指定参数{id};@Url可以动态替换相对路径,同时也可以@Path方法可以动态设置路径中指定参数{id};

POST retrofit2.http.POST
PATCH retrofit2.http.PATCH
HEAD retrofit2.http.HEAD
DELETE retrofit2.http.DELETE
OPTIONS retrofit2.http.OPTIONS
HTTP retrofit2.http.HTTP HTTP请求可以指定以上所有请求方法,同时也可以@Path方法可以动态设置路径中指定参数{id};

 

 

 

 

 

 

 

 

 

 

下面时通过GET方法和HTTP定义的请求接口;

@GET("group/{id}/users")
Observable<String> groupList(@Path("id") int groupId);

@HTTP(method = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值