android网络框架retrofit源码解析一

前面两篇文章大概介绍了Retrofit框架以及简单使用,下面我们来关注下源码。

其中在阅读源码的过程中,主要参考了该博客http://www.2cto.com/kf/201405/305248.html

Retrofit的使用就是以下几步:

        定义接口,参数声明,Url都通过Annotation指定

        通过RestAdapter生成一个接口的实现类(动态代理)

        调用接口请求数据

1.我们先关注下Annotation

先看@Get

/**Make a GET request to a REST path relative to base URL. */

@Documented

@Target(METHOD)

@Retention(RUNTIME)

@RestMethod("GET")

public @interface GET {

 String value();

}

1@GET本身也被几个Anotation注解

2@Target表示@GET注解是用于方法的

3value方法就返回这个注解的value值,在上例中就是/users/{user}/repos,然后就是

4@Retention定义Annotation形态。Runtime表示编译器将Annotation储存于class档中,可由VM读入 

5)再看@RestMethod

@Documented

@Target(ANNOTATION_TYPE)

@Retention(RUNTIME)

public @interface RestMethod {

 String value();

  boolean hasBody() default false;

}

RestMethod是一个用于AnnotationAnnotation,比如上面的例子中用来注解的@GETvalue方法就返回GEThasBody表示是否有Body,对于POST这个方法就返回true

 

再看Post

/**Make a POST request to a REST path relative to base URL. */

@Documented

@Target(METHOD)

@Retention(RUNTIME)

@RestMethod(value = "POST", hasBody = true)

public @interface POST {

 String value();

}

RetrofitAnnotation包含请求方法相关的@GET@POST@HEAD@PUT@DELETA@PATCH,和参数相关的@Path@Field@Multipart等。

下篇文章会讲解在哪里进行解析这些Annotation等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值