Zaylour 2018-09-03 01:19 采纳率: 69.2%
浏览 2164
已结题

异步网络请求,用的是Retrofit+okhttp+rxjava网络不好的情况下,怎么给出提示。

一个点击按钮,里面是一个网络异步请求,我点击时会出来一个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

  • 写回答

1条回答 默认 最新

  • devmiao 2018-09-03 04:25
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?