Search in sources :

Example 6 with OkHttpClient

use of okhttp3.OkHttpClient in project sonarqube by SonarSource.

the class OkHttpClientProviderTest method get_returns_a_singleton.

@Test
public void get_returns_a_singleton() {
    OkHttpClient client1 = underTest.provide(settings, runtime);
    OkHttpClient client2 = underTest.provide(settings, runtime);
    assertThat(client2).isNotNull().isSameAs(client1);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Test(org.junit.Test)

Example 7 with OkHttpClient

use of okhttp3.OkHttpClient in project Pokemap by omkarmoghe.

the class NianticManager method loginPTC.

private void loginPTC(final String username, final String password, NianticService.LoginValues values, final LoginListener loginListener) {
    HttpUrl url = HttpUrl.parse(LOGIN_URL).newBuilder().addQueryParameter("lt", values.getLt()).addQueryParameter("execution", values.getExecution()).addQueryParameter("_eventId", "submit").addQueryParameter("username", username).addQueryParameter("password", password).build();
    OkHttpClient client = mClient.newBuilder().followRedirects(false).followSslRedirects(false).build();
    NianticService service = new Retrofit.Builder().baseUrl(BASE_URL).addConverterFactory(GsonConverterFactory.create()).client(client).build().create(NianticService.class);
    Callback<NianticService.LoginResponse> loginCallback = new Callback<NianticService.LoginResponse>() {

        @Override
        public void onResponse(Call<NianticService.LoginResponse> call, Response<NianticService.LoginResponse> response) {
            String location = response.headers().get("location");
            if (location != null && location.split("ticket=").length > 0) {
                String ticket = location.split("ticket=")[1];
                requestToken(ticket, loginListener);
            } else {
                Log.e(TAG, "PTC login failed via loginPTC(). There was no location header in response.");
                loginListener.authFailed("Pokemon Trainer Club Login Failed");
            }
        }

        @Override
        public void onFailure(Call<NianticService.LoginResponse> call, Throwable t) {
            t.printStackTrace();
            Log.e(TAG, "PTC login failed via loginPTC(). loginCallback.onFailure() threw: " + t.getMessage());
            loginListener.authFailed("Pokemon Trainer Club Login Failed");
        }
    };
    Call<NianticService.LoginResponse> call = service.login(url.toString());
    call.enqueue(loginCallback);
}
Also used : Call(retrofit2.Call) OkHttpClient(okhttp3.OkHttpClient) HttpUrl(okhttp3.HttpUrl) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Callback(retrofit2.Callback)

Example 8 with OkHttpClient

use of okhttp3.OkHttpClient in project CloudReader by youlookwhat.

the class HttpUtils method getOkClient.

public OkHttpClient getOkClient() {
    OkHttpClient client1;
    client1 = getUnsafeOkHttpClient();
    return client1;
}
Also used : OkHttpClient(okhttp3.OkHttpClient)

Example 9 with OkHttpClient

use of okhttp3.OkHttpClient in project CloudReader by youlookwhat.

the class HttpUtils method getUnsafeOkHttpClient.

public OkHttpClient getUnsafeOkHttpClient() {
    try {
        final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

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

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[] {};
            }
        } };
        // Install the all-trusting trust manager
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustAllCerts, new SecureRandom());
        // Create an ssl socket factory with our all-trusting manager
        SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
        OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
        okBuilder.readTimeout(20, TimeUnit.SECONDS);
        okBuilder.connectTimeout(10, TimeUnit.SECONDS);
        okBuilder.writeTimeout(20, TimeUnit.SECONDS);
        okBuilder.addInterceptor(new HttpHeadInterceptor());
        okBuilder.addInterceptor(getInterceptor());
        okBuilder.sslSocketFactory(sslSocketFactory);
        okBuilder.hostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String hostname, SSLSession session) {
                //                    Log.d("HttpUtils", "==come");
                return true;
            }
        });
        return okBuilder.build();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) GsonBuilder(com.google.gson.GsonBuilder) SSLSession(javax.net.ssl.SSLSession) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 10 with OkHttpClient

use of okhttp3.OkHttpClient in project Android by Tracman-org.

the class LoginActivity method authenticateWithTracmanServer.

private void authenticateWithTracmanServer(final Request request) throws Exception {
    // Needed to support TLS 1.1 and 1.2
    //		TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
    //				TrustManagerFactory.getDefaultAlgorithm());
    //		trustManagerFactory.init((KeyStore) null);
    //		TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
    //		if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {
    //			throw new IllegalStateException("Unexpected default trust managers:"
    //					+ Arrays.toString(trustManagers));
    //		}
    //		X509TrustManager trustManager = (X509TrustManager) trustManagers[0];
    OkHttpClient client = new OkHttpClient.Builder().build();
    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
            //Log.e(TAG, "Failed to connect to Tracman server!");
            showError(R.string.server_connection_error);
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response res) throws IOException {
            if (!res.isSuccessful()) {
                showError(R.string.login_no_user_error);
                res.body().close();
                throw new IOException("Unexpected code: " + res);
            } else {
                //Log.d(TAG, "Response code: " + res.code());
                String userString = res.body().string();
                System.out.println("Full response: " + userString);
                String userID, userName, userSK;
                try {
                    JSONObject user = new JSONObject(userString);
                    userID = user.getString("_id");
                    userName = user.getString("name");
                    userSK = user.getString("sk32");
                //Log.v(TAG, "User retrieved with ID: " + userID);
                } catch (JSONException e) {
                    //Log.e(TAG, "Unable to parse user JSON: ", e);
                    //Log.e(TAG, "JSON String used: " + userString);
                    userID = null;
                    userName = null;
                    userSK = null;
                }
                // Save user as loggedInUser
                SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences.Editor editor = sharedPref.edit();
                editor.putString("loggedInUser", userString);
                editor.putString("loggedInUserId", userID);
                editor.putString("loggedInUserName", userName);
                editor.putString("loggedInUserSk", userSK);
                editor.commit();
                startActivityForResult(new Intent(LoginActivity.this, SettingsActivity.class), SIGN_OUT);
            }
        }
    });
}
Also used : Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) SharedPreferences(android.content.SharedPreferences) JSONException(org.json.JSONException) Intent(android.content.Intent) IOException(java.io.IOException) Response(okhttp3.Response) Callback(okhttp3.Callback) ResultCallback(com.google.android.gms.common.api.ResultCallback) JSONObject(org.json.JSONObject)

Aggregations

OkHttpClient (okhttp3.OkHttpClient)632 Request (okhttp3.Request)359 Response (okhttp3.Response)302 IOException (java.io.IOException)196 Test (org.junit.Test)155 Call (okhttp3.Call)111 HttpLoggingInterceptor (okhttp3.logging.HttpLoggingInterceptor)67 Retrofit (retrofit2.Retrofit)65 File (java.io.File)56 Interceptor (okhttp3.Interceptor)45 RequestBody (okhttp3.RequestBody)39 ResponseBody (okhttp3.ResponseBody)38 Cache (okhttp3.Cache)36 Gson (com.google.gson.Gson)33 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)33 Headers (okhttp3.Headers)33 Callback (okhttp3.Callback)32 GsonBuilder (com.google.gson.GsonBuilder)29 JSONObject (org.json.JSONObject)28 Provides (dagger.Provides)27