Retrofit 2.0使用(2)如何使用@Body的形式发送Post
在使用Retrofit的时候如果只是有几个参数我们可以用@Querry的形式,然后需要使用','隔开
但是在需要@Querry的参数多了之后,如果再用上面的方式就会造成参数写了一大堆的麻烦事
所以Retrofit就提供了@Body 的形式来帮助我们处理这种事务
下面看代码吧
public interface LoginApiService {
@Headers({"Content-Type: application/json","Accept: application/json"})
@POST("server?")
Observable<GetLoginJson> Login(@Body RequestBody body);
}
这是他的API接口,需要在开头@Header 然后后面的值。。。。。( ▼-▼ )!讲道理我现在还没弄明白为啥这样写,有啥规律可循,后面搞懂了再来补上
补上:!
Content-Type 表示请求信息的格式,这个在Spring MVC里有应用
然后application/json 就代表json数据格式,当然还有很多其他的格式,这个有兴趣可以去查阅一下
/***20161125修改**/
无意间在简书上找到了@Header和@Headers的说明
@Header:header处理,不能被互相覆盖,用于修饰参数,
//动态设置Header值
@GET("user")
Call<User> getUser(@Header("Authorization") String authorization)
@Headers 用于修饰方法,用于设置多个Header值:
@Headers({
"Accept: application/vnd.github.v3.full+json",
"User-Agent: Retrofit-Sample-App"
})
@GET("users/{username}")
Call<User> getUser(@Path("username") String username);
。。。。。( ▼-▼ )!表示还是不懂Headers的写法啊
引用自简书作者@BoBoMEe
/***20161125编辑 end**/
然后你需要做的是封装一个Post请求的类
public class PostInfo {
private PostInfoBean postInfoBean;
public PostInfoBean getPostInfoBean() {
return postInfoBean;
}
public void setPostInfoBean(PostInfoBean postInfoBean) {
this.postInfoBean = postInfoBean;
}
public class PostInfoBean{
private String command;
private String posModel;
private String posSn;
private String posMac;
private String userId;
private String passWord;
/**get 省略*/
/**set 省略*/
}
然后使用Retrofit的时候在你实例了ApiService那个接口之后,还需要实例化一个请求Header
实例化完成之后由于我这边服务器接收的是Json类型的请求,我还需要用Gson将他转成Json字符串的形式,然后再(很重要)通过RequestBody(包含于Okhttp包中)类转化为RequestBody,之后再调用API接口即可
LoginApiService loginApiService = mRetrofit.create(LoginApiService.class);
PostInfo postInfo = new PostInfo();
PostInfo.PostInfoBean postInfoBean = postInfo.new PostInfoBean();
postInfoBean.setCommand("xxx");
postInfoBean.setPosModel("xx");
postInfoBean.setPosSn(getPhoneSn());
postInfoBean.setPosMac(getPhoneMac());
postInfoBean.setUserId("xx");
postInfoBean.setPassWord("xx");
postInfoBean.setVersion("xx");
postInfo.setPostInfoBean(postInfoBean);
Gson gson = new Gson();
String postInfoStr = gson.toJson(postInfoBean);
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),postInfoStr);
loginApiService.Login(body)
.xxx.xxx;
Retrofit 2.0使用(2)如何使用@Body的形式发送Post的更多相关文章
- Android Retrofit 2.0 使用-补充篇
推荐阅读,猛戳: 1.Android MVP 实例 2.Android Retrofit 2.0使用 3.RxJava 4.RxBus 5.Android MVP+Retrofit+RxJava实践小 ...
- Retrofit 2.0使用
最近在想能不能把之前项目里的网络请求改下 想通过Retrofit重构下,因为Retrofit完美兼容Rxjava后面会用到Rxjava 所以 开个坑写点 由于网上Retrofit 2.0的架构介绍很详 ...
- Retrofit 2.0 超能实践(四),完成大文件断点下载
作者:码小白 文/CSDN 博客 本文出自:http://blog.csdn.net/sk719887916/article/details/51988507 码小白 通过前几篇系统的介绍和综合运用, ...
- Retrofit 2.0 超能实践(三),轻松实现文件/多图片上传/Json字符串
文:http://blog.csdn.net/sk719887916/article/details/51755427 Tamic 简书&csdn同步 通过前两篇姿势的入门 Retrofit ...
- Retrofit 2.0 超能实践(一),okHttp完美支持Https传输
http: //blog.csdn.net/sk719887916/article/details/51597816 Tamic首发 前阵子看到圈子里Retrofit 2.0,RxJava(Andro ...
- Retrofit 2.0 超能实践,完美支持Https传输
http://blog.csdn.NET/sk719887916/article/details/51597816 前阵子看到圈子里Retrofit 2.0,RxJava(Android), OkHt ...
- android -------- Retrofit + RxJava2.0 + Kotlin + MVP 开发的 WanAndroid 项目
简介 wanandroid项目基于 Retrofit + RxJava2.0 + Kotlin + MVP 用到的依赖 implementation 'io.reactivex.rxjava2:rxj ...
- Retrofit 2.0基于OKHttp更高效更快的网络框架 以及自定义转换器
时间关系,本文就 Retrofit 2.0的简单使用 做讲解 至于原理以后有空再去分析 项目全面.简单.易懂 地址: 关于Retrofit 2.0的简单使用如下: https://gitee.c ...
- Android Retrofit 2.0使用
实例带你了解Retrofit 2.0的使用,分享目前开发Retrofit遇到的坑和心得. 添加依赖 app/build.gradle 1 compile 'com.squareup.retrofit2 ...
随机推荐
- LeetCode Burst Balloons
原题链接在这里:https://leetcode.com/problems/burst-balloons/ 题目: Given n balloons, indexed from 0 to n-1. E ...
- MVC中的模型
为MVC Music Store 建模 建模代码: public class Album { public virtual int AlbumId { get; set; } public virtu ...
- UE4中使用数据表(Data Table)
本文依据官方文档数据驱动游戏性元素整理而来. 做过游戏的应该都清楚,如果游戏稍微有点规模,那么使用数据驱动来做游戏一般是必不可少的一步,一般也就是策划通过本表的方式来解决.下面我们来简单说一下UE4中 ...
- C++学习笔记(1)——数据类型占空间大小
boolean bool 1 byte character char 1 byte May be signed or unsigned wchar_t 1 byte char16_t ...
- MyEclipse基础配置
1.设置默认工作空间编码 window/preferences/general/workspace/Text file encoding 2.设置文件默认打开方式 xml建议设置 html建议设置 j ...
- CSS:opacity 的取值范围是 0~1
CSS:opacity 的取值范围是 0~1,难怪设置为 1~100 看不到效果.
- xcode 中 的工程模板
基于视图的应用程序(view-based application) 应用程序如果仅使用一个视图,应该使用这个模板.一个简单的视图控制器会管理应用程序的主视图,而界面布置则使用一个Interface B ...
- ViewPager With FragmentPagerAdapter
采用PagerAdapter中的FragmentPagerAdapter来实现页面切换,适用于a handful of typically more static fragments to be pa ...
- lipo 合并target为Simulator和Device编译的静态库
进入项目对应的Build目录后,以下指令: $lipo -create Debug-iphoneos/libSalamaDeveloper.a Debug-iphonesimulator/libSal ...
- python(八)内置模块logging/os/time/sys/json/pickle
模块 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少 ...