您的位置:首页 > 理论基础 > 计算机网络

android网络框架retrofit源码解析一

2014-09-03 15:02 543 查看
前面两篇文章大概介绍了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注解是用于方法的
3)value方法就返回这个注解的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是一个用于Annotation的Annotation,比如上面的例子中用来注解的@GET,value方法就返回GET,hasBody表示是否有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();
}

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

下篇文章会讲解在哪里进行解析这些Annotation等。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: