4
votes

I have a problem with calling API with mentioned libs bellow

My dependencies:

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.0.1'

Code which provides retrofit instance

@Provides
@Singleton
RxJava2CallAdapterFactory provideRxJavaCallAdapter(){
    return RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io());
}

@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient, RxJava2CallAdapterFactory rxJavaCallAdapterFactory){
    return new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addCallAdapterFactory(rxJavaCallAdapterFactory)
            .client(okHttpClient)
            .build();
}

Api interface

@GET("form/{slugOrUUID}")
Observable<CoreObject> getForumRx(@Path("slugOrUUID") String slugOrUUID);

And finally place where I'm calling REST

    ForumService forumService = retrofit.create(ForumService.class);

    Observable<CoreObject> photography = forumService.getForumRx("sample");

    photography.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<CoreObject>() {
                @Override
                public void onSubscribe(Disposable d) {
                    Log.i(TAG, "onSubscribe: ");
                }

                @Override
                public void onNext(CoreObject value) {
                    Log.i(TAG, "onNext: ");
                }

                @Override
                public void onError(Throwable e) {
                    Log.e(TAG, "onError: ", e);
                }

                @Override
                public void onComplete() {
                    Log.i(TAG, "onComplete: ");
                }
            });

As a result Im getting 404 - there is no problem when I'm calling API without RxJava. Does anyone can tell me what's wrong or why I'm getting such error?

com.jakewharton.retrofit2.adapter.rxjava2.HttpException: HTTP 404 
                                                               at com.jakewharton.retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:54)
                                                               at com.jakewharton.retrofit2.adapter.rxjava2.BodyObservable$BodyObserver.onNext(BodyObservable.java:37)
                                                               at com.jakewharton.retrofit2.adapter.rxjava2.CallObservable.subscribeActual(CallObservable.java:43)
                                                               at io.reactivex.Observable.subscribe(Observable.java:10179)
                                                               at com.jakewharton.retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
                                                               at io.reactivex.Observable.subscribe(Observable.java:10179)
                                                               at io.reactivex.internal.operators.observable.ObservableSubscribeOn$1.run(ObservableSubscribeOn.java:39)
                                                               at io.reactivex.Scheduler$1.run(Scheduler.java:134)
                                                               at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:59)
                                                               at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:51)
                                                               at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                               at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
                                                               at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                               at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                               at java.lang.Thread.run(Thread.java:761)
2
Can you share the URL you're calling? Does it return a valid value in curl? - ditn
I can't share the URL but as I mentioned I have no problem when I'm not using rx for that also Postman does not have a problem with that. - Robert
Include a logging interceptor and check whether the right url is called. - nhaarman
Voting to close since it is a problem that can no longer be reproduced or that was caused by a simple typographical error. - nhaarman
photography.subscribeOn(Schedulers.io()) is redundant, you have created the RxJava2CallAdapterFactory with this scheduler. Try adding gist.github.com/agustinsivoplas/… for more details. - AndroidRuntimeException

2 Answers

0
votes

It is not clear for me, but if the problem is not in different HTTP rsponses, but in different behaviour (getting triggered onError with 401 response in Retrofit 2 RxJava instead of onNext), then it works as designed.

You need to analyze the class of Exception which comes to onError handler.

@Override
public void onError(Throwable e) {
    if(e instanceof HttpException) {
         // Cast e to HttpException and do what you need to
    } else {
         // This is another exception, like invalid JSON, etc.
         Log.e(TAG, "onError: ", e);
    }
}

Earlier I answerd almost the same question with simple kotlin example and more explanations here: https://stackoverflow.com/a/45868252/1824898

0
votes

I hope this will help someone. I wasted a lot of my time. It was basically responseCode of HTTPs request which playing this game.

Please refer to this: https://stackoverflow.com/a/60630522/6236752