Android Retrofit2 Post请求添加Json类型参数笔记
来源:互联网 发布:创盈门窗优化软件 编辑:程序博客网 时间:2024/06/07 01:12
Android Retrofit2 Post请求添加Json类型参数笔记
一.添加Header
1.添加单独Header
对于某个API所需要添加Header时,可以直接在Service接口上添加@Headers注解:
@Headers({ "Content-Type: application/json;charset=UTF-8", "User-Agent: Retrofit-your-App"})@GET(ConstantsApi.douban_in_theaters)Observable<MovieTheatersModel> requestTheatersMovies(@Query("city")String city, @Query("count")Integer count, @Query("start")Integer start);
2.添加共同Header
如上,我们可以发现,大部分API「”Content-Type: application/json;charset=UTF-8”」都是Header中必须的,那么我们可以直接添加在拦截器中:
new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create()) .client(new OkHttpClient.Builder() .addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request() .newBuilder() .addHeader("Content-Type", "application/json;charset=UTF-8") .addHeader("header2", "123456") .addHeader("header3", "123456") .addHeader("header4", "123456") .build(); return chain.proceed(request); } }).build();
然后你说直接这样每次都add一个匿名的intercept,令人头大的缩进,也太不软件工程了,那就直接封装一个:
public class MyInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request() .newBuilder() .addHeader("Content-Type", "application/json;charset=UTF-8") .addHeader("header2", "123456") .addHeader("header3", "123456") .addHeader("header4", "123456") .build(); return chain.proceed(request); }}
每次请求需要这些header的时候只需要:
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);MyInterceptor myInterceptor = new MyInterceptor();OkHttpClient client = new OkHttpClient() .newBuilder() .addInterceptor(httpLoggingInterceptor) .addInterceptor(myInterceptor) .build();//添加自定义InterceptormovieService = new Retrofit.Builder() //配合Retrofit .baseUrl(ConstantsApi.BASE_DOUBAN) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(client) .build() .create(DoubanMovieService.class);
二.添加Json类型Body
假定需求Json格式如下:
{ "param1": "111", "param2": "222", "data": { "param3": "string", "param4": "string2" }}
1.创建JsonObject对象
JSONObject root = new JSONObject(); JSONObject requestData = new JSONObject(); try { requestData.put("param3", "string"); requestData.put("param4", "string2"); root.put("param1", "111"); root.put("param2", "222"); root.put("data", requestData); } catch (JSONException e) { e.printStackTrace(); }
2.API 接口设置
@POST("api/data?")Observable<PromotionModel> getResult(@Body RequestBody requestBody);
3.联网请求(包含上述1中代码)
JSONObject root = new JSONObject();JSONObject requestData = new JSONObject();try { requestData.put("param3", "string"); requestData.put("param4", "string2"); root.put("param1", "111"); root.put("param2", "222"); root.put("data", requestData);} catch (JSONException e) { e.printStackTrace();}RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),root.toString());OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new Interceptor()) .addInterceptor(logging) .build();service = new Retrofit.Builder() .client(client) .baseUrl(Constants.BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build() .create(Service.class);return service.getResult(requestBody);
0 0
- Android Retrofit2 Post请求添加Json类型参数笔记
- swift post请求参数类型为json
- postman测试post请求参数为json类型
- Android——retrofit2请求参数注解
- Android post请求网络数据(json作为参数)
- Android HttpURLconnection使用POST请求参数是json
- post请求添加公共参数
- [Android] Retrofit2.0 GET和POST方式请求
- Android Retrofit2.0-post&get请求&文件上传&结合Rxjava
- android笔记之--向网络发送GET/POST请求参数
- android笔记之--向网络发送GET/POST请求参数
- python http请求post参数,json解析
- post请求中参数为json格式
- Post 请求参数 数据装载. 生成JSON
- http post 请求参数格式是json
- Java Post请求 传递Json参数
- android设置post请求服务器时参数的设置以及JSON参数的设置
- fiddler 模拟 post请求添加参数
- 堆排序 Heap Sort
- poj3041_匈牙利算法_二分图最小覆盖点(最大匹配数)
- Link 标签 rel=Stylesheet的实际作用
- Python在工作中的应用
- java播放音频文件2017.4.20绝对有效 粘贴运行!
- Android Retrofit2 Post请求添加Json类型参数笔记
- 每日三题-Day1-C(HDU 1069 Monkey and Banana 最大有序子序列和)
- iOS cookie详解
- Kafka安装配置
- hibernate教程--常用配置和核心API详解
- 分布式,云计算和大数据主流技术
- JSP+Servlet+MySQL完成将数据库中的数据显示到页面
- 数据结构下课复习2
- 基于Docker搭建Hadoop集群