Chain Retrofit 使用 RxJava 调用授权和后续调用

Chain Retrofit calls for authorization and subsequent calls using RxJava

我是 Rxjava 的新手。我想从第一个 api 调用的响应中捕获访问令牌(作为 headers 的一部分接收),然后将该访问令牌(作为 header)用于后续 api 来电。

注意:在第一个api调用中,响应没有body,只返回header。

我的网络接口:

public interface NetworkInterface {

@POST("https://appauth.treuspan/Login")
Observable<Response<Void>> getAuthDetails(
        @Header("Content-Type") String contentType,
        @Body AuthRequestDetails authRequestDetails
        );

@POST("https://appauth.treuspan//storedetails")
Observable<StoreDetailsResponse> getStoreDetails(
        @Header("Content-Type") String contentType,
        @Header("Accept") String accept,
        @Header("Authorization") String authorization,
        @Body AuthRequestBody authRequestBody
        );
}

我的可观察方法:

    public Observable<Response<Void>> getAuthObservable() {

    return NetworkClient.getRetrofit().create(NetworkInterface.class)
            .getAuthDetails("application/x-www-form-urlencoded",
                    new AuthRequestDetails().getAuthRequestDetails())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Response<Void>>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(Response<Void> voidResponse) {
                    String access_token_received = voidResponse.headers().get("access_token");
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });

以上无法编译。它的编译说:

Incompatible type:
required: io.reactivex.Observable<retrofit2.Response<java.lang.Void>>
Found:    void

如果我能够成功进行上述调用,那么我必须在后续调用中使用访问令牌作为 header 的一部分,如下所示

public Observable<StoreDetailsResponse> getStoreDetailsObservable() {
    return NetworkClient.getRetrofit().create(NetworkInterface.class)
            .getStoreDetails("application/x-www-form-urlencoded",
                    "application/json",
                    "Bearer access_token_received",
                    new AuthRequestBody().getAuthRequestBody())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<StoreDetailsResponse>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(StoreDetailsResponse response) {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });
}

以上代码也无法编译。它说:

Incompatible type:
required: io.reactivex.Observable<com.example.androidtest.model.StoreDetailsResponse>
Found:    void

请建议我怎样才能做到这一点。

subscribe(...) 将 return 订阅 尝试编辑您的代码,使方法 return 成为这样的订阅

    public Subscription getAuthObservable() {

        return NetworkClient.getRetrofit().create(NetworkInterface.class)
            .getAuthDetails("application/x-www-form-urlencoded",
                    new AuthRequestDetails().getAuthRequestDetails())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Response<Void>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onNext(Response<Void> voidResponse) {
                    String access_token_received = voidResponse.headers().get("access_token");
                }
            });
      }