Rxjava and OkHttp request

0

I'm new to reactive programming and currently learning kotlin. I'm trying to make an API request using okhttp client but instead of using asynctask, I want to try rxjava.

fun network():Observable<String>{

    val exe = Observable.create<String> { emitter->

       emitter.onNext("https://pokeapi.co/api/v2/pokemon/500")
    }
    return exe.onErrorReturn{
        it.toString()
    }.subscribeOn(Schedulers.io()).flatMap {
        val c = OkHttpClient();
        val req = Request.Builder()
            .url(it)
            .build();

        val resp = c.newCall(req).execute()
        Observable.just(resp.toString())
    }

I created a method with return type of observable string which will contain a string url from pokemon API and then I added a subscribeOn method which will force it to do the okhttpClient request be executed in the io thread (do in background equivalent of asynctask) then finally subscribe to it via simply calling the method in the onCreate of my activity:

network().subscribe {
        Log.e("response",it);
    }

Unfortunately im having the following error:

     FATAL EXCEPTION: RxCachedThreadScheduler-1
io.reactivex.exceptions.OnErrorNotImplementedException: The exception was not handled due to missing onError handler in the subscribe() method call. Further reading: https://github.com/ReactiveX/RxJava/wiki/Error-Handling | javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb955faf8: Failure in SSL library, usually a protocol error
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:741 0x9da8e8c1:0x00000000)
    at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:704)
    at io.reactivex.internal.functions.Functions$OnErrorMissingConsumer.accept(Functions.java:701)
    at io.reactivex.internal.observers.LambdaObserver.onError(LambdaObserver.java:77)
    at io.reactivex.internal.observers.BasicFuseableObserver.onError(BasicFuseableObserver.java:100)
    at io.reactivex.internal.observers.BasicFuseableObserver.fail(BasicFuseableObserver.java:110)
    at io.reactivex.internal.operators.observable.ObservableMap$MapObserver.onNext(ObservableMap.java:59)
    at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeOnObserver.onNext(ObservableSubscribeOn.java:58)
    at io.reactivex.internal.operators.observable.ObservableOnErrorReturn$OnErrorReturnObserver.onNext(ObservableOnErrorReturn.java:65)
    at io.reactivex.internal.operators.observable.ObservableCreate$CreateEmitter.onNext(ObservableCreate.java:66)
    at ui.activities.ObjectManipulation$network$exe$1.subscribe(ObjectManipulation.kt:37)
    at io.reactivex.internal.operators.observable.ObservableCreate.subscribeActual(ObservableCreate.java:40)
    at io.reactivex.Observable.subscribe(Observable.java:12267)
    at io.reactivex.internal.operators.observable.ObservableOnErrorReturn.subscribeActual(ObservableOnErrorReturn.java:31)
    at io.reactivex.Observable.subscribe(Observable.java:12267)
    at io.reactivex.internal.operators.observable.ObservableSubscribeOn$SubscribeTask.run(ObservableSubscribeOn.java:96)
    at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578)
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:153)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:267)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:856)
 Caused by: javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb955faf8: Failure in SSL library, usually a protocol error
error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure (external/openssl/ssl/s23_clnt.c:741 0x9da8e8c1:0x00000000)

Line 37 in the error is the onNext(url). Any useful hint or reply would be greatly appreciated!

android
kotlin
rx-java2
okhttp
asked on Stack Overflow Dec 3, 2019 by porngrammer64

1 Answer

0

At first it seems that the problem comes from the OkHTTP client configuration, you can check this to see how to fix it.

If you want that the app doesn´t crash you should handle the error by implementing onError when you are subscribing, you can find more information here.

Finally I would recommend you to use Retrofit instead of using raw OkHTTP. You can find an example here.

answered on Stack Overflow Dec 3, 2019 by JJaviMS

User contributions licensed under CC BY-SA 3.0