나는 배우려고 노력 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<String> {
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를 추가하면 잡을 수 없으므로 어떻게해야할지 모르겠습니다.
뷰 Observable의 구독자도 onError를 구현해야했습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다