404 - 使用Retrofit2和RXJava2调用API时

时间:2017-01-24 10:48:26

标签: android retrofit2 rx-android rx-java2

我在使用上面提到的libs调用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)

2 个答案:

答案 0 :(得分:0)

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

您需要分析来自Exception处理程序的onError类。

@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

答案 1 :(得分:0)

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

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