【问题标题】:Android:Returning from Rx Android RetrofitAndroid:从 Rx Android Retrofit 回归
【发布时间】:2017-09-26 21:08:11
【问题描述】:

我有一个返回 Observable 的类方法。 observable 包含传递给 post 方法所需的授权密钥。我能够从一个类返回值,但无法按时在另一类中捕获它。我正在阅读并且人们使用了 Observable.blocking.first 来阻止当前线程,这不是一个好方法。任何人都知道如何在另一个班级中抓住它。这是我的代码:

类返回 observable:

public class GetAuthenticationToken {
     String authentication_token;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://abc.def.com/")
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        GetTokenInterface service = retrofit.create(GetTokenInterface.class);

        Observable<String> response_call = service.getToken1(setAPI_KEY, getaccesstoken);
        String savedToken;
        @CheckResult
        public Observable<String> getToken() {
            return service.getToken1(setAPI_KEY, getaccesstoken)
                    .subscribeOn(Schedulers.newThread())
                    .doOnNext(new Action1<String>() {
                        public void call(String token) {
                            savedToken = token;
                        }
                    }) 
                    .observeOn(AndroidSchedulers.mainThread());
        }

        public String getSavedToken() {
            return savedToken;
        }
}

但不知道在不阻塞线程的情况下在另一个类中按时捕获它:

  public class Post_To_Api {
GetAuthenticationToken getauthenticationtoken = new GetAuthenticationToken();
        Observable<String> generated_token = getauthenticationtoken.getToken(); //Dont know to get it here on time without blocking thread
                    generated_token.subscribeOn(Schedulers.newThread())
                            .observeOn(AndroidSchedulers.mainThread()) 
                            .subscribe(new Subscriber<String>() {
                                @Override
                                public void onCompleted() {

                                    System.out.print("Complete");
                                }

                                @Override
                                public void onError(Throwable e) {

                                    System.out.print("Fail");
                                }

                                @Override
                                public void onNext(String token) {
                                    authentication_token = token; //getting correct value here
                                }
                            });

    }

【问题讨论】:

  • 你想做什么?刷新令牌?
  • 我正在尝试从 gettoken() 方法返回值并在另一个类中使用它
  • 更明确。
  • 不知道如何按时得到返回值
  • @FlorescuGeorgeCătălin 查看我的编辑。我不知道如何在 Post_To_Api 类中按时获得价值

标签: android retrofit retrofit2 rx-android


【解决方案1】:

解决方案是保持异步执行,而不是使其阻塞。

MyApiServide myApiService = // Create your retrofit API service 

public Observable<SomeApiResult> requestApiItem() { 
    return getApiToken()
       .flatMap(new Func1<String, Observable<SomeApiResult>>() {
           public Observable<SomeApiResult> call(String token) {
               return myApiService.requestItem(token);
           }
       })
       .subscribeOn(Schedulers.io())
}


public Observable<String> getApiToken() {
    // get token from API as you have
}

// Were you use the API
requestApiItem()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Subscriber<SomeApiResult>() {
        @Override
        public void onCompleted() {
            System.out.print("Complete");
        }

        @Override
        public void onError(Throwable e) {
            System.out.print("Fail");
        }

        @Override
        public void onNext(SomeApiResult apiResult) { 
            myView.setText(apiResult.someValue); // or whatever you want to do with result.
        }
    });

【讨论】:

    猜你喜欢
    • 2017-09-29
    • 2018-10-11
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 2017-10-15
    相关资源
    最近更新 更多