美文网首页安卓开发--通信
Retrofit2 + OkHttp3 + Rxjava ,po

Retrofit2 + OkHttp3 + Rxjava ,po

作者: qianshilingyao | 来源:发表于2017-06-21 14:59 被阅读39次

最近使用安卓网络请求,自己属于网络请求这块的大白菜,因此本文也是写给自己作为笔记的,大牛可以跳过忽略。

因为此前对http 协议了解不深入,此前很多博文写到很多用用法,但是post的值是什么却没有写出来,自己这时候也遇到一些麻烦,

所以这里,从 htpp 通信的数据格式上,对 Retrofit 注解关键字进行分析。

一、首先的搭建好网络抓包工具,我使用的Fiddler,这样子,手机怎样请求就可以看得一清二楚了,其使用可参考以下文章连接

http://www.jianshu.com/p/ab131b732ba0

http://www.jianshu.com/p/99b6b4cd273c

http://www.jianshu.com/p/a9db6823f0f7

二、看资料

英文官方链接:http://square.github.io/retrofit/ 

中文个人翻译链接:http://blog.csdn.net/leilba/article/details/50685205

参考文章:

http://www.jianshu.com/p/c1a3a881a144

http://www.jianshu.com/p/442a29da7b23

http://www.jianshu.com/p/308f3c54abdd

http://frodoking.github.io/2015/05/16/android-retrofit/

http://gank.io/post/560e15be2dca930e00da1083

三、添加依赖以下4个依赖就可以了,截止20170621 Retrofit 最新的库分支是2.3.0,

converter-gson:2.3.0 和 converter-jackson:2.3.0 这个 json 解析的选其中的一个都行;

//网络框架依赖包

compile'com.squareup.retrofit2:retrofit:2.3.0'

compile'com.squareup.retrofit2:converter-gson:2.3.0'

//    compile 'com.squareup.retrofit2:converter-jackson:2.3.0'

compile'com.squareup.retrofit2:adapter-rxjava:2.3.0'

compile'io.reactivex:rxandroid:1.2.1'

四、实验操作:

1、操作结果:

@POST("user target Url")

@FormUrlEncoded

Call<RegResult> postFeed( @Field("post") String  postData);

如果,postData=”123456789“,那么post报文 是:  post=123456789

同样,若是多个值,Call postFeed ( @Field("post1") String  postData1,@Field("post2") String  postData2));

那么post报文 是:  post1=123456789&post2=123456789

2、操作结果:

@POST("user target Url")

Observable<RegResult >    postFeed( @Body RequestBody post);

其中body,通过解析字符串得到:RequestBody body = RequestBody.create( MediaType.parse("application/octet-stream" ), NetWorks.postData.getBytes());

如果,NetWorks.postData = ”123456789“,

那么post报文 是:  123456789,这里要注意;


如果是通过解析 json 对象得到: RequestBody body = RequestBody.create(MediaType.parse("application/json"), result.toString());

如果,JSONObject result =new JSONObject();

result=put("key1",”123456789“),那么post报文 是:  key1=123456789;

3、操作结果:

@POST("user target Url")

Observable    postFeed( @Body String  post);

如果,NetWorks.postData= ”123456789“,那么post报文 是:  "123456789",注意这里会多一对双引号!!!

相关文章

网友评论

    本文标题:Retrofit2 + OkHttp3 + Rxjava ,po

    本文链接:https://www.haomeiwen.com/subject/gxwzqxtx.html