博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Retrofit+Rxjava+OkHttp
阅读量:7210 次
发布时间:2019-06-29

本文共 1082 字,大约阅读时间需要 3 分钟。

  hot3.png

参考:

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 请求方式时,只需要更改方法定义的标签,用 标签,参数标签用 或者@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
getTop250(@Field("start") int start, @Field("count") int count);}//get接口:public interface MovieService { //获取豆瓣Top250 榜单 @GET("top250") Call
getTop250(@Query("start") int start,@Query("count")int count);}

转载于:https://my.oschina.net/huiyun/blog/792186

你可能感兴趣的文章
2012年4月19日
查看>>
获取站点所有缓存,以及清除站点缓存
查看>>
oracle 是user_tables里面可以查找到一个表,而用DESC或者insert语句插入时就会报不存在视图。...
查看>>
找水王续
查看>>
cocos2d-x之Node移除HelloWorld节点
查看>>
AtCoder WTF 2019 C2. Triangular Lamps Hard
查看>>
[转].NET Framework、C#、CLR和Visual Studo之间的版本关系
查看>>
sql语句-2-字符串数字日期时间
查看>>
[Python3网络爬虫开发实战] 3.1.2-处理异常
查看>>
25、没有编程基础可以学习PHP吗?
查看>>
mybatis案例源码详解
查看>>
Python--day37--进程锁
查看>>
导出PDF乱码
查看>>
UE4的AI学习(1)——基本概念
查看>>
自动开机 双网卡网络唤醒
查看>>
Unity中的特殊文件夹
查看>>
Windows下安装Hadoop
查看>>
ganglia 一站式部署
查看>>
svn 的使用
查看>>
react-router-dom
查看>>