当前位置:网站首页>Retrofit2.0 method summary of adding header
Retrofit2.0 method summary of adding header
2022-06-09 23:59:00 【Sharp surge】
One 、 Using annotations
1. Add a single header
public interface ApiService {
@Headers("Content-Type: application/javascript")
@GET("/data")
Call<List<AddressBean>> getData();
}
2. Add multiple header
public interface ApiService {
@Headers({
"Content-Type: application/javascript",
"User-Agent: YourAgent"
})
@GET("/data/{user_id}")
Call<List<AddressBean>> getData();
}3. Dynamic addition header
public interface ApiService {
@GET("/data")
Call<List<AddressBean>> getData(@Header("Content-Range") String contentRange);
}
4. Dynamically add multiple header
@GET("/search")
Call<ResponseBody> list(@HeaderMap Map<String, String> headers);1. Post Add a single header
@POST("api/sys/getPermissions")
Observable<ResponseBean<PermissionsBean>> getPermissions(@Query("__token") String token);
2.post Add fixed multiple header
@POST("api/sys/getPermissions")
@Headers({"Content-Type:application/x-www-form-urlencoded",
"Authorization:Basic VmJyZjJNU1FxejhBVlhzMmajhETkJV"})
Observable<TokenBean> getSsoToken();
Two 、 If you want to add a global header, You need to add header
/**
* Request interceptor
*/
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()))// Injection header information
.build();
logRequest(request);
return logResponse(chain.proceed(request));
}
}
/**
* Get header information
*/
private Map<String, String> getHeaders() {
HashMap<String, String> headersMap = new HashMap<>();
headersMap.put("Cookie","jeeplus.session.id");
return headersMap;
}
————————————————
Copyright notice : This paper is about CSDN Blogger 「 Pearl of Tianlan 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/u012693479/article/details/99645085
边栏推荐
- Simulated annealing-n queen problem
- "At the age of 35, I have no sideline, so I'm embarrassed to mix in the workplace": get rid of the sideline of dead salary recommendation
- Deploy MySQL based on statefulset in kubernetes (Part 1)
- Getting started with Emu8086
- IOS cache - nscache and sandbox cache
- To serve the "nervous system" with a broad and subtle vision
- Book club recruits | let's read "Mr. toad goes to see a psychologist"
- Question bank and answers of 2022 metal and nonmetal mine blasting examination
- Introduction to C # WPF layout control layoutcontrol
- POI exporting Excel
猜你喜欢

低边驱动和高边驱动

To serve the "nervous system" with a broad and subtle vision

Mazhiqiang: research progress and application of speech recognition technology -- RTC dev Meetup

Simulated annealing-n queen problem

读书会招募 | 一起来读《蛤蟆先生去看心理医生》吧

JVM 讲解

DDD驱动领域设计学习笔记

exness:推特表示继续向马斯克分享数据,并在7月底或8月初进行股东投票

只需八步将小程序一键打包生成App

Microcomputer principle and interface technology exercise 1
随机推荐
Anatomy of illusory rendering system (15) - XR topic
哨兵3(Sentinel-3)数据简介
Install idea
荐书 | 即使是来自星星的你,我也想去靠近
Q-learning
数字大时代来临,360携手创业黑马助力中小企业抓住关键未来
POI exporting Excel
Detailed explanation of IEEE 754 floating point number standard
14 乘法器:如何像搭乐高一样搭电路(下)?
C# WPF UI框架MahApps切换主题
Is it necessary to get PMP?
2022年金属非金属矿山爆破考试题库及答案
C # WPF background dynamic add control (Classic)
Deploy MySQL based on statefulset in kubernetes (Part 1)
辐射亮度与表观反射率的转换
打开xlsx文件时自动打开personal.xlsb表格文件
Manual single precision floating point type
2022金属非金属矿山支柱特种作业证考试题库模拟考试平台操作
"Leak detection and vacancy filling" Android internship interview knowledge points (II)
ArcMap解决几何错误