Search in sources :

Example 1 with ApiException

use of com.androidwind.androidquick.module.exception.ApiException in project AndroidQuick by ddnosh.

the class Network2Fragment method initViewsAndEvents.

@Override
protected void initViewsAndEvents(Bundle savedInstanceState) {
    showLoadingDialog("加载中...");
    mTestBeanList = new ArrayList<>();
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mRecyclerView.setHasFixedSize(true);
    mCommonAdapter = new CommonAdapter<TestBean>(getActivity(), R.layout.item_common_adapter_2, mTestBeanList) {

        @Override
        public void convert(CommonViewHolder holder, final TestBean bean) {
            holder.setText(R.id.tv_login, bean.getLogin());
            holder.setText(R.id.tv_id, bean.getId() + "");
            holder.setImageResourceWithGlide(R.id.iv_avatar, bean.getAvatar_url());
            holder.setOnClickListener(R.id.ll_rv_common_adapter_item, new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    LogUtil.d(TAG, "onItemClick");
                    ToastUtil.showToast(bean.getLogin() + " clicked!");
                    bean.setLogin(bean.getLogin() + " clicked!");
                    notifyDataSetChanged();
                }
            });
        }
    };
    mRecyclerView.setAdapter(mCommonAdapter);
    // mCompositeSubscription = new CompositeSubscription();
    // Subscription subscription =
    RetrofitManager.INSTANCE.getRetrofit(Constants.GANK_API_URL).create(TestApis.class).getOctocat().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<List<TestBean>>() {

        @Override
        public void onError(ApiException exception) {
            LogUtil.e(TAG, "error:" + exception.getMessage());
        }

        @Override
        public void onSuccess(List<TestBean> testBeans) {
            dismissLoadingDialog();
            LogUtil.i(TAG, testBeans.toString());
            // 不能这样赋值:mTestBeanList = list;
            // 方法一
            // mTestBeanList.clear();
            // mTestBeanList.addAll(list);
            // mCommonAdapter.notifyDataSetChanged();
            // 方法二
            mCommonAdapter.update(testBeans);
        }
    });
    // mCompositeSubscription.add(subscription);
    RetrofitManager.INSTANCE.getRetrofit(Constants.GANK_API_URL).create(TestApis.class).getTestData().subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<List<NameBean>>() {

        @Override
        public void onError(ApiException exception) {
            LogUtil.e(TAG, "error:" + exception.getMessage());
        }

        @Override
        public void onSuccess(List<NameBean> testBeans) {
            dismissLoadingDialog();
            LogUtil.i(TAG, testBeans.toString());
            ToastUtil.showToast(testBeans.toString());
        }
    });
}
Also used : CommonViewHolder(com.androidwind.androidquick.ui.adapter.CommonViewHolder) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) BindView(butterknife.BindView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) TestBean(com.androidwind.androidquick.demo.bean.TestBean) ArrayList(java.util.ArrayList) List(java.util.List) NameBean(com.androidwind.androidquick.demo.bean.NameBean) ApiException(com.androidwind.androidquick.module.exception.ApiException)

Example 2 with ApiException

use of com.androidwind.androidquick.module.exception.ApiException in project AndroidQuick by ddnosh.

the class Network3Fragment method clickSyncFlatMap.

public void clickSyncFlatMap() {
    TestApis testApis = RetrofitManager.INSTANCE.getRetrofit(Constants.GANK_API_URL).create(TestApis.class);
    // 先访问唐诗宋词接口
    testApis.getTangShiSongCi().subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).flatMap(new Function<TSSCRes<TSSCResult>, ObservableSource<XHYRes<XHYResult>>>() {

        @Override
        public ObservableSource<XHYRes<XHYResult>> apply(TSSCRes<TSSCResult> listTSSCRes) throws Exception {
            if (listTSSCRes != null && listTSSCRes.getResult() != null) {
                // 再访问歇后语接口
                return testApis.getXHY();
            } else {
                return null;
            }
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new BaseObserver<XHYRes<XHYResult>>() {

        @Override
        public void onError(ApiException exception) {
            LogUtil.e(TAG, "error:" + exception.getMessage());
        }

        @Override
        public void onSuccess(XHYRes<XHYResult> testBeans) {
            LogUtil.i(TAG, testBeans.toString());
        }
    });
}
Also used : BiFunction(io.reactivex.functions.BiFunction) Function(io.reactivex.functions.Function) TestApis(com.androidwind.androidquick.demo.features.module.network.retrofit.TestApis) ApiException(com.androidwind.androidquick.module.exception.ApiException)

Example 3 with ApiException

use of com.androidwind.androidquick.module.exception.ApiException in project AndroidQuick by ddnosh.

the class Network3Fragment method clickAsyncMerge.

private void clickAsyncMerge() {
    TestApis testApis = RetrofitManager.INSTANCE.getRetrofit(Constants.GANK_API_URL).create(TestApis.class);
    Observable<TSSCRes<TSSCResult>> o1 = testApis.getTangShiSongCi();
    Observable<XHYRes<XHYResult>> o2 = testApis.getXHY();
    Observable.merge(o1, o2).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).subscribe(new BaseObserver<Object>() {

        @Override
        public void onError(ApiException exception) {
            LogUtil.e(TAG, "error:" + exception.getMessage());
        }

        @Override
        public void onSuccess(Object object) {
            if (object instanceof TSSCRes) {
                LogUtil.i(TAG, object.toString());
            } else if (object instanceof XHYRes) {
                LogUtil.i(TAG, object.toString());
            }
        }
    });
}
Also used : TestApis(com.androidwind.androidquick.demo.features.module.network.retrofit.TestApis) ApiException(com.androidwind.androidquick.module.exception.ApiException)

Example 4 with ApiException

use of com.androidwind.androidquick.module.exception.ApiException in project AndroidQuick by ddnosh.

the class Network3Fragment method clickAsyncZip.

private void clickAsyncZip() {
    TestApis testApis = RetrofitManager.INSTANCE.getRetrofit(Constants.GANK_API_URL).create(TestApis.class);
    Observable<TSSCRes<TSSCResult>> o1 = testApis.getTangShiSongCi();
    Observable<XHYRes<XHYResult>> o2 = testApis.getXHY();
    Observable.zip(o1, o2, new BiFunction<TSSCRes<TSSCResult>, XHYRes<XHYResult>, Integer>() {

        @Override
        public Integer apply(TSSCRes<TSSCResult> tsscRes, XHYRes<XHYResult> xhyRes) throws Exception {
            return tsscRes.getResult().size() + xhyRes.getResult().size();
        }
    }).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).subscribe(new BaseObserver<Integer>() {

        @Override
        public void onError(ApiException exception) {
            LogUtil.e(TAG, "error:" + exception.getMessage());
        }

        @Override
        public void onSuccess(Integer count) {
            LogUtil.e(TAG, "count = " + count);
        }
    });
}
Also used : BiFunction(io.reactivex.functions.BiFunction) TestApis(com.androidwind.androidquick.demo.features.module.network.retrofit.TestApis) ApiException(com.androidwind.androidquick.module.exception.ApiException)

Example 5 with ApiException

use of com.androidwind.androidquick.module.exception.ApiException in project AndroidQuick by ddnosh.

the class MainActivity method getSdkVersion.

private void getSdkVersion() {
    Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com").client(new OkHttpClient.Builder().build()).addConverterFactory(// 添加 string 转换器
    ScalarsConverterFactory.create()).addCallAdapterFactory(// 添加 RxJava 适配器
    RxJava2CallAdapterFactory.create()).build();
    retrofit.create(TestApis.class).getSdkVersion().compose(RxUtil.<String>applySchedulers()).compose(lifecycleProvider.bindUntilEvent(Lifecycle.Event.ON_DESTROY)).subscribe(new BaseObserver<String>() {

        @Override
        public void onError(ApiException exception) {
        }

        @Override
        public void onSuccess(String html) {
            if (!StringUtil.isEmpty(html)) {
                if (html.contains("</text></g> </svg>")) {
                    String[] sArray = html.split("</text></g> </svg>");
                    int position = sArray[0].lastIndexOf(">");
                    if (position > 0) {
                        String sdk = sArray[0].substring(position + 1);
                        String sdkVersion = getResources().getString(R.string.sdk_version);
                        String version = String.format(sdkVersion, sdk);
                        tvSdkVersion.setText(version);
                    }
                }
            }
        }
    });
}
Also used : Retrofit(retrofit2.Retrofit) OkHttpClient(okhttp3.OkHttpClient) ApiException(com.androidwind.androidquick.module.exception.ApiException)

Aggregations

ApiException (com.androidwind.androidquick.module.exception.ApiException)8 TestApis (com.androidwind.androidquick.demo.features.module.network.retrofit.TestApis)4 BiFunction (io.reactivex.functions.BiFunction)3 Function (io.reactivex.functions.Function)2 List (java.util.List)2 View (android.view.View)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 BindView (butterknife.BindView)1 NameBean (com.androidwind.androidquick.demo.bean.NameBean)1 TestBean (com.androidwind.androidquick.demo.bean.TestBean)1 Gank2Apis (com.androidwind.androidquick.demo.features.module.network.retrofit.Gank2Apis)1 GankRes (com.androidwind.androidquick.demo.features.module.network.retrofit.GankRes)1 CommonViewHolder (com.androidwind.androidquick.ui.adapter.CommonViewHolder)1 Observable (io.reactivex.Observable)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 OkHttpClient (okhttp3.OkHttpClient)1 Retrofit (retrofit2.Retrofit)1