Git Product home page Git Product logo

Comments (7)

liujingxing avatar liujingxing commented on June 19, 2024

自己检查下Observer对象是否导入错了

from rxlife.

fredyzhang5532 avatar fredyzhang5532 commented on June 19, 2024

去掉
.to(RxLife.to(this))
就运行正常

from rxlife.

liujingxing avatar liujingxing commented on June 19, 2024

那就是你Observable对象的泛型不对,takeUntil操作符应该返回了Observable对象,应该需要返回Observable对象

from rxlife.

liujingxing avatar liujingxing commented on June 19, 2024

不对,请贴上你完整源码,还不清楚,你用的Observable还是Flowable,或者其他。

from rxlife.

fredyzhang5532 avatar fredyzhang5532 commented on June 19, 2024

不对,请贴上你完整源码,还不清楚,你用的Observable还是Flowable,或者其他。

public class Erweimalogin {
    private int code;
    private String msg;
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
}
public interface Lejia {
    @GET("weixin/login/xcx")
    Observable<Erweimalogin> erweima_login(@Query("token") String token);
}
private boolean isgetuser = false;
 Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("https://" + G.gStrYuMing + "/") // 设置 网络请求 Url
                    .addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖)
                    .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) // 支持RxJava
                    .build();
            // 创建 网络请求接口 的实例
            Lejia request = retrofit.create(Lejia.class);
            Observable observable = request.erweima_login("test123");
            observable.delay(1, TimeUnit.SECONDS)
                    .repeatUntil(new BooleanSupplier() {
                        @Override
                        public boolean getAsBoolean() throws Exception {
                            return isgetuser;
                        }
                    })
                    .retryWhen(new RetryWithDelay(20,1000))
                    .takeUntil(new Predicate<Erweimalogin>() {
                        @Override
                        public boolean test(Erweimalogin integer) throws Exception {
                            return (integer.getCode() == 1);
                            // 返回true时,就停止发送事件
                        }
                    })
                    .subscribeOn(Schedulers.io())               // 切换到IO线程进行网络请求
                    .observeOn(AndroidSchedulers.mainThread())  // 切换回到主线程 处理请求结果
//                    .to(RxLife.toMain(this))
                    .subscribe(new Observer<Erweimalogin>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            Log.d(TAG, "dingyue");
                            disposable=d;
                        }
                        @Override
                        public void onNext(Erweimalogin result) {
                            // e.接收服务器返回的数据
                            Log.i(TAG, String.valueOf(result.getMsg()));
                        }
                        @Override
                        public void onError(Throwable e) {
                            // 获取轮询结束信息
                            Log.d(TAG, e.toString());
                        }
                        @Override
                        public void onComplete() {
                            Log.d(TAG, "完成");
                        }
                    });

from rxlife.

fredyzhang5532 avatar fredyzhang5532 commented on June 19, 2024
public class Presenter extends BaseScope {
        private boolean isgetuser = false;
        public Presenter(LifecycleOwner owner) {
            super(owner); //添加生命周期监听
        }
        public void test1() {
            //步骤1:创建Retrofit对象
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("https://" + G.gStrYuMing + "/") // 设置 网络请求 Url
                    .addConverterFactory(GsonConverterFactory.create()) //设置使用Gson解析(记得加入依赖)
                    .addCallAdapterFactory(RxJava3CallAdapterFactory.create()) // 支持RxJava
                    .build();
            // 步骤2:创建 网络请求接口 的实例
            Lejia request = retrofit.create(Lejia.class);
            Observable observable = request.erweima_login("test123");
            // 步骤3:发送网络请求进行轮询
            observable.delay(1, TimeUnit.SECONDS)
                    .repeatUntil(new BooleanSupplier() {
                        @Override
                        public boolean getAsBoolean() throws Exception {
                            return isgetuser;
                        }
                    })
                    .retryWhen(new RetryWithDelay(20,1000))
                    .takeUntil(new Predicate<Erweimalogin>() {
                        @Override
                        public boolean test(Erweimalogin integer) throws Exception {
                            return (integer.getCode() == 1);
                            // 返回true时,就停止发送事件
                        }
                    })
                    .subscribeOn(Schedulers.io())               // 切换到IO线程进行网络请求
                    .observeOn(AndroidSchedulers.mainThread())  // 切换回到主线程 处理请求结果
//                    .to(RxLife.toMain(this))
                    .subscribe(new Observer<Erweimalogin>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            Log.d(TAG, "dingyue");
                            disposable=d;
                        }
                        @Override
                        public void onNext(Erweimalogin result) {
                            // e.接收服务器返回的数据
                            Log.i(TAG, String.valueOf(result.getMsg()));
                        }
                        @Override
                        public void onError(Throwable e) {
                            // 获取轮询结束信息
                            Log.d(TAG, e.toString());
                        }
                        @Override
                        public void onComplete() {
                            Log.d(TAG, "完成");
                        }
                    });
        }
    }

from rxlife.

liujingxing avatar liujingxing commented on June 19, 2024

Observable<Erweimalogin> observable = request.erweima_login("test123"); 返回值要明确泛型

from rxlife.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.