关于retrofit的Post请求

  • 首先网上的讲解一大堆,这里只是自己当时的问题,或者是接口的问题,现在吧写出来,为了下次使用
  • 为嘛的接口是 http:xxxxx /xxx/xxx
  • 然后需要是post请求 方式是 {“dev”:”xxxxxooooo”}
  • 自己找了一大堆,最后解决ok,下面代码
BASE_URL = "https://xxxxxx.xxx/xxxx/";

//http服务接口
public interface ApiService {
    @POST("report")
    Call<ResultBean> getResult (@Body BodyPost jsonBean);
}

ResultBean  ----》代表请求结果后的  实体类

BodyPost   ----》代表你要进行请求体 body来进行post请求的  实体类
比如我上面要求是{"dev":"xxxxxooooo"}, 则body类是

public class BodyPost {
    private String dev ;
    public BodyPost(String dev) {
        this.dev = dev;
    }
}

//最后是请求代码

  Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(AppUrl.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        mApiService = retrofit.create(ApiService.class);

        String xxxx= JsonUtil.changeArrayDateToJson(getjsonbean); 
        //xxxx代表键值对的 值!!!!
        mApiService.getResult( new BodyPost(xxxx))
                .enqueue(new Callback<ResultBean>() {
            @Override
            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {

                if (!response.isSuccessful()){
                    try {
                        Log.d("no success"  , response.errorBody().string());

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else {
                    Logutils.d("成功结果"  , response.body().toString() + "");
                }
            }

            @Override
            public void onFailure(Call<ResultBean> call, Throwable t) {
                    t.printStackTrace();
            }
        });
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值