有没有办法编辑网络呼叫的主体,以添加95%的呼叫中使用的默认属性?
我已经看到一个查询参数很容易添加(link)
但是,我还没有看到它的身体。
我的问题是我正在使用一个旧API,要求我在每个请求中发送令牌。所以我需要在大多数类中添加这一行。
@SerializedName("token") val token: String
有任何想法吗?
如果发送标头,则应使用httpInterceptor解决此问题
final OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request()
.newBuilder()
// add token key on request header
// key will be using access token
.addHeader("token", yourToken)
.build();
return chain.proceed(request);
}
});
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
编辑:对不起,我已经意识到你现在要求发送身体。
我认为用古老的方式可以实现(没有Gson,Moshi等)。这比添加每个请求更烦人。