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;

 

 

 

转载于:https://www.cnblogs.com/fengfenghuifei/p/6038309.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果您使用Retrofit发送POST请求@Body方式处理数据库参数,可以按照以下步骤进行操作: 1. 创建一个Java类,用于存储参数。例如,如果您要向数据库添加一条用户记录,可以创建一个名为User的Java类,其属性包括用户名,密码等等。 ```java public class User { private String username; private String password; public User(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public String getPassword() { return password; } } ``` 2. 在Retrofit接口中定义POST请求,使用@Body注解将User对象作为请求体发送到服务器。 ```java public interface ApiService { @POST("users") Call<Void> createUser(@Body User user); } ``` 3. 在应用中使用Retrofit对象创建接口实例,并调用createUser方法。 ```java // 创建Retrofit对象 Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://example.com/api/") .addConverterFactory(GsonConverterFactory.create()) .build(); // 创建接口实例 ApiService apiService = retrofit.create(ApiService.class); // 创建User对象 User user = new User("username", "password"); // 发送POST请求 Call<Void> call = apiService.createUser(user); call.enqueue(new Callback<Void>() { @Override public void onResponse(Call<Void> call, Response<Void> response) { // 请求成功 } @Override public void onFailure(Call<Void> call, Throwable t) { // 请求失败 } }); ``` 上述代码将向服务器发送一条POST请求,将User对象作为请求体发送到服务器。如果请求成功,服务器将会将User对象存储到数据库中。如果请求失败,将会调用onFailure方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值