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等。
其中在阅读源码的过程中,主要参考了该博客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等。
相关文章推荐
- android网络框架retrofit源码解析二
- Android 网络框架之Retrofit2使用详解及从源码中解析原理
- [置顶] Android 网络框架之Retrofit2使用详解及从源码中解析原理
- android网络框架retrofit源码解析四
- Android 网络框架之Retrofit2使用详解及从源码中解析原理
- Android 网络框架之Retrofit2使用详解及从源码中解析原理
- Android 网络访问框架retrofit2,okhttp3之简单封装,kotlin源码
- Android网络框架volley学习(十一)volley源码解析总结
- Android网络框架:OKHttp源码简单解析(一)
- retrofit网络框架源码解析
- Android网络请求框架—OKHttp 源码解析
- retrofit网络框架源码解析
- andorid网络框架retrofit源码解析三
- retrofit网络框架源码解析
- Android 网络框架 Volley 源码解析
- Android 玩转IOC,Retfotit源码解析,教你徒手实现自定义的Retrofit框架
- Android 玩转IOC,Retfotit源码解析,教你徒手实现自定义的Retrofit框架
- Retrofit 2.0使用详解,配合OkHttp、Gson,Android最强网络请求框架
- android网络框架Retrofit 同步异步
- Android 网络通信框架Volley的解析