Example usage for com.squareup.okhttp OkHttpClient OkHttpClient

List of usage examples for com.squareup.okhttp OkHttpClient OkHttpClient

Introduction

In this page you can find the example usage for com.squareup.okhttp OkHttpClient OkHttpClient.

Prototype

public OkHttpClient() 

Source Link

Usage

From source file:org.chromium.cronet_sample_apk.CronetSampleActivity.java

License:Open Source License

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mQuicResultText = (TextView) findViewById(R.id.Quic_resultView);
    mQuicReceiveDataText = (TextView) findViewById(R.id.Quic_dataView);
    mQuicReceiveDataText.setMovementMethod(new ScrollingMovementMethod());

    mOkhttpResultText = (TextView) findViewById(R.id.OKHttp_resultView);
    mOkhttpReceiveDataText = (TextView) findViewById(R.id.OKHttp_dataView);
    mOkhttpReceiveDataText.setMovementMethod(new ScrollingMovementMethod());

    mUrlInput = (EditText) findViewById(R.id.urlText);
    mPostInput = (EditText) findViewById(R.id.postText);

    mQuicTimes = (EditText) findViewById(R.id.QuicTimes);
    mOKHttpTimes = (EditText) findViewById(R.id.OKHttpTimes);

    mQuicLoadBtn = (Button) findViewById(R.id.Quic_Load_Switch);
    mQuicBtn = (Button) findViewById(R.id.Quic_switch);
    mOkhttpLoadBtn = (Button) findViewById(R.id.OKHttp_switch);
    mOkhttpSynchronousBtn = (Button) findViewById(R.id.OKHttp_Synchronous_switch);

    CronetEngine.Builder myBuilder = new CronetEngine.Builder(this);

    //myBuilder.enableHttpCache(CronetEngine.Builder.HTTP_CACHE_IN_MEMORY, 100 * 1024);
    //myBuilder.enableHttp2(true);
    myBuilder.enableQuic(true);//from   ww  w  .ja va2  s  .c o m
    QuicEnable = true;

    mCronetEngine = myBuilder.build();
    mOKHttpClient = new OkHttpClient();

    String appUrl = (getIntent() != null ? getIntent().getDataString() : null);
    if (appUrl == null) {
        promptForURL("https://104.199.184.104:8081");
    } else {
        startWithURL(appUrl);
    }
}

From source file:org.chtijbug.drools.swimmingpool.restclient.GuvnorConnexionConfiguration.java

License:Apache License

public GuvnorRestApi getGuvnorRestApiService() {
    RequestInterceptor requestInterceptor = new RequestInterceptor() {
        @Override//from  ww w  .j  av a2  s . co  m
        public void intercept(RequestFacade request) {
            request.addHeader("Authorization", createAuthenticationHeader());
        }
    };
    // Maybe you have to correct this or use another / no Locale

    RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(this.getHostname() + this.getWebappName())
            .setRequestInterceptor(requestInterceptor).setClient(new OkClient(new OkHttpClient()))
            .setConverter(new JacksonConverter(new ObjectMapper())).build();

    return restAdapter.create(GuvnorRestApi.class);
}

From source file:org.dataconservancy.cos.osf.client.retrofit.RetrofitOsfServiceFactory.java

License:Apache License

/**
 * Constructs a new RetrofitOsfServiceFactory with the supplied JSON configuration classpath resource.
 * Default implementations of the OSF and Waterbutler configuration services, Jackson {@code ObjectMapper}, and
 * {@code OkHttpClient} will be used.  If the classpath resource is <em>not</em> absolute (beginning with a
 * '{@code /}'), then this constructor will resolve the resource under
 * {@code /org/dataconservancy/cos/osf/client/config/}.  This constructor adds the {@link AuthInterceptor} to the
 * {@code OkHttpClient} if an {@code authHeader} is found in the configuration for the OSF v2 API.  It will
 * scan the classpath under {@code org.dataconservancy.cos.osf.client.model} for classes with the {@link Type}
 * annotation, and add them to the {@link com.github.jasminb.jsonapi.ResourceConverter} used to convert JSON
 * documents to Java objects.  The {@code ResourceConverter} is also configured to resolve urls using the
 * {@code OkHttpClient}./*from   w ww  .  j  a  va 2 s.c om*/
 *
 * @param jsonConfigurationResource classpath resource containing the JSON configuration for the OSF and Waterbutler
 *                                  HTTP endpoints
 */
public RetrofitOsfServiceFactory(final String jsonConfigurationResource) {
    try {
        this.osfConfigSvc = new JacksonOsfConfigurationService(jsonConfigurationResource);
    } catch (Exception e) {
        throw new IllegalStateException(String.format(ERR_CONFIGURING_CLASS,
                JacksonOsfConfigurationService.class.getName(), e.getMessage()), e);
    }
    try {
        this.wbConfigSvc = new JacksonWbConfigurationService(jsonConfigurationResource);
    } catch (Exception e) {
        throw new IllegalStateException(String.format(ERR_CONFIGURING_CLASS,
                JacksonWbConfigurationService.class.getName(), e.getMessage()), e);
    }
    this.httpClient = new OkHttpClient();
    if (osfConfigSvc.getConfiguration().getAuthHeader() != null) {
        httpClient.interceptors().add(new AuthInterceptor(osfConfigSvc.getConfiguration().getAuthHeader()));
    }
    if (osfConfigSvc.getConfiguration().getApiVersion() != null) {
        httpClient.interceptors()
                .add(new ApiVersionInterceptor(osfConfigSvc.getConfiguration().getApiVersion()));
    }
    this.httpClient.setConnectTimeout(osfConfigSvc.getConfiguration().getConnect_timeout_ms(), MILLISECONDS);
    this.httpClient.setReadTimeout(osfConfigSvc.getConfiguration().getRead_timeout_ms(), MILLISECONDS);
    this.httpClient.setWriteTimeout(osfConfigSvc.getConfiguration().getWrite_timeout_ms(), MILLISECONDS);

    // ... the JSON-API converter used by Retrofit to map JSON documents to Java objects
    final List<Class<?>> domainClasses = new ArrayList<>();

    new FastClasspathScanner("org.dataconservancy.cos.osf.client.model")
            .matchClassesWithAnnotation(Type.class, domainClasses::add).scan();

    final ResourceConverter resourceConverter = new ResourceConverter(new ObjectMapper(),
            domainClasses.toArray(new Class[] {}));

    resourceConverter.setGlobalResolver(relUrl -> {
        final com.squareup.okhttp.Call req = httpClient.newCall(new Request.Builder().url(relUrl).build());
        try {
            return req.execute().body().bytes();
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    });

    try {
        this.jsonApiConverterFactory = new JSONAPIConverterFactory(resourceConverter);
    } catch (Exception e) {
        throw new IllegalStateException(
                String.format(ERR_CONFIGURING_CLASS, JSONAPIConverterFactory.class.getName(), e.getMessage()),
                e);
    }
}

From source file:org.dataconservancy.cos.osf.client.retrofit.RetrofitOsfServiceFactory.java

License:Apache License

/**
 * Constructs a new RetrofitOsfServiceFactory with the supplied JSON API converter factory.
 * Default implementations of the OSF and Waterbutler configuration services and {@code OkHttpClient} will be used.
 * By default this constructor will look for a classpath resource at
 * {@code /org/dataconservancy/cos/osf/client/config/osf-client.json}.  This constructor adds the
 * {@link AuthInterceptor} to the {@code OkHttpClient} if an {@code authHeader} is found in the configuration for
 * the OSF v2 API.//from  w  w  w.j av  a 2s  . c om
 *
 * @param jsonApiConverterFactory the configured Retrofit {@code retrofit.Converter.Factory} to use
 */
public RetrofitOsfServiceFactory(final JSONAPIConverterFactory jsonApiConverterFactory) {
    if (jsonApiConverterFactory == null) {
        throw new IllegalArgumentException(
                String.format(NOT_NULL_IAE, JSONAPIConverterFactory.class.getName()));
    }

    this.jsonApiConverterFactory = jsonApiConverterFactory;

    try {
        this.osfConfigSvc = new JacksonOsfConfigurationService(DEFAULT_CONFIGURATION_RESOURCE);
    } catch (Exception e) {
        throw new IllegalStateException(String.format(ERR_CONFIGURING_CLASS,
                JacksonOsfConfigurationService.class.getName(), e.getMessage()), e);
    }
    try {
        this.wbConfigSvc = new JacksonWbConfigurationService(DEFAULT_CONFIGURATION_RESOURCE);
    } catch (Exception e) {
        throw new IllegalStateException(String.format(ERR_CONFIGURING_CLASS,
                JacksonWbConfigurationService.class.getName(), e.getMessage()), e);
    }
    this.httpClient = new OkHttpClient();
    if (osfConfigSvc.getConfiguration().getAuthHeader() != null) {
        httpClient.interceptors().add(new AuthInterceptor(osfConfigSvc.getConfiguration().getAuthHeader()));
    }
    if (osfConfigSvc.getConfiguration().getApiVersion() != null) {
        httpClient.interceptors()
                .add(new ApiVersionInterceptor(osfConfigSvc.getConfiguration().getApiVersion()));
    }

    this.httpClient.setConnectTimeout(osfConfigSvc.getConfiguration().getConnect_timeout_ms(), MILLISECONDS);
    this.httpClient.setReadTimeout(osfConfigSvc.getConfiguration().getRead_timeout_ms(), MILLISECONDS);
    this.httpClient.setWriteTimeout(osfConfigSvc.getConfiguration().getWrite_timeout_ms(), MILLISECONDS);
}

From source file:org.dataconservancy.cos.osf.client.retrofit.RetrofitOsfServiceFactory.java

License:Apache License

/**
 * Constructs a new RetrofitOsfServiceFactory with the supplied JSON API converter factory and configuration
 * resource. Default implementations of the OSF and Waterbutler configuration services and {@code OkHttpClient} will
 * be used. If the classpath resource is <em>not</em> absolute (beginning with a '{@code /}'), then this constructor
 * will resolve the resource under {@code /org/dataconservancy/cos/osf/client/config/}.  This constructor adds the
 * {@link AuthInterceptor} to the {@code OkHttpClient} if an {@code authHeader} is found in the configuration for
 * the OSF v2 API./*from w  w  w .  ja va  2s  . co m*/
 *
 * @param jsonConfigurationResource classpath resource containing the JSON configuration for the OSF and Waterbutler
 *                                  HTTP endpoints
 * @param jsonApiConverterFactory   the configured Retrofit {@code retrofit.Converter.Factory} to use
 */
public RetrofitOsfServiceFactory(final String jsonConfigurationResource,
        final JSONAPIConverterFactory jsonApiConverterFactory) {
    if (jsonApiConverterFactory == null) {
        throw new IllegalArgumentException(
                String.format(NOT_NULL_IAE, JSONAPIConverterFactory.class.getName()));
    }

    this.jsonApiConverterFactory = jsonApiConverterFactory;

    try {
        this.osfConfigSvc = new JacksonOsfConfigurationService(jsonConfigurationResource);
    } catch (Exception e) {
        throw new IllegalStateException(String.format(ERR_CONFIGURING_CLASS,
                JacksonOsfConfigurationService.class.getName(), e.getMessage()), e);
    }
    try {
        this.wbConfigSvc = new JacksonWbConfigurationService(jsonConfigurationResource);
    } catch (Exception e) {
        throw new IllegalStateException(String.format(ERR_CONFIGURING_CLASS,
                JacksonWbConfigurationService.class.getName(), e.getMessage()), e);
    }
    this.httpClient = new OkHttpClient();
    if (osfConfigSvc.getConfiguration().getAuthHeader() != null) {
        httpClient.interceptors().add(new AuthInterceptor(osfConfigSvc.getConfiguration().getAuthHeader()));
    }
    if (osfConfigSvc.getConfiguration().getApiVersion() != null) {
        httpClient.interceptors()
                .add(new ApiVersionInterceptor(osfConfigSvc.getConfiguration().getApiVersion()));
    }
    this.httpClient.setConnectTimeout(osfConfigSvc.getConfiguration().getConnect_timeout_ms(), MILLISECONDS);
    this.httpClient.setReadTimeout(osfConfigSvc.getConfiguration().getRead_timeout_ms(), MILLISECONDS);
    this.httpClient.setWriteTimeout(osfConfigSvc.getConfiguration().getWrite_timeout_ms(), MILLISECONDS);
}

From source file:org.dataconservancy.cos.osf.client.retrofit.TestingOsfServiceFactory.java

License:Apache License

/**
 * Creates a new RetrofitOsfServiceFactory with default implementations for the required collaborators.  Not
 * recommended for production./*www .  ja  va 2s.  c  o  m*/
 *
 * @param jsonConfigurationResource a classpath resource containing the configuration for the OSF API; must be JSON
 */
public TestingOsfServiceFactory(final String jsonConfigurationResource) {
    // Configure the configuration service.
    osfConfigurationService = new JacksonOsfConfigurationService(jsonConfigurationResource);
    wbConfigurationService = new JacksonWbConfigurationService(jsonConfigurationResource);

    // Wiring for the RetrofitOsfService Factory

    // ... the OK HTTP client used by Retrofit to make calls
    httpClient = new OkHttpClient();
    httpClient.interceptors()
            .add(new AuthInterceptor(osfConfigurationService.getConfiguration().getAuthHeader()));

    // ... the JSON-API converter used by Retrofit to map JSON documents to Java objects
    final List<Class<?>> domainClasses = new ArrayList<>();

    new FastClasspathScanner("org.dataconservancy.cos.osf.client.model")
            .matchClassesWithAnnotation(Type.class, domainClasses::add).scan();

    final ResourceConverter resourceConverter = new ResourceConverter(new ObjectMapper(),
            domainClasses.toArray(new Class[] {}));

    resourceConverter.setGlobalResolver(relUrl -> {
        final com.squareup.okhttp.Call req = httpClient.newCall(new Request.Builder().url(relUrl).build());
        try {
            return req.execute().body().bytes();
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    });

    final JSONAPIConverterFactory jsonApiConverterFactory = new PaginatedConverterFactory(httpClient,
            resourceConverter);

    factory = new RetrofitOsfServiceFactory(osfConfigurationService, wbConfigurationService, httpClient,
            jsonApiConverterFactory);
}

From source file:org.dataconservancy.cos.osf.client.service.RetrofitOsfServiceFactory.java

License:Apache License

/**
 * Constructs a new RetrofitOsfServiceFactory with the supplied JSON configuration classpath resource.
 * Default implementations of the OSF and Waterbutler configuration services, Jackson {@code ObjectMapper}, and
 * {@code OkHttpClient} will be used.  If the classpath resource is <em>not</em> absolute (beginning with a
 * '{@code /}'), then this constructor will resolve the resource under
 * {@code /org/dataconservancy/cos/osf/client/config/}.  This constructor adds the {@link AuthInterceptor} to the
 * {@code OkHttpClient} if an {@code authHeader} is found in the configuration for the OSF v2 API.  It will
 * scan the classpath under {@code org.dataconservancy.cos.osf.client.model} for classes with the {@link Type}
 * annotation, and add them to the {@link com.github.jasminb.jsonapi.ResourceConverter} used to convert JSON
 * documents to Java objects.  The {@code ResourceConverter} is also configured to resolve urls using the
 * {@code OkHttpClient}./*from   ww  w.  j  a  va2  s.  c o  m*/
 *
 * @param jsonConfigurationResource classpath resource containing the JSON configuration for the OSF and Waterbutler
 *                                  HTTP endpoints
 */
public RetrofitOsfServiceFactory(String jsonConfigurationResource) {
    try {
        this.osfConfigSvc = new JacksonOsfConfigurationService(jsonConfigurationResource);
    } catch (Exception e) {
        throw new IllegalStateException(String.format(ERR_CONFIGURING_CLASS,
                JacksonOsfConfigurationService.class.getName(), e.getMessage()), e);
    }
    try {
        this.wbConfigSvc = new JacksonWbConfigurationService(jsonConfigurationResource);
    } catch (Exception e) {
        throw new IllegalStateException(String.format(ERR_CONFIGURING_CLASS,
                JacksonWbConfigurationService.class.getName(), e.getMessage()), e);
    }
    this.httpClient = new OkHttpClient();
    if (osfConfigSvc.getConfiguration().getAuthHeader() != null) {
        httpClient.interceptors().add(new AuthInterceptor(osfConfigSvc.getConfiguration().getAuthHeader()));
    }

    // ... the JSON-API converter used by Retrofit to map JSON documents to Java objects
    List<Class<?>> domainClasses = new ArrayList<>();

    new FastClasspathScanner("org.dataconservancy.cos.osf.client.model")
            .matchClassesWithAnnotation(Type.class, domainClasses::add).scan();

    ResourceConverter resourceConverter = new ResourceConverter(new ObjectMapper(),
            domainClasses.toArray(new Class[] {}));

    resourceConverter.setGlobalResolver(relUrl -> {
        com.squareup.okhttp.Call req = httpClient.newCall(new Request.Builder().url(relUrl).build());
        try {
            return req.execute().body().bytes();
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    });

    try {
        this.jsonApiConverterFactory = new JSONAPIConverterFactory(resourceConverter);
    } catch (Exception e) {
        throw new IllegalStateException(
                String.format(ERR_CONFIGURING_CLASS, JSONAPIConverterFactory.class.getName(), e.getMessage()),
                e);
    }
}

From source file:org.dataconservancy.cos.osf.client.service.RetrofitOsfServiceFactory.java

License:Apache License

/**
 * Constructs a new RetrofitOsfServiceFactory with the supplied JSON API converter factory.
 * Default implementations of the OSF and Waterbutler configuration services and {@code OkHttpClient} will be used.
 * By default this constructor will look for a classpath resource at
 * {@code /org/dataconservancy/cos/osf/client/config/osf-client.json}.  This constructor adds the
 * {@link AuthInterceptor} to the {@code OkHttpClient} if an {@code authHeader} is found in the configuration for
 * the OSF v2 API.//from ww w  .  j  a  va2s.c  o m
 *
 * @param jsonApiConverterFactory the configured Retrofit {@code retrofit.Converter.Factory} to use
 */
public RetrofitOsfServiceFactory(JSONAPIConverterFactory jsonApiConverterFactory) {
    if (jsonApiConverterFactory == null) {
        throw new IllegalArgumentException(
                String.format(NOT_NULL_IAE, JSONAPIConverterFactory.class.getName()));
    }

    this.jsonApiConverterFactory = jsonApiConverterFactory;

    try {
        this.osfConfigSvc = new JacksonOsfConfigurationService(DEFAULT_CONFIGURATION_RESOURCE);
    } catch (Exception e) {
        throw new IllegalStateException(String.format(ERR_CONFIGURING_CLASS,
                JacksonOsfConfigurationService.class.getName(), e.getMessage()), e);
    }
    try {
        this.wbConfigSvc = new JacksonWbConfigurationService(DEFAULT_CONFIGURATION_RESOURCE);
    } catch (Exception e) {
        throw new IllegalStateException(String.format(ERR_CONFIGURING_CLASS,
                JacksonWbConfigurationService.class.getName(), e.getMessage()), e);
    }
    this.httpClient = new OkHttpClient();
    if (osfConfigSvc.getConfiguration().getAuthHeader() != null) {
        httpClient.interceptors().add(new AuthInterceptor(osfConfigSvc.getConfiguration().getAuthHeader()));
    }
}

From source file:org.dataconservancy.cos.osf.client.service.RetrofitOsfServiceFactory.java

License:Apache License

/**
 * Constructs a new RetrofitOsfServiceFactory with the supplied JSON API converter factory and configuration
 * resource. Default implementations of the OSF and Waterbutler configuration services and {@code OkHttpClient} will
 * be used. If the classpath resource is <em>not</em> absolute (beginning with a '{@code /}'), then this constructor
 * will resolve the resource under {@code /org/dataconservancy/cos/osf/client/config/}.  This constructor adds the
 * {@link AuthInterceptor} to the {@code OkHttpClient} if an {@code authHeader} is found in the configuration for
 * the OSF v2 API./* w  w  w . j a v  a 2s .c  om*/
 *
 * @param jsonConfigurationResource classpath resource containing the JSON configuration for the OSF and Waterbutler
 *                                  HTTP endpoints
 * @param jsonApiConverterFactory   the configured Retrofit {@code retrofit.Converter.Factory} to use
 */
public RetrofitOsfServiceFactory(String jsonConfigurationResource,
        JSONAPIConverterFactory jsonApiConverterFactory) {
    if (jsonApiConverterFactory == null) {
        throw new IllegalArgumentException(
                String.format(NOT_NULL_IAE, JSONAPIConverterFactory.class.getName()));
    }

    this.jsonApiConverterFactory = jsonApiConverterFactory;

    try {
        this.osfConfigSvc = new JacksonOsfConfigurationService(jsonConfigurationResource);
    } catch (Exception e) {
        throw new IllegalStateException(String.format(ERR_CONFIGURING_CLASS,
                JacksonOsfConfigurationService.class.getName(), e.getMessage()), e);
    }
    try {
        this.wbConfigSvc = new JacksonWbConfigurationService(jsonConfigurationResource);
    } catch (Exception e) {
        throw new IllegalStateException(String.format(ERR_CONFIGURING_CLASS,
                JacksonWbConfigurationService.class.getName(), e.getMessage()), e);
    }
    this.httpClient = new OkHttpClient();
    if (osfConfigSvc.getConfiguration().getAuthHeader() != null) {
        httpClient.interceptors().add(new AuthInterceptor(osfConfigSvc.getConfiguration().getAuthHeader()));
    }
}