你好,我看到了header的添加逻辑,我直接把生成的逻辑粘贴到我的网络请求的拦截器里面了(拦截器的代码见下方),还是不行。我想请问一下,这个authToken是android生成的,还是后台生成返回的的,假如说是android的生成的话,是怎么生成的,假如说是后台生成的,是在哪里返回给我android的。我对这块authToken生成的逻辑比较混乱,抱歉
@Override
public Response intercept(Chain chain) throws IOException {
if (!NetworkUtils.isConnected()) {
LogUtils.e("当前无网络连接");
// ThreadManager.getInstance().runOnUIThread(() -> MyToast.showShortToast(Utils.getApp(), "哎呀,网络开小差去了..."));
}
Request originRequest = chain.request();
RequestBody body = originRequest.body();
Request.Builder builder = originRequest.newBuilder();
builder.addHeader(DataHelper.AUTHORIZATION_HEADER, DataHelper.authToken(originRequest.url().host())).build();
Response response = chain.proceed(originRequest);
String responseAuthToken = response.header(DataHelper.AUTHORIZATION_HEADER, null);
if (!TextUtils.isEmpty(responseAuthToken)) {
DataHelper.putAuthToken(originRequest.url().host(), responseAuthToken);
}
if (originRequest.method().equalsIgnoreCase("POST")) {
if (body != null) {
builder.post(body);
}
}
LogUtils.d("发送请求: method:" + originRequest.method() + "\n"
+ "url:" + originRequest.url() + "\n"
+ "请求头:" + originRequest.headers() + "\n"
+ "请求参数: " + "{" + JSON.toJSONString(body) + "}");
return chain.proceed(builder.build());
}
object DataHelper {
const val WFC_OKHTTP_COOKIE_CONFIG = "WFC_OK_HTTP_COOKIES"
const val AUTHORIZATION_HEADER = "authToken"
@JvmStatic
fun authToken(host:String):String?{
val sp: SharedPreferences = Utils.getApp().getSharedPreferences(WFC_OKHTTP_COOKIE_CONFIG,
Context.MODE_PRIVATE
)
val authToken = sp.getString("$AUTHORIZATION_HEADER:$host", null)
if (Util.isEmpty(authToken)){
return ""
}
return authToken
}
@JvmStatic
fun putAuthToken(host:String,responseAuthToken:String) {
val sp: SharedPreferences = Utils.getApp().getSharedPreferences(
WFC_OKHTTP_COOKIE_CONFIG,
Context.MODE_PRIVATE
)
sp.edit()
.putString("$AUTHORIZATION_HEADER:$host", responseAuthToken)
.apply()
}
}