美文网首页
Retrofit基础上添加反射方法

Retrofit基础上添加反射方法

作者: Android刘东 | 来源:发表于2020-06-19 10:52 被阅读0次

1.方法

     /***
     *  @param methodName 方法名
     * @param args 方法参数值
     */
    public <T> Disposable getHttp(String methodName, String[] args, final AHttpBack mHttpBack) {
        try {
            Class<?>[] nClass = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                nClass[i] = String.class;
            }
            Object object = HttpMethods.getInstance().getApiService().getClass()
                    .getMethod(methodName, nClass)
                    .invoke(HttpMethods.getInstance().getApiService(), args);
            if (object instanceof Observable) {
                Observable<T> mObservable = (Observable<T>) object;
                return mObservable.compose(schedulersTransformer()).subscribe(new Consumer<T>() {
                    @Override
                    public void accept(T t) throws Exception {
                        mHttpBack.onSuccess(t);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        mHttpBack.onExceptions(throwable);
                    }
                });

            }
            return null;
        } catch (Exception e) {
            mHttpBack.onExceptions(e);
            return null;
        }

    }

    public  ObservableTransformer schedulersTransformer() {
        return new ObservableTransformer() {
            @Override
            public ObservableSource apply(Observable upstream) {
                return upstream.subscribeOn(Schedulers.io())
                        .unsubscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

相关文章

网友评论

      本文标题:Retrofit基础上添加反射方法

      本文链接:https://www.haomeiwen.com/subject/hvhvxktx.html