• 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;

     

  • 相关阅读:
    前端向后端发送数据时,有时需要转数据格式,但是有时会得到意外的false数据
    js字符串处理,把img标签包裹在p标签里
    antd 对话框、上传图片、轮播图结合在一起
    JavaScript 交换数组元素位置
    codeforces 803 F. Coprime Subsequences (莫比乌斯反演)
    UVa 12716 GCD XOR (数论+bitmask)
    codeforces 1556D
    codeforces 1556 E. Equilibrium (线段树)
    codeforces 1556F
    UVa 1502 GRE Words (DP+AC自动机+jry线段树)
  • 原文地址:https://www.cnblogs.com/fengfenghuifei/p/6038309.html
Copyright © 2020-2023  润新知