Search in sources :

Example 6 with MaybeEmitter

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;
        }
    });
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) MaybeEmitter(io.reactivex.MaybeEmitter) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ParseException(org.apache.http.ParseException) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) NonNull(io.reactivex.annotations.NonNull) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) MaybeOnSubscribe(io.reactivex.MaybeOnSubscribe) ParseException(org.apache.http.ParseException)

Example 7 with MaybeEmitter

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;
}
Also used : MaybeEmitter(io.reactivex.MaybeEmitter) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) MaybeOnSubscribe(io.reactivex.MaybeOnSubscribe)

Example 8 with MaybeEmitter

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;
}
Also used : MaybeEmitter(io.reactivex.MaybeEmitter) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) MaybeOnSubscribe(io.reactivex.MaybeOnSubscribe)

Example 9 with MaybeEmitter

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;
        }
    });
}
Also used : Response(com.cv4j.netdiscovery.core.domain.Response) MaybeEmitter(io.reactivex.MaybeEmitter) MaybeOnSubscribe(io.reactivex.MaybeOnSubscribe)

Aggregations

MaybeEmitter (io.reactivex.MaybeEmitter)9 MaybeOnSubscribe (io.reactivex.MaybeOnSubscribe)9 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 Response (com.cv4j.netdiscovery.core.domain.Response)4 IOException (java.io.IOException)4 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 Map (java.util.Map)2 Cookie (com.cv4j.netdiscovery.core.cookies.Cookie)1 CookieGroup (com.cv4j.netdiscovery.core.cookies.CookieGroup)1 HttpRequestBody (com.cv4j.netdiscovery.core.domain.HttpRequestBody)1 Request (com.cv4j.netdiscovery.core.domain.Request)1 NonNull (io.reactivex.annotations.NonNull)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 MediaType (okhttp3.MediaType)1 RequestBody (okhttp3.RequestBody)1 HttpEntity (org.apache.http.HttpEntity)1