Java Code Examples for com.squareup.okhttp.OkHttpClient#setWriteTimeout()

The following examples show how to use com.squareup.okhttp.OkHttpClient#setWriteTimeout() . 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: HawkularMetricsClient.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor.
 * @param metricsServer
 */
public HawkularMetricsClient(URL metricsServer) {
    this.serverUrl = metricsServer;
    httpClient = new OkHttpClient();
    httpClient.setReadTimeout(DEFAULT_READ_TIMEOUT, TimeUnit.SECONDS);
    httpClient.setWriteTimeout(DEFAULT_WRITE_TIMEOUT, TimeUnit.SECONDS);
    httpClient.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT, TimeUnit.SECONDS);
    httpClient.setFollowRedirects(true);
    httpClient.setFollowSslRedirects(true);
    httpClient.setProxySelector(ProxySelector.getDefault());
    httpClient.setCookieHandler(CookieHandler.getDefault());
    httpClient.setCertificatePinner(CertificatePinner.DEFAULT);
    httpClient.setAuthenticator(AuthenticatorAdapter.INSTANCE);
    httpClient.setConnectionPool(ConnectionPool.getDefault());
    httpClient.setProtocols(Util.immutableList(Protocol.HTTP_1_1));
    httpClient.setConnectionSpecs(DEFAULT_CONNECTION_SPECS);
    httpClient.setSocketFactory(SocketFactory.getDefault());
    Internal.instance.setNetwork(httpClient, Network.DEFAULT);
}
 
Example 2
Source File: HttpConnectorFactory.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * @return a new http client
 */
private OkHttpClient createHttpClient() {
    OkHttpClient client = new OkHttpClient();
    client.setReadTimeout(connectorOptions.getReadTimeout(), TimeUnit.SECONDS);
    client.setWriteTimeout(connectorOptions.getWriteTimeout(), TimeUnit.SECONDS);
    client.setConnectTimeout(connectorOptions.getConnectTimeout(), TimeUnit.SECONDS);
    client.setFollowRedirects(connectorOptions.isFollowRedirects());
    client.setFollowSslRedirects(connectorOptions.isFollowRedirects());
    client.setProxySelector(ProxySelector.getDefault());
    client.setCookieHandler(CookieHandler.getDefault());
    client.setCertificatePinner(CertificatePinner.DEFAULT);
    client.setAuthenticator(AuthenticatorAdapter.INSTANCE);
    client.setConnectionPool(ConnectionPool.getDefault());
    client.setProtocols(Util.immutableList(Protocol.HTTP_1_1));
    client.setConnectionSpecs(DEFAULT_CONNECTION_SPECS);
    client.setSocketFactory(SocketFactory.getDefault());
    Internal.instance.setNetwork(client, Network.DEFAULT);

    return client;
}
 
Example 3
Source File: OkHttpClientFactory.java    From Auth0.Android with MIT License 6 votes vote down vote up
@VisibleForTesting
OkHttpClient modifyClient(OkHttpClient client, boolean loggingEnabled, boolean tls12Enforced, int connectTimeout, int readTimeout, int writeTimeout) {
    if (loggingEnabled) {
        enableLogging(client);
    }
    if (tls12Enforced) {
        enforceTls12(client);
    }
    if(connectTimeout > 0){
        client.setConnectTimeout(connectTimeout, TimeUnit.SECONDS);
    }
    if(readTimeout > 0){
        client.setReadTimeout(readTimeout, TimeUnit.SECONDS);
    }
    if(writeTimeout > 0){
        client.setWriteTimeout(writeTimeout, TimeUnit.SECONDS);
    }
    client.setProtocols(Arrays.asList(Protocol.HTTP_1_1, Protocol.SPDY_3));
    return client;
}
 
Example 4
Source File: WXWebSocketManager.java    From weex with Apache License 2.0 6 votes vote down vote up
public void connect(String url) {
  try {
    mHttpClient= (OkHttpClient) Class.forName("com.squareup.okhttp.OkHttpClient").newInstance();
  } catch (Exception e) {
    isSupportWebSocket =false;
    return;
  }
  mHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
  mHttpClient.setWriteTimeout(10, TimeUnit.SECONDS);
  // Disable timeouts for read
  mHttpClient.setReadTimeout(0, TimeUnit.MINUTES);

  Request request = new Request.Builder().url(url).build();
  WebSocketCall call = WebSocketCall.create(mHttpClient, request);
  call.enqueue(this);
}
 
Example 5
Source File: BuildService.java    From Gank with Apache License 2.0 5 votes vote down vote up
public static OkHttpClient defaultOkHttpClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(5 , TimeUnit.SECONDS);
    client.setReadTimeout(5    , TimeUnit.SECONDS);
    client.setWriteTimeout(10  , TimeUnit.SECONDS);
    return client;
}
 
Example 6
Source File: NightscoutUploader.java    From xDrip-Experimental with GNU General Public License v3.0 5 votes vote down vote up
public NightscoutUploader(Context context) {
    mContext = context;
    prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    client = new OkHttpClient();
    client.setConnectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
    client.setReadTimeout(SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
    enableRESTUpload = prefs.getBoolean("cloud_storage_api_enable", false);
    enableMongoUpload = prefs.getBoolean("cloud_storage_mongodb_enable", false);
}
 
Example 7
Source File: Downloader.java    From external-resources with Apache License 2.0 5 votes vote down vote up
public Downloader(@NonNull Context context, @NonNull OkHttpClient client,
    @NonNull Converter converter, @NonNull Url url, @NonNull Options options) {
  this.context = context.getApplicationContext();
  this.client = client;
  this.url = url;
  this.options = options;
  this.converter = converter;

  Cache cache = new Cache(context.getApplicationContext());

  client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  client.setWriteTimeout(WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  client.setCache(new com.squareup.okhttp.Cache(cache.getCacheDir(), cache.getCacheSize()));
}
 
Example 8
Source File: TestCaseActivity.java    From NewXmPluginSDK with Apache License 2.0 5 votes vote down vote up
public Response uploadFile(String url, String filePath) throws IOException {
    Log.d("test", "url:" + url);
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(30, TimeUnit.SECONDS);
    client.setReadTimeout(15, TimeUnit.SECONDS);
    client.setWriteTimeout(30, TimeUnit.SECONDS);
    Request request = new Request.Builder()
            .url(url)
            .put(RequestBody.create(MediaType.parse(""), new File(filePath)))
            .build();
    return client.newCall(request).execute();
}
 
Example 9
Source File: WXOkHttpDispatcher.java    From weex with Apache License 2.0 5 votes vote down vote up
private static OkHttpClient defaultOkHttpClient() {
  OkHttpClient client = new OkHttpClient();
  client.networkInterceptors().add(new OkHttpInterceptor());
  client.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  client.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  client.setWriteTimeout(DEFAULT_WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  return client;
}
 
Example 10
Source File: OkHttpRequestor.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
/**
 * Returns an {@code OkHttpClient} instance with the default settings for this SDK.
 */
public static OkHttpClient defaultOkHttpClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    // enables certificate pinning
    client.setSslSocketFactory(SSLConfig.getSSLSocketFactory());
    return client;
}
 
Example 11
Source File: ApiFactory.java    From spark-sdk-android with Apache License 2.0 5 votes vote down vote up
private static OkHttpClient buildClientWithTimeout(int timeoutInSeconds) {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(timeoutInSeconds, TimeUnit.SECONDS);
    client.setReadTimeout(timeoutInSeconds, TimeUnit.SECONDS);
    client.setWriteTimeout(timeoutInSeconds, TimeUnit.SECONDS);
    return client;
}
 
Example 12
Source File: WXOkHttpDispatcher.java    From incubator-weex-playground with Apache License 2.0 5 votes vote down vote up
private static OkHttpClient defaultOkHttpClient() {
  OkHttpClient client = new OkHttpClient();
  client.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  client.setReadTimeout(DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  client.setWriteTimeout(DEFAULT_WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
  return client;
}
 
Example 13
Source File: ApiFactory.java    From particle-android with Apache License 2.0 5 votes vote down vote up
private static OkHttpClient buildClientWithTimeout(int timeoutInSeconds) {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(timeoutInSeconds, TimeUnit.SECONDS);
    client.setReadTimeout(timeoutInSeconds, TimeUnit.SECONDS);
    client.setWriteTimeout(timeoutInSeconds, TimeUnit.SECONDS);
    return client;
}
 
Example 14
Source File: RemoteService.java    From AndroidNetwork with MIT License 5 votes vote down vote up
public static synchronized RemoteService getInstance() {
    if (service == null) {
        service = new RemoteService();
        mOkHttpClient = new OkHttpClient();

        //设置超时时间
        //参见:OkHttp3超时设置和超时异常捕获
        //http://blog.csdn.net/do168/article/details/51848895
        mOkHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
        mOkHttpClient.setWriteTimeout(10, TimeUnit.SECONDS);
        mOkHttpClient.setReadTimeout(30, TimeUnit.SECONDS);
    }
    return service;
}
 
Example 15
Source File: RemoteService.java    From AndroidNetwork with MIT License 5 votes vote down vote up
public static synchronized RemoteService getInstance() {
    if (service == null) {
        service = new RemoteService();
        mOkHttpClient = new OkHttpClient();

        //设置超时时间
        //参见:OkHttp3超时设置和超时异常捕获
        //http://blog.csdn.net/do168/article/details/51848895
        mOkHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
        mOkHttpClient.setWriteTimeout(10, TimeUnit.SECONDS);
        mOkHttpClient.setReadTimeout(30, TimeUnit.SECONDS);
    }
    return service;
}
 
Example 16
Source File: DOkHttp.java    From Pas with Apache License 2.0 5 votes vote down vote up
private DOkHttp() {
    mOkHttpClient = new OkHttpClient();
    // mOkHttpClient.networkInterceptors().add(new StethoInterceptor());

    mOkHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
    mOkHttpClient.setWriteTimeout(20, TimeUnit.SECONDS);
    mOkHttpClient.setReadTimeout(30, TimeUnit.SECONDS);

    gson=new Gson();

    //更新UI线程
    mainHanlder = new Handler(Looper.getMainLooper());

}
 
Example 17
Source File: RemoteService.java    From AndroidNetwork with MIT License 5 votes vote down vote up
public static synchronized RemoteService getInstance() {
    if (service == null) {
        service = new RemoteService();
        mOkHttpClient = new OkHttpClient();

        //设置超时时间
        //参见:OkHttp3超时设置和超时异常捕获
        //http://blog.csdn.net/do168/article/details/51848895
        mOkHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
        mOkHttpClient.setWriteTimeout(10, TimeUnit.SECONDS);
        mOkHttpClient.setReadTimeout(30, TimeUnit.SECONDS);
    }
    return service;
}
 
Example 18
Source File: OkHttpDownloader.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
private static OkHttpClient defaultOkHttpClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(Utils.DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setReadTimeout(Utils.DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(Utils.DEFAULT_WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    return client;
}
 
Example 19
Source File: OkHttpDownloader.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
private static OkHttpClient defaultOkHttpClient() {
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(Utils.DEFAULT_CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setReadTimeout(Utils.DEFAULT_READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(Utils.DEFAULT_WRITE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    return client;
}
 
Example 20
Source File: OkHttpHelper.java    From ImitateTaobaoApp with Apache License 2.0 3 votes vote down vote up
private OkHttpHelper(){

        mHttpClient = new OkHttpClient();

        mHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
        mHttpClient.setReadTimeout(10,TimeUnit.SECONDS);
        mHttpClient.setWriteTimeout(30,TimeUnit.SECONDS);

        mGson = new Gson();

        mHandler = new Handler(Looper.getMainLooper()); //主要接受子线程发送的数据, 并用此数据配合主线程
        // 更新UI

    }