Retrofit+Rxjava+OkHttp
2016-11-22 00:00
246 查看
摘要: 各自的职责:Retrofit 负责请求的数据和请求的结果,使用接口的方式呈现,OkHttp 负责请求的过程,RxJava 负责异步,各种线程之间的切换。
参考:
http://duanyytop.github.io/2016/08/06/Retrofit%E7%94%A8%E6%B3%95%E8%AF%A6%E8%A7%A3/
http://www.jianshu.com/p/811ba49d0748
1,首先,要使用Retrofit ,你肯定需要把它的包引入,在你的build.gradle文件中添加如下配置:
说明:使用POST 请求方式时,只需要更改方法定义的标签,用@POST 标签,参数标签用 @Field 或者@Body或者FieldMap,注意:使用POST 方式时注意2点,1,必须加上 @FormUrlEncoded标签,否则会抛异常。2,使用POST方式时,必须要有参数,否则会抛异常:
接口实例 post/get:
参考:
http://duanyytop.github.io/2016/08/06/Retrofit%E7%94%A8%E6%B3%95%E8%AF%A6%E8%A7%A3/
http://www.jianshu.com/p/811ba49d0748
1,首先,要使用Retrofit ,你肯定需要把它的包引入,在你的build.gradle文件中添加如下配置:
compile 'com.squareup.retrofit2:retrofit:2.1.0'//retrofit compile 'com.google.code.gson:gson:2.6.2'//Gson 库 //下面两个是RxJava 和RxAndroid compile 'io.reactivex:rxjava:1.1.0' compile 'io.reactivex:rxandroid:1.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0'//转换器,请求结果转换成Model compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'//配合Rxjava 使用
说明:使用POST 请求方式时,只需要更改方法定义的标签,用@POST 标签,参数标签用 @Field 或者@Body或者FieldMap,注意:使用POST 方式时注意2点,1,必须加上 @FormUrlEncoded标签,否则会抛异常。2,使用POST方式时,必须要有参数,否则会抛异常:
if (isFormEncoded && !gotField) { throw methodError("Form-encoded method must contain at least one @Field."); }
接口实例 post/get:
//post接口: public interface MovieService { //获取豆瓣Top250 榜单 @FormUrlEncoded @POST("top250") Call<MovieSubject> getTop250(@Field("start") int start, @Field("count") int count); } //get接口: public interface MovieService { //获取豆瓣Top250 榜单 @GET("top250") Call<MovieSubject> getTop250(@Query("start") int start,@Query("count")int count); }
相关文章推荐
- Rxjava+ReTrofit+okHttp深入浅出-终极封装特殊篇(替换Gson返回)
- MVP+Retrofit+Okhttp+RxJava
- RxJava2+Retrofit2+okHttp的二次封装
- RxJava+Retrofit+OkHttp深入浅出-终极封装二(网络请求)
- rxjava+retrofit+okhttp 打造通用接口
- RxJava+Retrofit+OkHttp 深入浅出-终极封装一
- Android Retrofit2+OkHttp3+RxJava 三联合
- rxjava+Retrofit+okhttp 实战网络请求
- retrofit+RxJava+okhttp简便封装实现网络请求(详解)
- Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava
- 浅谈RxJava+Retrofit+OkHttp 封装使用
- Retrofit + RxJava + OkHttp 让网络请求变的简单
- Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava
- MVP+Retrofit+RxJava+Okhttp构造一个合格的Android框架
- Android中的Retrofit+OkHttp+RxJava缓存架构使用
- RxJava+Retrofit+OkHttp深入浅出-终极封装二(网络请求)
- RxJava+Retrofit+OkHttp实战
- Retrofit2.0通俗易懂的学习姿势,Retrofit2.0 + OkHttp3 + Gson + RxJava
- retrofit okhttp RxJava bk Gson Lambda 综合示例【配置】