RxAndroid+;改装callTimeout';t触发器错误
我正在使用RxAndroid+改进来发出http请求。代码如下所示:RxAndroid+;改装callTimeout';t触发器错误,android,retrofit,rx-java,okhttp,rx-android,Android,Retrofit,Rx Java,Okhttp,Rx Android,我正在使用RxAndroid+改进来发出http请求。代码如下所示: Interceptor headerInterceptor = getHeaderInterceptor(); HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY
Interceptor headerInterceptor = getHeaderInterceptor();
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.callTimeout(5, TimeUnit.SECONDS)
.addInterceptor(headerInterceptor)
.addInterceptor(httpLoggingInterceptor)
.build();
Gson gson = new GsonBuilder()
.setLenient()
.create();
sRetrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
像这样使用它:
ApiProvider.provideApi(MyApi.class)
.submit(id, mRequest)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
response -> {
Log.w("tag", "success");
},
throwable -> {
Log.w("tag", "error");
}
);
我将connectTimeout/readTimeout/writeTimeout设置为60秒,将callTimeout设置为5秒
我知道这个配置可能不合理,但我只想在5秒后得到一个超时异常,并且Log.w(“tag”,“error”)可以调用代码>
然而,我发现我的测试永远不会调用这条线。如果我将connectionTimeout
设置为1秒,那么将立即调用此行
那么,如果我希望callTimeout触发日志错误行,我应该怎么做 正如我所看到的,我认为您的问题可能不是您为callTimeout(5,TimeUnit.seconds)
设置了多少秒,我认为您的可能Rx流已经抛出了一些错误,所以流刚刚中断您可以从这里得到任何响应。然而,你们将时间秒重置为1s,然后重启应用程序,这个时间流不会中断,你们会得到错误。
所以,简单地重新测试它,以确保您的流在进入此订阅之前不会收支平衡
我已经用一些愚蠢的实现进行了测试:
class MainActivity : AppCompatActivity() {
val delayInterceptor = object : Interceptor {
override fun intercept(chain: Interceptor.Chain): okhttp3.Response {
Thread.sleep(6000L)
return chain.proceed(chain.request())
}
}
val client: OkHttpClient = OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.callTimeout(5, TimeUnit.SECONDS)
.addInterceptor(delayInterceptor)
.build()
val retrofit: Retrofit = Retrofit.Builder()
.baseUrl("https://en.wikipedia.org/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
data class Wiki(
@SerializedName("type")
val type: String
)
interface WikiService {
@GET("api/rest_v1/page/random/summary")
fun getRandomSummary(): Single<Wiki>
}
@SuppressLint("CheckResult")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
retrofit.create(WikiService::class.java)
.getRandomSummary()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
Log.d("tag", "success")
}, {
Log.e("tag", "error")
})
}
}
class MainActivity:AppCompatActivity(){
val delayInterceptor=对象:拦截器{
覆盖趣味拦截(链:Interceptor.chain):okhttp3.Response{
线程睡眠(6000L)
返回链。继续(链。请求())
}
}
val client:OkHttpClient=OkHttpClient.Builder()
.connectTimeout(60,时间单位为秒)
.writeTimeout(60,时间单位。秒)
.readTimeout(60,时间单位为秒)
.callTimeout(5,时间单位为秒)
.addInterceptor(延迟侦听器)
.build()
val改装:改装=改装.Builder()
.baseUrl(“https://en.wikipedia.org/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.客户(客户)
.build();
数据类维基(
@序列化名称(“类型”)
val类型:String
)
接口WikiService{
@获取(“api/rest\u v1/page/random/summary”)
乐趣getRandomSummary():单
}
@SuppressLint(“检查结果”)
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
create(WikiService::class.java)
.getRandomSummary()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.订阅({
Log.d(“标记”、“成功”)
}, {
Log.e(“标记”、“错误”)
})
}
}
我终于找到了原因
我使用的是com.jakewharton.reformation2.adapter.rxjava2.RxJava2CallAdapterFactory
,根据自述文件,它已被弃用:
切换到2.adapter.rxjava2.RxJava2CallAdapterFactory
后,一切都开始正常工作
顺便说一句,对于任何可能对他们之间的区别感兴趣的人来说?附上我在下面找到的钥匙信息:
尝试添加。retryOnConnectionFailure(false)
5秒呼叫超时非常少…大约50秒或1分钟。@AntonisRadz尝试retryOnConnectionFailure(false)
,不起作用。但还是要谢谢你@PrajwalW嗯,我同意5秒太短了,但是如果我将连接超时设置为1秒,代码运行良好,我可以调用onError
。非常感谢。正如你提到的,连接超时似乎小于调用超时。请试试这个。设置calltimeout值>连接超时值。感谢您的回答!请您帮助确认Log.e(“标记”、“错误”+it)
,其中it
指的是中断异常:超时
?就像回调是由callTimeout触发的一样,它应该是这个错误而不是其他错误。如果响应不是json对象而是纯字符串,则触发onError的原因可能不止一个,例如SocketTimeoutException
(由connectionTimeout引起)或ClassCastException。。谢谢你的帮助!!
This is now DEPRECATED!
Retrofit 2.2 and newer have a first-party call adapter for RxJava 2: https://github.com/square/retrofit/tree/master/retrofit-adapters/rxjava2