一个点击按钮,里面是一个网络异步请求,我点击时会出来一个dialog:
test.setOnClickListener{
initDialog()
RetrofitManager2.getApiBase().getRecognition("参数...")
.timeout(10, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()).subscribe( Action1 {
if (mDialog != null) {
mDialog!!.dismiss()
}
}, ApiErrorHandler(object : ApiErrorListener(){//这里是自定义的错误
override fun onConnectException(t: Throwable) {
super.onConnectException(t)
ToastUtils.showShort(R.string.no_internet)
}
override fun onDatabaseException(t: DatabaseException) {
super.onDatabaseException(t)
}
override fun onOtherException(t: Throwable) {
super.onOtherException(t)
}
}))
}
fun initDialog() {
if (mDialog != null) {
mDialog!!.dismiss()
}
mDialog = Dialog(this, R.style.Dialog)
mDialog!!.setContentView(R.layout.firset_dialog_view)
val window = mDialog!!.getWindow()
val lp = window!!.attributes
val screenW = getScreenWidth()
lp.width = (0.6 * screenW).toInt()
val titleTxtv = mDialog!!.findViewById<View>(R.id.tvLoad) as TextView
titleTxtv.text = "请稍等..."
mDialog!!.show()
mDialog!!.setCanceledOnTouchOutside(false)
}
如果在网络不好的情况下,这个dialog会一直在转圈,我想设置一个10秒的时间,如果这个请求还没有完成就给用户一个"当前网络信号不好",这样的提示,我用了rxjava的timeout,但是我找不到onError这个方法,都是onErrorReturn、onErrorResumeNext、onExceptionResumeNext这几个,对rxjava的运用还是不怎么熟悉,请问这个应该怎么实现。
object RetrofitManager {
private val unsafeHttpClient: OkHttpClient by lazy { createUnSafeOkHttpClient() }
private val httpClient: OkHttpClient by lazy { createOkhttpClient() }
private val retrofitClient: Retrofit by lazy { createRetrofit() }
private val api:ApiBase by lazy { retrofitClient.create(ApiBase::class.java) }
fun getOkHttpClient(): OkHttpClient = httpClient
fun getUnSafeOkHttpClient(): OkHttpClient = unsafeHttpClient
fun getRetrofit(): Retrofit = retrofitClient
fun getApiBase(): ApiBase = api
private fun createOkhttpClient(): OkHttpClient {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
return OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.addInterceptor(ApiPostInterceptor())
.addInterceptor(ApiResponseInterceptor())
.addInterceptor(ApiAutoBaseUrlInterceptor())
.build()
}
private fun createRetrofit(): Retrofit {
return Retrofit.Builder()
.baseUrl(TT_PHP_SERVER)
.client(httpClient)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build()
}
private fun createUnSafeOkHttpClient(): OkHttpClient {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
return UnsafeOkHttpClient.getUnsafeOkHttpClientBuilder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(15, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.build()
}
}
@POST("china_mobile_recognition.php")
@FormUrlEncoded
fun getRecognition(@Field("session") session:String?,@Field("user_id")user_id:String?,@Field("appid")appid:String?
,@Field("token")token:String?,@Field("timestamp")timestamp:String?,@Field("phonenum")phonenum:String?
,@Field("msgid")msgid:String?,@Field("Sign")Sign:String?): Observable