我正在尝试从HTTP参数动态计算校验和。然后,我想添加这个校验和作为HTTP参数。
首先,我需要获得作为参数传入的字段,但看起来retrofit只能访问url查询参数。
@Gordak展示了获取查询参数的方法,但如果可能的话,我想要实现的是获取请求链中的post参数。
发布于 2016-11-11 09:37:39
好了,我们开始吧。
首先,构建您的OkHTTP客户端和retrofit对象。
OkHttpClient client = httpBuilder
.addNetworkInterceptor(INTERCEPTOR_REQUEST_ADD_CHECKSUM)
.build();
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.baseUrl("https://my.domain.com")
.build();然后,您需要定义拦截器:
private static final Interceptor INTERCEPTOR_REQUEST_ADD_CHECKSUM = new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
HttpUrl url = chain.request().url();
String param1 = url.queryParameter("param1");
String param2 = url.queryParameter("param2");
String chk = aMethodToComputeChecksum(param1,param2);
url = url.newBuilder().addQueryParameter("checksum", chk).build();
Request request = chain.request().newBuilder().url(url).build();
return chain.proceed(request);
}发布于 2016-11-11 08:41:06
也许它会有所帮助--尝试计算一次这个参数并用RequestInterceptor编写它。
https://stackoverflow.com/questions/40543444
复制相似问题