Retrofit初探——POST方式提交JSON数据

0x00 HTTP Method:POST

POST请求再日常的使用中很常见,比如登录,上传数据中使用。之前介绍了GET方式,今天简单介绍下如何使用POST来提交数据。

0x01 常用的POST方式

POST方式提交数据再浏览器中的表现主要是使用Form,在客户端中国中的主要表现是提交JSON数据。当然,具体是什么数据格式并不重要,我们可以通过抓包来分析:最终数据都是一样的。

使用Model对象

首先新建一个model对象,比如:User,添加常用的熟悉和get/set方法。新建我们的Service:

@POST("/send")
public Call<ResponseBody> modelPost(@Url String url, @Body User user);

测试代码:

@Test
public void modelPost() throws Exception {
   HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
   logging.setLevel(HttpLoggingInterceptor.Level.BASIC);

   OkHttpClient client = RetrofitManager.getClient(logging);

   Retrofit retrofit = RetrofitManager.getRetrofit(client);
   ExampleService service = retrofit.create(ExampleService.class);

   String url = "http://www.remoteurl.com";
   User user = new User();
   user.setName("ttdevs");
   Call<ResponseBody> example = service.modelPost(url, user);

   final CountDownLatch countDownLatch = new CountDownLatch(1);
   example.enqueue(new Callback<ResponseBody>() {
       @Override
       public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
           try {
               print(response.body().string());
           } catch (IOException e) {
               e.printStackTrace();
           }

           countDownLatch.countDown();
       }

       @Override
       public void onFailure(Call<ResponseBody> call, Throwable t) {
           countDownLatch.countDown();
       }
   });
   countDownLatch.await();
}

使用RequestBody对象

这里我们来提交一份JSON数据,首先还是再Service中创建一个方法:

@POST("/send")
public Call<ResponseBody> withRequestBody(@Url String url, @Body RequestBody body);

再接着创建我们的请求:

@Test
public void withRequestBody() throws Exception {
    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BASIC);

    OkHttpClient client = RetrofitManager.getClient(logging);

    Retrofit retrofit = RetrofitManager.getRetrofit(client);
    ExampleService service = retrofit.create(ExampleService.class);

    String url = "http://www.remoteurl.com";

    JSONObject result = new JSONObject();
    try {
        result.put("record", "hello");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    RequestBody body = RequestBody.create(MediaType.parse("application/json"), result.toString());
    Call<ResponseBody> example = service.withRequestBody(url, body);

    final CountDownLatch countDownLatch = new CountDownLatch(1);
    example.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                print(response.body().string());
            } catch (IOException e) {
                e.printStackTrace();
            }

            countDownLatch.countDown();
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            countDownLatch.countDown();
        }
    });
    countDownLatch.await();
}

总结

第一种方法,我们需要为每一个请求的对象创建一个Model,如果你不想创建model则可以选择第二种方式,直接创建一个JSON字符串,然后提交即可。还是相当简答的。

—— EOF ——

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值