Retrofit2 添加header和baseparam

2024-08-30 04:48
文章标签 header retrofit2 baseparam

本文主要是介绍Retrofit2 添加header和baseparam,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一.添加header

1.请求拦截器:

方式一:OkHttpClient.Builder httpClient = new OkHttpClient.Builder();  httpClient.addInterceptor(new Interceptor() {  @Overridepublic Response intercept(Interceptor.Chain chain) throws IOException {Request original = chain.request();Request request = original.newBuilder().header("User-Agent", "Your-App-Name").header("Accept", "application/vnd.yourapi.v1.full+json").method(original.method(), original.body()).build();return chain.proceed(request);}}方式二:OkHttpClient.Builder httpClient = new OkHttpClient.Builder();  httpClient.addInterceptor(new Interceptor() {  @Overridepublic Response intercept(Interceptor.Chain chain) throws IOException {Request original = chain.request();Request request = original.newBuilder().addHeader("Cache-Control", "no-cache").addHeader("Cache-Control", "no-cache").method(original.method(), original.body()).build();return chain.proceed(request);}}OkHttpClient client = httpClient.build();  Retrofit retrofit = new Retrofit.Builder()  .baseUrl(API_BASE_URL).addConverterFactory(GsonConverterFactory.create()).client(client).build();
OkHtt请求头通过拦截器添加Header,两种方式的不同.header(key, val):如果key相同,最后一个val会将前面的val值覆盖.addHeader(key, val):如果key相同,最后一个val不会将前面的val值覆盖,而是新添加一个Header

2.动态添加

@GET("get.php")Call<StoryResponse> getPhp(@Header("headerType") String type);

 @GET("get.php")Call<StoryResponse> getPhp(@HeaderMap  Map<String,String> type);

3.静态添加

@Headers("headertype:2")@GET("get.php")Call<StoryResponse> getPhp();
  @Headers({"headertype:2","type:3"})@GET("get.php")Call<StoryResponse> getPhp();

二 .添加baseParam

为所有的网络请求添加公用的param,如uid,imei等,仍然使用到了拦截器:

 //base节点拦截器Interceptor baseParamInterceptor = new Interceptor() {@Overridepublic Response intercept(Chain chain) throws IOException {Request original = chain.request();HttpUrl originalUrl = original.url();HttpUrl url = originalUrl.newBuilder().addQueryParameter("apikey","1234").addQueryParameter("imei","1456").build();Request.Builder requestBuilder = original.newBuilder().url(url);Request request = requestBuilder.build();return chain.proceed(request);}};
然后添加拦截器就可以了:

 //定制OkHttpOkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();//OkHttp进行添加拦截器loggingInterceptorhttpClientBuilder..addInterceptor(headerInterceptor);return httpClientBuilder.build();






这篇关于Retrofit2 添加header和baseparam的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1119841

相关文章

【Http 每日一问,访问服务端的鉴权Token放在header还是cookie更合适?】

结论先行: token静态的,不变的,放在header里面。 典型场景 ,每次访问时需要带个静态token请求服务端,向服务端表明是谁请求,此时token也可以认为是个固定的access-key。token动态的,会失效,放在cookie里面。 典型场景,业务登录态token,存在有效期的,过一段时间可能会失效。 下面具体展开下。 在选择将鉴权 Token 放在 HTTP Header 还是

el-table使用#header自定义表头后脱离响应式问题处理

问题描述:当titleList的值进行筛选改变的时候#header里面的的数据并没有实时响应,如下面的代码 <el-table :data="newData" border style="width: 100%"><el-table-columnv-for="(day, index) in titleList" :key="day.date"width="600"align="center">

解决Vue请求 ‘No 'Access-Control-Allow-Origin' header is present on the requested resource’错误

如果我们用VueResouce直接请求,这样写(以豆瓣api为例): this.$http.get('https://api.douban.com//v2/movie/top250').then((response) => {this.movie = response.data;console.log(this.movie); }); 就会报错: 因为这是一个跨域的请求,不能直接

【Spring】获取cookie,session,header(3)

本系列共涉及4个框架:Sping,SpringBoot,Spring MVC,Mybatis。 博客涉及框架的重要知识点,根据序号学习即可。 目录 本系列共涉及4个框架:Sping,SpringBoot,Spring MVC,Mybatis。 博客涉及框架的重要知识点,根据序号学习即可。 1、获取cookie 1.1传统版本【使用HttpServletRequest】 1.2简洁版本

NavigationView findViewById can't find header view

在最新的android support library, (23.1.0)中,NavigationView中的headerlayout里,view不能找到,返回的是null,但是在这版本之前确实可以的,在google+上找到了原因: Ian Lake 2015年10月16日   1 Follow along on  https://c

retrofit + okhttp 数组 +header+ post + body[params]

很辛苦,搞定实际上是因为基础确实落下了很多,加上retrofit的body讲解太少,翻墙看了很多资料,最终,解决,给各位分享下代码,互相学习; 1.需求:添加header,并且数组进行post传参,@body api代码: @POST("void")Call<Bean> getbean( @Header("a") String a ,@Header("b") Stri

OGG-01389 File header failed to parse tokens.

OGG-01389 File header failed to parse tokens. http://blog.csdn.net/zbdba/article/details/44095105;  处理的思路:   1.查看日志   2.在目标端看最新的队列文件的日期,假如没有最新的队列文件就说明源端没有投递过来   3.在源端查看日志   4.在源端修改参数文件,使版本一致   5

Retrofit2 打印网络请求日志

1.新建log拦截器 HttpLoggingInterceptor.Level level= HttpLoggingInterceptor.Level.BODY;//新建log拦截器HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger()

invalid stream header: 76616C75

java 项目操作redis ,有如下三种方式  使用 原生的 Jedis 使用 StringRedisTemplate 使用 RedisTemplate<Object,Object> 但是他们并不是通用的,存取操作都涉及到 key value 的序列化和反序列化规则, 使用 Jedis 设置的key 再使用 StringRedisTemplate 进行get 是可以获取成功的,但是使用 R

http请求头header参数中不能包含_

服务器之所以会禁止,是因为下划线和中划线都为会被映射为 CGI 系统变量名中的下划线,这样容易引起混淆. 所以head中如app_set_id需转换为appSetId