- 1.Retrofit provides two options to define HTTP request header fields: static and dynamic. Static headers can’t be changed for different requests. The header’s key and value are fixed and initiated with the app startup.
2.In contrast, dynamic headers must be set for each request.
分为动态和静态两种方式。方式一: 静态消息头
define the header and respective value for your API method as an annotation. The header gets automatically added by Retrofit for every request using this method. The annotation can be either key-value-pair as one string or as a list of strings.
类似如下这种:
@Headers({“Cache-Control: max-age=640000”, “User-Agent: My-App-Name”})多个
@Headers(“Cache-Control: max-age=640000”) 单个方式二: 通过代码 通过拦截器来设置
via the intercept method of Retrofits RequestInterceptor (customized implementation of the Interceptor interface in Retrofit 2).
http://blog.csdn.net/u011734444/article/details/50536411
http://www.jianshu.com/p/aaaa3a9bd46f
【这种方式的特点是:These values are passed with every request which is executed using the RestAdapter (Retrofit in Retrofit 2) and the integrated RequestInterceptor (Interceptor in Retrofit 2). 不用每个接口去一一修改,先用简单的】
3.动态方式:只有一种
Call<List<Task>> getTasks(@Header("Content-Range") String contentRange);
4.对已有的消息头进行覆盖Override Existing Headers in Retrofit 2 两个方法使用。
5.API解析
1.interface Call extends Cloneable
1.1. 调用类接口,支持泛型,也就是支持任何类型的对象。An invocation of a Retrofit method that sends a request to a webserver and returns a response retrofit网络请求框架的调用方法,向网络服务器发送请求并返回一个retrofit类型的响应
1.2. 轮询polling的机制:通过克隆call以相同的参数向同一个服务器发送HTTP请求。
Each call yields its own HTTP request and response pair. Use {@link #clone} to make multiple calls with the same parameters to the same webserver; this may be used to implement polling orto retry a failed call.
1.3. 需要触发方法来真正发起请求:同步请求Call.execute ; 异步请求Call.enqueue ,无论哪种方式随时可以取消请求Call.cancel,异常类型 : IOException 比如涉及到网络连接,避免不了流,还增加了两个健壮性的判断方法isExecuted()/isCanceled()2.final class Retrofit
2.1. 网络请求框架,其本身就是一个最终类。
Retrofit adapts a Java interface to HTTP calls by using annotations on the declared methods to define how requests are made. 在声明的网络请求mobilApi前通过注释来发动具体api请求,通过一系列的拼接封装来获取HTTP call对象,并发起连接。
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.example.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
-
- 2.2. Create instances using {@linkplain Builder the builder} and pass your interface to {@link #create} to generate an implementation.通过retrofit.builder方法来创建retrofit实例,并通过retrofit.create创建方法来生成一个实现public T create(final Class service) Service类本质上就是一个后端api接口的集合类,在这个类中定义各种后端接口
-
- 2.3. 对于这个自定义的服务类:Create an implementation of the API endpoints defined by the {@code service} interface.
The relative path for a given method is obtained from an annotation on the method describing the request type. 相对路径的访问端口在请求类型的注释中
- 2.3. 对于这个自定义的服务类:Create an implementation of the API endpoints defined by the {@code service} interface.
-
- 2.4. 支持的请求类型方法:
The built-in methods are:
01.retrofit2.http.GET GET
02.retrofit2.http.PUT PUT
03.retrofit2.http.POST POST
04.retrofit2.http.PATCH PATCH
05.retrofit2.http.HEAD HEAD
06.retrofit2.http.DELETE DELETE
07.retrofit2.http.OPTIONS OPTIONS
08.自定义方法:You can use a custom HTTP method with @HTTP
09.动态路径:For a dynamic URL, omit the path on the annotation and annotate the first parameter with {@link Url @Url}.
- 2.4. 支持的请求类型方法:
-
- 2.5. 注释关键字解析,同上,接口
http://blog.csdn.net/lmj623565791/article/details/51304204
- 2.5. 注释关键字解析,同上,接口
3.retrofit的默认OkHttpClient的超时时间:
http://www.devtf.cn/?p=1264- 1)连接超时15秒
- 2)读数据超时20秒
- 3)写数据不会超时
4.retrofit打印json数据
http://stackoverflow.com/questions/32965790/retrofit-2-0-how-to-print-the-full-json-response
https://github.com/square/retrofit/issues/1072#- 5.上传多张图片:
http://www.jianshu.com/p/9847fc8fd5e4
http://blog.csdn.net/u010046908/article/details/50608182 - 6.文件上传:
http://www.jianshu.com/p/77de2cbbfb72
http://blog.csdn.net/wbwjx/article/details/51379506 - 7.最后参考:
http://square.github.io/retrofit/
https://futurestud.io/blog/retrofit-send-objects-in-request-body
http://www.jianshu.com/p/3e13e5d34531