Retrofit系列一

  • 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();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值