设置此类拦截器后有没有办法删除特定标头:
public class AuthInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val original: Request = chain.request()
val request: Request = original.newBuilder()
.addHeader(AppConstant.HEADER_APP_TOKEN, AppConfig.apptoken) //<-- need to remove this one for only one request
.addHeader(AppConstant.HEADER_SECURITY_TOKEN, AppConfig.security_token)
.method(original.method(), original.body())
.build()
return chain.proceed(request)
Run Code Online (Sandbox Code Playgroud)
这是我的改造实例:
object RetrofitClientInstance {
private val httpClient = OkHttpClient.Builder()
.addInterceptor(AuthInterceptor())
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS))
private val retrofit = Retrofit.Builder()
.baseUrl(AppConstant.SERVER_BETA)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build()
fun <T> getRetrofitInstance(service: Class<T>): T {
return retrofit.create(service)
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 API 服务:
interface ApiService {
@GET("/app/shoes") …Run Code Online (Sandbox Code Playgroud) 我从服务器收到 UTC 格式的时间。我解析为一个Instant对象,然后将其转换为LocalDateTime. 像这样:
fun String.toLocaleDateTime(): LocalDateTime {
return LocalDateTime.ofInstant(
Instant.parse(this),
ZoneOffset.systemDefault()
)
}
Run Code Online (Sandbox Code Playgroud)
问题是不同年份的LocalDateTime对象是不同的。
例如,将 UTC 时间转换为 IST 时间时:
1800-01-01T03:36:32Z -> 1800-01-01T09:30
1870-01-01T03:36:32Z -> 1870-01-01T08:57:42
1906-01-01-01T30Z:36:32Z -01-01T09:06:32