未连接到Internet时发生致命exception
我正在努力学习Kotlin
和rxjava
。 我有一个非常简单的应用程序,只需点击一个button
,即可从网站获取一些数据。
一切工作正常,除非我关闭我的手机数据,在这种情况下,我得到一个致命的例外,没有解释为什么。
我的代码:
// In the activity's onCreate ViewObservable.clicks(button) .observeOn(Schedulers.io()) .subscribe{ fetchTest().observeOn(AndroidSchedulers.mainThread()) .subscribe{ s:String -> textView.text = s } } // Elsewhere in the activity code @Throws(IOException::class) private fun fetchTest(): Observable { val url = URL(MY_URL) val httpConnection = url.openConnection() var stream = httpConnection.inputStream val reader = BufferedReader(InputStreamReader(stream)) val response = reader.readLines().reduce { x, y -> x + y } stream?.close() return Observable.just(response) }
当我处于调试模式时,错误发生在这里:
var stream = httpConnection.inputStream
而logcat完全没有帮助:
02-12 10:13:37.748 8616-8645/com.example.app E/AndroidRuntime﹕ FATAL EXCEPTION: RxCachedThreadScheduler-1 Process: com.example.app, PID: 8616
我知道错误是因为没有网络连接,但添加try / catch无法捕捉它,所以我不知道该怎么办
观察者的用户也需要实现onError
- 与Android的Kotlin:基类和Kotlin Android扩展
- 应用程序没有安装错误在android中kotlin gradle 3.0.0-alpha5
- 在使用Kotlin协同程序时,与Room dao类错误
- 安卓Braintree,贝宝不工作
- Android体系结构组件:ViewModel在配置更改时返回null
- kotlin todo-mvp使用framents
- 如何在同一个src文件夹中运行不同的.kt文件/类?
- android.support.v4.app.FragmentManager不能提供@ Provide或@ Produces注解的方法
- 将android kotlin项目迁移到sdk 27