Retrofit初探——POST方式提交JSON数据
来源:互联网 发布:虚拟机与主机网络连接 编辑:程序博客网 时间:2024/05/20 20:44
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);
测试代码:
@Testpublic 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);
再接着创建我们的请求:
@Testpublic 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 ——
0 1
- Retrofit初探——POST方式提交JSON数据
- 使用Retrofit通过post提交Json数据
- 王学岗Retrofit初探(三)——json的数据
- Retrofit如何提交json数据
- POST提交JSON数据
- 以httpclient方式提交数据—post方式
- POST 提交数据方式
- POST提交数据方式
- 以Post方式提交并解析json数据
- application/json 四种常见的 POST 提交数据方式
- application/json 四种常见的 POST 提交数据方式
- from 表单post方式提交,返回json数据
- application/json 四种常见的 POST 提交数据方式
- HttpURLconnection使用POST方式提交JSON数据给服务器
- application/json 四种常见的 POST 提交数据方式
- application/json 四种常见的 POST 提交数据方式
- application/json 四种常见的 POST 提交数据方式
- HttpURLconnection使用POST方式提交JSON数据给服务器
- 【codevs1031 质数环】回溯法
- Struts2 与Servlet API解耦,耦合的访问方式
- ppt实现水滴效果
- 装饰者设计模式
- 计算机系统分类
- Retrofit初探——POST方式提交JSON数据
- Mysql的优化和架构浅析
- 279. Perfect Squares
- Rdd成员变量Dependency介绍(类型及其如何被初始化)
- tips:js没有块级作用域。
- Android UI开发第二十六篇——Fragment间的通信
- 二阶魔方还原教程
- 对话框弹出(Bootstrap)
- 利用Python爬虫获取IP2Location定位数据