Search in sources :

Example 16 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project FreeFlow by Comcast.

the class DribbbleFetch method load.

public void load(final ArtbookActivity caller, int itemsPerPage, int page) {
    new AsyncTask<String, Void, String>() {

        OkHttpClient client = new OkHttpClient();

        private Exception exception;

        protected String doInBackground(String... urls) {
            try {
                return get(new URL(urls[0]));
            } catch (Exception e) {
                this.exception = e;
                Log.e(TAG, "Exception: " + e);
                return null;
            }
        }

        protected void onPostExecute(String data) {
            DribbbleFeed feed = new Gson().fromJson(data, DribbbleFeed.class);
            caller.onDataLoaded(feed);
        }

        String get(URL url) throws IOException {
            HttpURLConnection connection = client.open(url);
            InputStream in = null;
            try {
                in = connection.getInputStream();
                byte[] response = readFully(in);
                return new String(response, "UTF-8");
            } finally {
                if (in != null)
                    in.close();
            }
        }

        byte[] readFully(InputStream in) throws IOException {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            for (int count; (count = in.read(buffer)) != -1; ) {
                out.write(buffer, 0, count);
            }
            return out.toByteArray();
        }
    }.execute("http://api.dribbble.com/shots/popular?per_page=" + itemsPerPage + "&page=" + page);
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) Gson(com.google.gson.Gson) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) URL(java.net.URL)

Example 17 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project ETSMobile-Android2 by ApplETS.

the class AuthentificationPortailTask method doInBackground.

protected Intent doInBackground(String... params) {
    OkHttpClient client = new OkHttpClient();
    String url = params[0], username = params[1], password = params[2];
    MediaType mediaType = MediaType.parse("application/json");
    RequestBody body = RequestBody.create(mediaType, "{\n  \"Username\": \"" + username + "\",\n  \"Password\": \"" + password + "\"\n}");
    Request request = new Request.Builder().url(url).post(body).addHeader("content-type", "application/json").addHeader("cache-control", "no-cache").build();
    Response response = null;
    String authCookie = "", domaine = "";
    int typeUsagerId = 0;
    final Intent res = new Intent();
    try {
        response = client.newCall(request).execute();
        if (response.code() == 200) {
            authCookie = response.header("Set-Cookie");
            JSONObject jsonResponse = new JSONObject(response.body().string());
            typeUsagerId = jsonResponse.getInt("TypeUsagerId");
            domaine = jsonResponse.getString("Domaine");
            res.putExtra(AccountManager.KEY_AUTHTOKEN, authCookie);
            res.putExtra(Constants.TYPE_USAGER_ID, typeUsagerId);
            res.putExtra(Constants.DOMAINE, domaine);
        } else {
            Log.e("Erreur Portail", response.toString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    res.putExtra(AccountManager.KEY_ACCOUNT_NAME, username);
    res.putExtra(AccountManager.KEY_ACCOUNT_TYPE, Constants.ACCOUNT_TYPE);
    res.putExtra(Constants.PARAM_USER_PASS, password);
    return res;
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) JSONObject(org.json.JSONObject) Request(com.squareup.okhttp.Request) MediaType(com.squareup.okhttp.MediaType) JSONException(org.json.JSONException) Intent(android.content.Intent) IOException(java.io.IOException) RequestBody(com.squareup.okhttp.RequestBody)

Example 18 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project ETSMobile-Android2 by ApplETS.

the class AppletsApiEvenementsRequest method loadDataFromNetwork.

@Override
public EvenementCommunauteList loadDataFromNetwork() throws Exception {
    String eventsAddress = context.getString(R.string.applets_api_events, source.getKey());
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder().url(eventsAddress).get().build();
    Response response = client.newCall(request).execute();
    String result = response.body().string();
    EvenementCommunauteList evenementList = new Gson().fromJson(result, EvenementCommunauteList.class);
    for (EvenementCommunaute event : evenementList) {
        event.setSourceEvenement(source);
    }
    return evenementList;
}
Also used : Response(com.squareup.okhttp.Response) EvenementCommunauteList(ca.etsmtl.applets.etsmobile.model.applets_events.EvenementCommunauteList) EvenementCommunaute(ca.etsmtl.applets.etsmobile.model.applets_events.EvenementCommunaute) OkHttpClient(com.squareup.okhttp.OkHttpClient) SpringAndroidSpiceRequest(com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest) Request(com.squareup.okhttp.Request) Gson(com.google.gson.Gson)

Example 19 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project AisenWeiBo by wangdan.

the class HttpsUtility method getOkHttpClient.

public OkHttpClient getOkHttpClient() {
    if (mOKHttpClient == null) {
        try {
            mOKHttpClient = new OkHttpClient();
            mOKHttpClient.setConnectTimeout(GlobalContext.CONN_TIMEOUT, TimeUnit.MILLISECONDS);
            mOKHttpClient.setReadTimeout(GlobalContext.READ_TIMEOUT, TimeUnit.MILLISECONDS);
            TrustManager tm = new X509TrustManager() {

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

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

                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
            };
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[] { tm }, null);
            mOKHttpClient.setSslSocketFactory(sslContext.getSocketFactory());
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    return mOKHttpClient;
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) X509TrustManager(javax.net.ssl.X509TrustManager) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager)

Example 20 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project pictureapp by EyeSeeTea.

the class UnsafeOkHttpsClientFactory method getUnsafeOkHttpClient.

public static OkHttpClient getUnsafeOkHttpClient() {
    try {
        // 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);
    }
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) CertificateException(java.security.cert.CertificateException) X509TrustManager(javax.net.ssl.X509TrustManager) TrustManager(javax.net.ssl.TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier) X509TrustManager(javax.net.ssl.X509TrustManager) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Aggregations

OkHttpClient (com.squareup.okhttp.OkHttpClient)47 Request (com.squareup.okhttp.Request)22 Response (com.squareup.okhttp.Response)18 IOException (java.io.IOException)15 RequestBody (com.squareup.okhttp.RequestBody)8 Provides (dagger.Provides)6 Gson (com.google.gson.Gson)5 Cache (com.squareup.okhttp.Cache)5 Singleton (javax.inject.Singleton)5 SpringAndroidSpiceRequest (com.octo.android.robospice.request.springandroid.SpringAndroidSpiceRequest)4 File (java.io.File)4 RestAdapter (retrofit.RestAdapter)4 OkClient (retrofit.client.OkClient)4 SSLContext (javax.net.ssl.SSLContext)3 X509TrustManager (javax.net.ssl.X509TrustManager)3 HttpResponse (org.apache.http.HttpResponse)3 Intent (android.content.Intent)2 SharedPreferences (android.content.SharedPreferences)2 Call (com.squareup.okhttp.Call)2 Dispatcher (com.squareup.okhttp.Dispatcher)2