Android 在截取和重新运行结果中进行http调用
我有这个拦截器,想法是在拦截器中调用另一个API 问题是onResponse在第一次调用时被调用Android 在截取和重新运行结果中进行http调用,android,retrofit,Android,Retrofit,我有这个拦截器,想法是在拦截器中调用另一个API 问题是onResponse在第一次调用时被调用 val serivceCa = serivce.getDate("http://api.aladhan.com/v1/gToH?date=07-12-2014") 但是我要截取器调用的结果 val serivceCa=serivce.getDate(“http://api.aladhan.com/v1/gToH?date=07-12-2020年) addInterceptor(
val serivceCa = serivce.getDate("http://api.aladhan.com/v1/gToH?date=07-12-2014")
但是我要截取器调用的结果
val serivceCa=serivce.getDate(“http://api.aladhan.com/v1/gToH?date=07-12-2020年)
addInterceptor(对象:拦截器){
覆盖趣味拦截(链:Interceptor.chain):okhttp3.Response{
val request=chain.request()
val响应=链。继续(请求)
val body=响应。peekBody(2048)
val serivce=clientInstance.buildService(serivce::class.java)
val serivceCa=serivce.getDate(“http://api.aladhan.com/v1/gToH?date=07-12-2020")
国际单项体育联合会(旗){
val result=serivceCa.enqueue(对象:回调{
覆盖失效时的乐趣(调用:调用,t:可丢弃){
Log.d(“result”,“response.body().toString()”)
}
覆盖fun onResponse(调用:调用,响应:响应){
Log.d(“result222t”,response.body().toString())
}
})
}
flag=false
返回响应
}
})
有趣的callAPI(){
val serivce=clientinstance.buildService(serivce::class.java)
val serivceCa=serivce.getDate(“http://api.aladhan.com/v1/gToH?date=07-12-2014")
val result=serivceCa.enqueue(对象:回调{
覆盖失效时的乐趣(调用:调用,t:可丢弃){
Log.d(“result”,“response.body().toString()”)
}
覆盖fun onResponse(调用:调用,响应:响应){
Log.d(“result”,response.body().toString())
}
})
flag=true
}
}您想先得到内部请求的响应吗?这是你的问题吗?@AdityaKurkure是的,请注意,当我调试时,调用“return response”的次数比调用onresponse的次数多两次,但第一次调用的不是inner,您希望先调用内部请求的响应吗?这是你的问题吗?@AdityaKurkure是的,请注意,当我调试时,调用“return response”的次数比调用onresponse的次数多两次,但第一次调用的不是内部
addInterceptor (object : Interceptor {
override fun intercept(chain: Interceptor.Chain): okhttp3.Response {
val request= chain.request()
val response = chain.proceed(request)
val body =response.peekBody(2048)
val serivce = RetrofitClientInstance.buildService(Serivce::class.java)
val serivceCa = serivce.getDate("http://api.aladhan.com/v1/gToH?date=07-12-2020")
if (flag) {
val result = serivceCa.enqueue(object : Callback<String> {
override fun onFailure(call: Call<String>, t: Throwable) {
Log.d("result", "response.body().toString()")
}
override fun onResponse(call: Call<String>, response: Response<String>) {
Log.d("resul222222t", response.body().toString())
}
})
}
flag=false
return response
}
})
val serivce = RetrofitClientInstance.buildService(Serivce::class.java)
val serivceCa = serivce.getDate("http://api.aladhan.com/v1/gToH?date=07-12-2014")
val result = serivceCa.enqueue(object : Callback<String> {
override fun onFailure(call: Call<String>, t: Throwable) {
Log.d("result", "response.body().toString()")
}
override fun onResponse(call: Call<String>, response: Response<String>) {
Log.d("result", response.body().toString())
}
})
flag = true
}