当前位置:网站首页>Retrofit2.0 添加Header的方法总结
Retrofit2.0 添加Header的方法总结
2022-06-09 23:22:00 【锐湃】
一、使用注解
1. 添加单个header
public interface ApiService {
@Headers("Content-Type: application/javascript")
@GET("/data")
Call<List<AddressBean>> getData();
}
2. 添加多个header
public interface ApiService {
@Headers({
"Content-Type: application/javascript",
"User-Agent: YourAgent"
})
@GET("/data/{user_id}")
Call<List<AddressBean>> getData();
}3. 动态添加header
public interface ApiService {
@GET("/data")
Call<List<AddressBean>> getData(@Header("Content-Range") String contentRange);
}
4. 动态添加多个header
@GET("/search")
Call<ResponseBody> list(@HeaderMap Map<String, String> headers);1. Post添加单个header
@POST("api/sys/getPermissions")
Observable<ResponseBean<PermissionsBean>> getPermissions(@Query("__token") String token);
2.post添加固定的多个header
@POST("api/sys/getPermissions")
@Headers({"Content-Type:application/x-www-form-urlencoded",
"Authorization:Basic VmJyZjJNU1FxejhBVlhzMmajhETkJV"})
Observable<TokenBean> getSsoToken();
二、如果要添加全局header,则需要在代码中添加header
/**
* 请求拦截器
*/
private class RequestInterceptor implements Interceptor {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request oldRequest = chain.request();
Request request = oldRequest.newBuilder()
.headers(Headers.of(getHeaders()))//注入头信息
.build();
logRequest(request);
return logResponse(chain.proceed(request));
}
}
/**
* 获取头信息
*/
private Map<String, String> getHeaders() {
HashMap<String, String> headersMap = new HashMap<>();
headersMap.put("Cookie","jeeplus.session.id");
return headersMap;
}
————————————————
版权声明:本文为CSDN博主「天兰之珠」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u012693479/article/details/99645085
边栏推荐
- CSRF/XSRF简介
- “當你不再是程序員,很多事會脫離掌控”—— 對話全球最大獨立開源公司SUSE CTO
- IEEE 754浮点数标准详解
- C # WPF generates table with variable rows and columns from background code
- String-4-242. Valid Letter ectopic words
- I've taken it. The MySQL table has 500W rows. Is there anyone who doesn't partition?
- What is a distributed software system
- 思维导图----3、SQL注入漏洞
- Microcomputer principle and interface technology exercise 1
- 企业无忧 | Apipost私有化部署活动即将火热开启
猜你喜欢

On chip variation (OCV) concept learning

Server operation and maintenance environment security system (Part 2)

Book club recruits | let's read "Mr. toad goes to see a psychologist"

Recommendation letter | look at the sky step by step, hand in hand, and see the stars one by two

14 乘法器:如何像搭乐高一样搭电路(下)?

“35岁还没副业,都不好意思混职场”:摆脱死工资推荐这种副业

在线文本字符串批量替换工具

Can I take the PMP Exam without project management experience?

蓝桥杯_分割立方体_组合数学_加法原理

牛客网:数据流中的中位数
随机推荐
双塔模型:ERNIE-Gram预训练精排Matching
« Lorsque vous n'êtes plus programmeur, beaucoup de choses échappent au contrôle » - conversation avec Suse CTO, la plus grande entreprise open source indépendante au monde
模型部署简述
Difference between stacked cable optical module and ordinary optical module
模拟退火-n皇后问题
Some problems of youtubednn recall
Swift GCD Notify after concurrent execution Lock barrier
荐书 | 手牵手一步两步望着天,看星星一颗两颗连成线
蓝桥杯_分割立方体_组合数学_加法原理
Openharmony risc-v lightweight system porting - shared with w800 porting
exness:推特表示继续向马斯克分享数据,并在7月底或8月初进行股东投票
2022金属非金属矿山支柱特种作业证考试题库模拟考试平台操作
Dynamic reading of protobuf data
I haven't seen this knowledge -- MySQL service evolution
[volume guide] mendeley document management tool tutorial
15 charts to give you a deeper understanding of floating point numbers
emu8086的入门使用
Virtual machine environment configuration record 1
On chip variation (OCV) concept learning
C# WPF从后台代码生成行列可变的表格