4

我在使用下面提到的库调用 API 时遇到问题

我的依赖:

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'

提供改造实例的代码

@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接口

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

最后是我调用 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: ");
                }
            });

结果我得到 404 - 当我在没有 RxJava 的情况下调用 API 时没有问题。有谁可以告诉我出了什么问题或为什么我会收到这样的错误?

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)
4

2 回答 2

0

我不清楚,但如果问题不在不同的 HTTP 响应中,而是在不同的行为中(在 Retrofit 2 RxJava 而不是 onNext 中使用 401 响应触发 onError),那么它可以按设计工作。

您需要分析处理程序的ExceptiononError

@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);
    }
}

早些时候,我kotlin在这里用简单的例子和​​更多解释回答了几乎相同的问题: https ://stackoverflow.com/a/45868252/1824898

于 2017-08-24T18:20:53.857 回答
0

我希望这会对某人有所帮助。我浪费了很多时间。玩这个游戏的基本上是HTTPs请求的responseCode。

请参考:https ://stackoverflow.com/a/60630522/6236752

于 2020-03-11T06:43:03.573 回答