简体   繁体   English

404 - 使用 Retrofit2 和 RXJava2 调用 API 时

[英]404 - when calling API with Retrofit2 and RXJava2

I have a problem with calling API with mentioned libs bellow我在使用下面提到的库调用 API 时遇到问题

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最后放置我调用 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.结果我得到 404 - 当我在没有 RxJava 的情况下调用 API 时没有问题。 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)

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

You need to analyze the class of Exception which comes to onError handler.您需要分析涉及onError处理程序的Exception类。

@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早些时候,我用简单的kotlin示例和更多解释回答了几乎相同的问题: https : kotlin

I hope this will help someone.我希望这会帮助某人。 I wasted a lot of my time.我浪费了很多时间。 It was basically responseCode of HTTPs request which playing this game.它基本上是玩这个游戏的HTTPs请求的responseCode。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM