Android Retrofit使用实例--post请求

上一篇介绍了Android Retrofit的get请求【Android Retrofit使用实例--get请求

下面介绍post请求

1、GetRequestInterface里添加post接口,如下:

/**
     * 获取出库单详情
     * */
    @FormUrlEncoded
    @POST("api/Order/Detail")
    Call<OutWarehouseDetail> getDetail(@Field("id") String id);

    //    多个参数实例
//    @FormUrlEncoded
//    @POST("api/Order/Detail")
//    Call<Object> withParams(
//            @Field("id") String id
//            , @Field("code")String code
//    );

2、发送请求,如下:

private void loadDetail(String sCode){
        if(!TextUtils.isEmpty(sCode)){
            //创建Retrofit对象
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://127.0.0.1:8080/")
                    .addConverterFactory(GsonConverterFactory.create()) //Gson数据转换器
                    .build();

            //创建网络请求接口实例
            GetRequestInterface request = retrofit.create(GetRequestInterface.class);
            Call<OutWarehouseDetail> call = request.getDetail(id);//id为参数

            //发送网络请求(异步)
            call.enqueue(new Callback<OutWarehouseDetail>() {
                @Override
                public void onResponse(Call<OutWarehouseDetail> call, Response<OutWarehouseDetail> response) {
                    //Log.i(TAG, "loadDetail->onResponse(MainActivity.java): "+response.body());

                    OutWarehouseDetail detail = response.body();
                    //Log.i(TAG, detail.sCode);
                }

                @Override
                public void onFailure(Call<OutWarehouseDetail> call, Throwable t) {
                    Log.i(TAG, "loadDetail->onFailure(MainActivity.java): "+t.toString() );
                }
            });
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值