我在Rx java中使用Retrofit,并且在onNext中获得身份验证令牌的值。根据onNext的结果,我必须通过在自定义标头中添加令牌来将数据发布到服务器。我是否应该在on next中编写所有代码,是否有wy从onNext返回值,所以当value可用时?

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

                Observable<String> response_call = service.getToken1(setAPI_KEY, setaccesstoken);

                response_call.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
                            }
                        });
                return authentication_token;
    }

}


然后在另一个类中:

generated_token = getToken();


但由于值不可用,我将生成generate_token空值是否有任何方法可以获取值?我必须使用此值进行发布,还是需要结合在onNext()中观察到的发布?

最佳答案

RxJava代码是异步执行的。例如

public class Foo {

    String value = "a";

    public String getFoo() {
        value = "a"; // This executes first
        Observable.just(value + "a")
                .observeOn(Schedulers.io())
                .subscribe(new Action1<String>() {
                    public void call(String v) {
                        value = v; // This is called later on IO thread.
                    }
                }); // This creates the observable and schedules execution on IO thread.
        return value; // Return value "a"
    }
}

Foo foo = new Foo();
foo.getFoo(); // Return a
Thread.sleep(100);
foo.getFoo(); // likley Return `aa` (depends upon how long the IO thread take to run)
Thread.sleep(100);
foo,getFoo(); // likley Return `aaa` (depends upon how long the IO thread take to run)


如果要从方法getToken中获取值,则返回Observable<String>(如果需要),还可以将令牌保存在类中,如下所示。

public class TokenService {

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://abc.efg.com/")
            .addConverterFactory(ScalarsConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            .build();
    GetTokenInterface service = retrofit.create(GetTokenInterface.class); // This should be done outside of the method for performance

    String savedToken;

    public Observable<String> refreshToken() {
        return service.getToken1(setAPI_KEY, setaccesstoken)
                .subscribeOn(Schedulers.newThread())
                .doOnNext(new Action1<String() {
                    public void call(String token) {
                        savedToken = token;
                    }
                }) // Do want is needed with token
                .observeOn(AndroidSchedulers.mainThread());
    }

    public String getSavedToken() {
        return savedToken;
    }

}

关于android - Android Retrofit:其他类中需要的onNext值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43682538/

10-11 18:50