All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.wandersnail.common.http.GeneralRequestTask Maven / Gradle / Ivy

There is a newer version: 2.2.1
Show newest version
package cn.wandersnail.common.http;


import java.util.concurrent.TimeoutException;

import cn.wandersnail.common.http.callback.RequestCallback;
import cn.wandersnail.common.http.util.SchedulerUtils;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;
import retrofit2.Converter;
import retrofit2.Response;

/**
 * 一般的请求任务
 * 

* date: 2019/8/23 21:19 * author: zengfansheng */ class GeneralRequestTask { private Disposable disposable; GeneralRequestTask(Observable> observable, Converter converter, Configuration configuration, RequestCallback callback) { //只有设置过超时才计 if (configuration.callTimeout > 0) { EasyHttp.executorService.execute(() -> { try { Thread.sleep(configuration.callTimeout); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (GeneralRequestTask.this) { if (disposable != null && !disposable.isDisposed()) { disposable.dispose(); } disposable = null; if (callback != null) { callback.onError(new TimeoutException("Http request timeout!")); } } }); } disposable = observable.compose(SchedulerUtils.applyGeneralObservableSchedulers()) .subscribe(response -> { disposable = null; if (callback != null) { try { callback.onSuccess(response.raw(), converter.convert(response.body())); } catch (Throwable t) { callback.onError(t); } } }, throwable -> { disposable = null; if (callback != null) { callback.onError(throwable); } }, () -> disposable = null); } Disposable getDisposable() { return disposable; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy