use of io.reactivex.MaybeEmitter in project RxJavaInAction by fengzhizi715.
the class TestHttpClientWithPoolAndMaybe method httpGet.
/**
* GET请求
*
* @param url 请求地址
* @param timeOut 超时时间
* @return
*/
public static Maybe<String> httpGet(String url, int timeOut) {
return Maybe.create(new MaybeOnSubscribe<String>() {
@Override
public void subscribe(@NonNull MaybeEmitter<String> e) throws Exception {
e.onSuccess(url);
}
}).map(new Function<String, String>() {
@Override
public String apply(@NonNull String s) throws Exception {
// 获取客户端连接对象
CloseableHttpClient httpClient = getHttpClient(timeOut);
// 创建GET请求对象
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse response = httpClient.execute(httpGet);
String msg = null;
try {
// 执行请求
response = httpClient.execute(httpGet);
// 获取响应实体
HttpEntity entity = response.getEntity();
// 获取响应信息
msg = EntityUtils.toString(entity, "UTF-8");
} catch (ClientProtocolException e) {
System.err.println("协议错误");
e.printStackTrace();
} catch (ParseException e) {
System.err.println("解析错误");
e.printStackTrace();
} catch (IOException e) {
System.err.println("IO错误");
e.printStackTrace();
} finally {
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
response.close();
} catch (IOException e) {
System.err.println("释放链接错误");
e.printStackTrace();
}
}
}
return msg;
}
});
}
use of io.reactivex.MaybeEmitter in project kripton by xcesco.
the class BindApp0DataSource method execute.
public <T> Maybe<T> execute(final MaybeTransaction<T> transaction) {
MaybeOnSubscribe<T> emitter = new MaybeOnSubscribe<T>() {
@Override
public void subscribe(MaybeEmitter<T> emitter) {
boolean needToOpened = !BindApp0DataSource.this.isOpenInWriteMode();
boolean success = false;
@SuppressWarnings("resource") SQLiteDatabase connection = needToOpened ? openWritableDatabase() : database();
DataSourceSingleThread currentDaoFactory = _daoFactorySingleThread.bindToThread();
currentDaoFactory.onSessionOpened();
try {
connection.beginTransaction();
if (transaction != null && TransactionResult.COMMIT == transaction.onExecute(currentDaoFactory, emitter)) {
connection.setTransactionSuccessful();
success = true;
}
// no onComplete;
} catch (Throwable e) {
Logger.error(e.getMessage());
e.printStackTrace();
emitter.onError(e);
currentDaoFactory.onSessionClear();
} finally {
try {
connection.endTransaction();
} catch (Throwable e) {
}
if (needToOpened) {
close();
}
if (success) {
currentDaoFactory.onSessionClosed();
} else {
currentDaoFactory.onSessionClear();
}
}
return;
}
};
Maybe<T> result = Maybe.create(emitter);
if (globalSubscribeOn != null)
result.subscribeOn(globalSubscribeOn);
if (globalObserveOn != null)
result.observeOn(globalObserveOn);
return result;
}
use of io.reactivex.MaybeEmitter in project kripton by xcesco.
the class BindXenoDataSource method execute.
public <T> Maybe<T> execute(final MaybeTransaction<T> transaction) {
MaybeOnSubscribe<T> emitter = new MaybeOnSubscribe<T>() {
@Override
public void subscribe(MaybeEmitter<T> emitter) {
boolean needToOpened = !BindXenoDataSource.this.isOpenInWriteMode();
boolean success = false;
@SuppressWarnings("resource") SQLiteDatabase connection = needToOpened ? openWritableDatabase() : database();
DataSourceSingleThread currentDaoFactory = _daoFactorySingleThread.bindToThread();
currentDaoFactory.onSessionOpened();
try {
connection.beginTransaction();
if (transaction != null && TransactionResult.COMMIT == transaction.onExecute(currentDaoFactory, emitter)) {
connection.setTransactionSuccessful();
success = true;
}
// no onComplete;
} catch (Throwable e) {
Logger.error(e.getMessage());
e.printStackTrace();
emitter.onError(e);
currentDaoFactory.onSessionClear();
} finally {
try {
connection.endTransaction();
} catch (Throwable e) {
}
if (needToOpened) {
close();
}
if (success) {
currentDaoFactory.onSessionClosed();
} else {
currentDaoFactory.onSessionClear();
}
}
return;
}
};
Maybe<T> result = Maybe.create(emitter);
if (globalSubscribeOn != null)
result.subscribeOn(globalSubscribeOn);
if (globalObserveOn != null)
result.observeOn(globalObserveOn);
return result;
}
use of io.reactivex.MaybeEmitter in project NetDiscovery by fengzhizi715.
the class SeleniumDownloader method download.
@Override
public Maybe<Response> download(Request request) {
return Maybe.create(new MaybeOnSubscribe<String>() {
@Override
public void subscribe(MaybeEmitter emitter) throws Exception {
if (webDriver != null) {
webDriver.get(request.getUrl());
if (action != null) {
action.perform(webDriver);
}
emitter.onSuccess(webDriver.getPageSource());
}
}
}).map(new Function<String, Response>() {
@Override
public Response apply(String html) throws Exception {
Response response = new Response();
response.setContent(html.getBytes());
response.setStatusCode(Constant.OK_STATUS_CODE);
response.setContentType(getContentType(webDriver));
return response;
}
});
}
Aggregations