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:com.massivcode.weatherinfoexam.utils.NetworkUtil.java

License:Apache License

/**
 * URL  ??  //from  w  w  w . ja  v  a  2 s  .  com
 * @param urlString
 * @return
 * @throws IOException
 */
public static String getDataFromUrl(String urlString) throws IOException {
    OkHttpClient client = new OkHttpClient();

    Request request = new Request.Builder().url(urlString).build();

    Response response = client.newCall(request).execute();
    return response.body().string();
}

From source file:com.maxleapmobile.gitmaster.api.ApiManager.java

License:Open Source License

private ApiManager() {
    mContext = GithubApplication.getInstance();

    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
    okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);
    okHttpClient.interceptors().add(new Interceptor() {
        @Override//from w w  w .ja  v a 2 s  .co m
        public Response intercept(Chain chain) throws IOException {
            mAccessToken = PreferenceUtil.getString(mContext, Const.ACCESS_TOKEN_KEY, null);
            HttpUrl url;
            if (mAccessToken != null) {
                url = chain.request().httpUrl().newBuilder().addQueryParameter("access_token", mAccessToken)
                        .build();
            } else {
                url = chain.request().httpUrl();
            }

            Request request = chain.request();
            Request newRequest;
            newRequest = request.newBuilder().url(url).addHeader("User-Agent", USER_AGENT)
                    .addHeader("Accept", ACCEPT).build();
            return chain.proceed(newRequest);
        }
    });
    mRetrofit = new Retrofit.Builder().baseUrl(API_URL).client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();

    mGithubApi = mRetrofit.create(GithubApi.class);
}

From source file:com.mebigfatguy.githublistener.EventPoller.java

License:Apache License

public EventPoller(ArrayBlockingQueue<GHEventInfo> queue, String userName, String authToken)
        throws IOException {
    eventQueue = queue;//  www .j  av  a  2s . c  o  m

    github = GitHub.connect(userName, authToken);
    github.setConnector(new OkHttpConnector(new OkHttpClient()));
}

From source file:com.meyersj.tamalenow.location.UpdateLocationService.java

License:Open Source License

@Override
public void onCreate() {
    super.onCreate();

    app = (TamaleApplication) getApplication();

    httpClient = new OkHttpClient();
    listener = new TamaleLocationListener(getApplicationContext(), app.getGPSInterval()) {
        @Override/*from   ww  w  . ja  v a  2 s  .c om*/
        public void onLocationChanged(Location location) {
            postLocation(location);
        }
    };
    listener.start();
}

From source file:com.microsoft.azure.AzureServiceClient.java

License:Open Source License

/**
 * Initializes a new instance of the ServiceClient class.
 *//*w w  w.  j  a va  2 s  . c o  m*/
protected AzureServiceClient() {
    this(new OkHttpClient(), new Retrofit.Builder());

    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    this.client.setCookieHandler(cookieManager);

    Executor executor = Executors.newCachedThreadPool();
    this.mapperAdapter = new AzureJacksonMapperAdapter();
    this.retrofitBuilder.addConverterFactory(mapperAdapter.getConverterFactory()).callbackExecutor(executor);
}

From source file:com.microsoft.rest.AzureServiceClient.java

License:Open Source License

/**
 * Initializes a new instance of the ServiceClient class.
 *//*from   w w  w.  j ava  2s.c  o  m*/
protected AzureServiceClient() {
    this(new OkHttpClient(), new Retrofit.Builder());

    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    this.client.setCookieHandler(cookieManager);

    Executor executor = Executors.newCachedThreadPool();
    this.retrofitBuilder.addConverterFactory(new AzureJacksonUtils().getConverterFactory())
            .callbackExecutor(executor);
}

From source file:com.microsoft.rest.ServiceClient.java

License:Open Source License

/**
 * Initializes a new instance of the ServiceClient class.
 *//*from w w w . j a  va 2  s  .com*/
protected ServiceClient() {
    this(new OkHttpClient(), new Retrofit.Builder());

    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    this.client.setCookieHandler(cookieManager);

    Executor executor = Executors.newCachedThreadPool();
    this.mapperAdapter = new JacksonMapperAdapter();
    this.retrofitBuilder.addConverterFactory(mapperAdapter.getConverterFactory()).callbackExecutor(executor);
}

From source file:com.microsoft.windowsazure.mobileservices.http.OkHttpClientFactoryImpl.java

License:Open Source License

@Override
public OkHttpClient createOkHttpClient() {

    OkHttpClient okClient = new OkHttpClient();
    //okClient.networkInterceptors().add(new UserAgentInterceptor(MobileServiceConnection.getUserAgent()));

    return okClient;
}

From source file:com.microsoft.windowsazure.mobileservices.zumoe2etestapp.MainActivity.java

License:Open Source License

private boolean IsNetBackend() {
    try {/*from w ww . j av  a  2s  .  com*/
        OkHttpClient httpclient = new OkHttpClient();

        Request request = new Request.Builder().url(getMobileServiceURL() + "api/runtimeinfo")
                .addHeader("ZUMO-API-VERSION", "2.0.0").build();

        Response response = httpclient.newCall(request).execute();

        String runtimeType;

        if (response.code() == 200) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            String responseString = response.body().string();

            JsonObject jsonResult = new JsonParser().parse(responseString).getAsJsonObject();

            runtimeType = jsonResult.get("runtime").getAsJsonObject().get("type").getAsString();

            out.close();
        } else {
            response.body().close();
            throw new IOException(String.valueOf(response.code()));
        }

        if (runtimeType.equals(".NET")) {
            return true;
        }

        return false;
    } catch (Exception ex) {
        return false;
    }
}

From source file:com.mifos.services.API.java

private OkHttpClient getUnsafeOkHttpClient() {
    try {//from w  w  w  . ja  v  a2 s  . c o  m
        // Create a trust manager that does not validate certificate chains
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
                    throws CertificateException {
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
                    throws CertificateException {
            }

            @Override
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };

        // Install the all-trusting trust manager
        final SSLContext sslContext = SSLContext.getInstance("SSL");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setSslSocketFactory(sslSocketFactory);
        okHttpClient.setHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        return okHttpClient;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}