Java Code Examples for okhttp3.OkHttpClient.Builder#readTimeout()
The following examples show how to use
okhttp3.OkHttpClient.Builder#readTimeout() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OKHttpFactory.java From flower with Apache License 2.0 | 6 votes |
private void initOKHttp() { Builder builder = new OkHttpClient().newBuilder(); builder.connectTimeout(httpConfig.getConnectTimeout(), TimeUnit.MILLISECONDS); builder.writeTimeout(httpConfig.getWriteTimeout(), TimeUnit.MILLISECONDS); builder.readTimeout(httpConfig.getReadTimeout(), TimeUnit.MILLISECONDS); ExecutorService executorService = new ThreadPoolExecutor(8, Runtime.getRuntime().availableProcessors() * 8, 60, TimeUnit.SECONDS, new SynchronousQueue<>(), new NamedThreadFactory("flower-http")); Dispatcher dispatcher = new Dispatcher(executorService); dispatcher.setMaxRequests(256); dispatcher.setMaxRequestsPerHost(Math.max(32, Runtime.getRuntime().availableProcessors() * 8)); builder.dispatcher(dispatcher); builder.retryOnConnectionFailure(true); builder.connectionPool(new ConnectionPool()); this.template = builder.build(); }
Example 2
Source File: OkHttpAdapter.java From katharsis-framework with Apache License 2.0 | 6 votes |
private void initImpl() { synchronized (this) { if (impl == null) { Builder builder = new OkHttpClient.Builder(); if (networkTimeout != null) { builder.readTimeout(networkTimeout, TimeUnit.MILLISECONDS); } for (OkHttpAdapterListener listener : listeners) { listener.onBuild(builder); } impl = builder.build(); } } }
Example 3
Source File: OkHttpAdapter.java From crnk-framework with Apache License 2.0 | 5 votes |
private synchronized void initImpl() { if (impl == null) { Builder builder = new OkHttpClient.Builder(); if (networkTimeout != null) { builder.readTimeout(networkTimeout, TimeUnit.MILLISECONDS); } for (OkHttpAdapterListener listener : nativeListeners) { listener.onBuild(builder); } impl = builder.build(); } }
Example 4
Source File: HttpConnection.java From PocketMaps with MIT License | 5 votes |
private static OkHttpClient getOkHttpClient() { if (client == null) { Builder b = new Builder(); b.connectTimeout(TIMEOUT_CONNECTION, TimeUnit.MILLISECONDS); b.readTimeout(TIMEOUT_SOCKET, TimeUnit.MILLISECONDS); client = b.build(); /* client.setConnectTimeout(TIMEOUT_CONNECTION, TimeUnit.MILLISECONDS); client.setReadTimeout(TIMEOUT_SOCKET, TimeUnit.MILLISECONDS); */ } return client; }
Example 5
Source File: CurlThread.java From fiery with Apache License 2.0 | 4 votes |
private String postHttp(String url, String postData) { String result = ""; Builder builder = new Builder(); builder.connectTimeout(10000, TimeUnit.MILLISECONDS); builder.readTimeout(10000, TimeUnit.MILLISECONDS); builder.writeTimeout(10000, TimeUnit.MILLISECONDS); builder.followRedirects(true); builder.retryOnConnectionFailure(false); OkHttpClient client = builder.build(); //MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"); RequestBody body = new FormBody.Builder().add("contents", postData).build(); //RequestBody.create(mediaType, "contents=" + postData); Request request = new Request.Builder() .url(url) .post(body) .build(); try { Response response = client.newCall(request).execute(); result = response.body().string(); } catch (Exception e) { e.printStackTrace(); } return result; /* String result = ""; if (postData.trim().length() == 0) { return ""; } HttpPost httppost = new HttpPost(url); CloseableHttpClient httpClient = HttpClients.createDefault(); //header httppost.addHeader("connection", "close"); httppost.addHeader("user-agent", "Ragnar Fiery LogPusher"); httppost.addHeader("Content-Type", "application/x-www-form-urlencoded"); //Configure RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(10000) .setConnectTimeout(10000) .setConnectionRequestTimeout(20000) .setContentCompressionEnabled(true) .setExpectContinueEnabled(true) .setMaxRedirects(3) .setRedirectsEnabled(true) .build(); httppost.setConfig(requestConfig); //set parameter List<NameValuePair> formparams = new ArrayList<>(); formparams.add(new BasicNameValuePair("contents", postData)); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8); httppost.setEntity(entity); try { CloseableHttpResponse response = httpClient.execute(httppost); result = EntityUtils.toString(response.getEntity()); } catch (Exception e) { e.printStackTrace(); log.error("http post error:" + e.getMessage()); } httppost.releaseConnection(); return result; */ }