Search in sources :

Example 1 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project hadoop by apache.

the class ConfRefreshTokenBasedAccessTokenProvider method refresh.

void refresh() throws IOException {
    try {
        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
        client.setReadTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
        String bodyString = Utils.postBody(GRANT_TYPE, REFRESH_TOKEN, REFRESH_TOKEN, refreshToken, CLIENT_ID, clientId);
        RequestBody body = RequestBody.create(URLENCODED, bodyString);
        Request request = new Request.Builder().url(refreshURL).post(body).build();
        Response responseBody = client.newCall(request).execute();
        if (responseBody.code() != HttpStatus.SC_OK) {
            throw new IllegalArgumentException("Received invalid http response: " + responseBody.code() + ", text = " + responseBody.toString());
        }
        Map<?, ?> response = READER.readValue(responseBody.body().string());
        String newExpiresIn = response.get(EXPIRES_IN).toString();
        accessTokenTimer.setExpiresIn(newExpiresIn);
        accessToken = response.get(ACCESS_TOKEN).toString();
    } catch (Exception e) {
        throw new IOException("Exception while refreshing access token", e);
    }
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) IOException(java.io.IOException) RequestBody(com.squareup.okhttp.RequestBody)

Example 2 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project hadoop by apache.

the class CredentialBasedAccessTokenProvider method refresh.

void refresh() throws IOException {
    try {
        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
        client.setReadTimeout(URLConnectionFactory.DEFAULT_SOCKET_TIMEOUT, TimeUnit.MILLISECONDS);
        String bodyString = Utils.postBody(CLIENT_SECRET, getCredential(), GRANT_TYPE, CLIENT_CREDENTIALS, CLIENT_ID, clientId);
        RequestBody body = RequestBody.create(URLENCODED, bodyString);
        Request request = new Request.Builder().url(refreshURL).post(body).build();
        Response responseBody = client.newCall(request).execute();
        if (responseBody.code() != HttpStatus.SC_OK) {
            throw new IllegalArgumentException("Received invalid http response: " + responseBody.code() + ", text = " + responseBody.toString());
        }
        Map<?, ?> response = READER.readValue(responseBody.body().string());
        String newExpiresIn = response.get(EXPIRES_IN).toString();
        timer.setExpiresIn(newExpiresIn);
        accessToken = response.get(ACCESS_TOKEN).toString();
    } catch (Exception e) {
        throw new IOException("Unable to obtain access token from credential", e);
    }
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) IOException(java.io.IOException) RequestBody(com.squareup.okhttp.RequestBody)

Example 3 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project storymaker by StoryMaker.

the class StorymakerDownloadManager method downloadWithTor.

private void downloadWithTor(boolean useTor, Uri uri, String title, String desc, File targetFile) {
    initNotificationManager();
    // generate id/tag for notification
    String nTag = indexItem.getExpansionId();
    int nId = 0;
    if (fileName.contains(scal.io.liger.Constants.MAIN)) {
        nId = Integer.parseInt(indexItem.getExpansionFileVersion());
    } else if (fileName.contains(scal.io.liger.Constants.PATCH)) {
        nId = Integer.parseInt(indexItem.getPatchFileVersion());
    }
    // incompatible with lungcast certificate
    // StrongHttpsClient httpClient = getHttpClientInstance();
    OkHttpClient httpClient = new OkHttpClient();
    // we're now using this method to support non-tor downloads as well, so settings must be checked
    if (useTor) {
        if (checkTor(context)) {
            Timber.d("DOWNLOAD WITH TOR PROXY: " + scal.io.liger.Constants.TOR_PROXY_HOST + "/" + scal.io.liger.Constants.TOR_PROXY_PORT);
            SocketAddress torSocket = new InetSocketAddress(scal.io.liger.Constants.TOR_PROXY_HOST, scal.io.liger.Constants.TOR_PROXY_PORT);
            Proxy torProxy = new Proxy(Proxy.Type.HTTP, torSocket);
            httpClient.setProxy(torProxy);
        } else {
            Timber.e("CANNOT DOWNLOAD WITH TOR, TOR IS NOT ACTIVE");
            if (context instanceof Activity) {
                // FIXME move to strings
                Utils.toastOnUiThread((Activity) context, "Check settings, can't use tor if orbot isn't running", true);
            }
            StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
            return;
        }
    }
    // disable attempts to retry (more retries ties up connection and prevents failure handling)
    httpClient.setRetryOnConnectionFailure(false);
    // set modest timeout (longer timeout ties up connection and prevents failure handling)
    httpClient.setConnectTimeout(3000, TimeUnit.MILLISECONDS);
    String actualFileName = targetFile.getName().substring(0, targetFile.getName().lastIndexOf("."));
    Timber.d("CHECKING URI: " + uri.toString());
    try {
        // FIXME - adding the "Connection: close" header to resolve an issue that seemed to be caused
        // FIXME - by a lingering connection.  when possible, the better solution would be to track
        // FIXME - down the possible end states and add appropriate cleanup steps.
        Request request = new Request.Builder().url(uri.toString()).addHeader("Connection", "close").build();
        // check for partially downloaded file
        File partFile = new File(targetFile.getPath().replace(".tmp", ".part"));
        if (partFile.exists()) {
            long partBytes = partFile.length();
            Timber.d("PARTIAL FILE " + partFile.getPath() + " FOUND, SETTING RANGE HEADER: " + "Range" + " / " + "bytes=" + Long.toString(partBytes) + "-");
            // request.setHeader("Range", "bytes=" + Long.toString(partBytes) + "-");
            request = new Request.Builder().url(uri.toString()).addHeader("Connection", "close").addHeader("Range", "bytes=" + Long.toString(partBytes) + "-").build();
        } else {
            Timber.d("PARTIAL FILE " + partFile.getPath() + " NOT FOUND, STARTING AT BYTE 0");
        }
        Response response = httpClient.newCall(request).execute();
        int statusCode = response.code();
        if ((statusCode == 200) || (statusCode == 206)) {
            Timber.d("DOWNLOAD SUCCEEDED, STATUS CODE: " + statusCode);
            // queue item here, "download" doesn't start until after we get a status code
            // queue item, use date to get a unique long, subtract to get a negative number (to distinguish from download manager items)
            Date startTime = new Date();
            long queueId = 0 - startTime.getTime();
            StorymakerQueueManager.addToQueue(context, queueId, targetFile.getName(), queueDao);
            targetFile.getParentFile().mkdirs();
            Timber.d("DOWNLOAD SUCCEEDED, GETTING ENTITY...");
            BufferedInputStream responseInput = new BufferedInputStream(response.body().byteStream());
            try {
                FileOutputStream targetOutput = new FileOutputStream(targetFile);
                byte[] buf = new byte[1024];
                int i;
                int oldPercent = 0;
                long thisTime;
                long lastTime = -1;
                int lastPercent = 0;
                while ((i = responseInput.read(buf)) > 0) {
                    // create status bar notification
                    int nPercent = StorymakerDownloadHelper.getDownloadPercent(context, fileName, installedDao);
                    thisTime = System.currentTimeMillis();
                    if (oldPercent == nPercent) {
                    // need to cut back on notification traffic
                    } else {
                        if (nPercent % 10 == 0 && nPercent != lastPercent) {
                            if (lastTime == -1 || (thisTime - lastTime) > 1000) {
                                lastPercent = nPercent;
                                oldPercent = nPercent;
                                lastTime = thisTime;
                                Notification nProgress = new Notification.Builder(context).setContentTitle(mAppTitle + " content download").setContentText(// assignment file names are meaningless uuids
                                indexItem.getTitle() + " - " + (nPercent / 10.0) + "%").setSmallIcon(android.R.drawable.arrow_down_float).setProgress(100, (nPercent / 10), false).setWhen(startTime.getTime()).build();
                                nManager.notify(nTag, nId, nProgress);
                            //Log.d("Storymaker Download Manager", "** NOTIFICATION ** " + nPercent );
                            }
                        }
                    }
                    targetOutput.write(buf, 0, i);
                }
                targetOutput.close();
                responseInput.close();
                Timber.d("SAVED DOWNLOAD TO " + targetFile);
            } catch (ConnectTimeoutException cte) {
                Timber.e(cte, "FAILED TO SAVE DOWNLOAD TO " + actualFileName + " (CONNECTION EXCEPTION)");
            } catch (SocketTimeoutException ste) {
                Timber.e(ste, "FAILED TO SAVE DOWNLOAD TO " + actualFileName + " (SOCKET EXCEPTION)");
            } catch (IOException ioe) {
                Timber.e(ioe, "FAILED TO SAVE DOWNLOAD TO " + actualFileName + " (IO EXCEPTION)");
            }
            // remove from queue here, regardless of success
            StorymakerQueueManager.removeFromQueue(context, queueId, queueDao);
            // remove notification, regardless of success
            nManager.cancel(nTag, nId);
            // (assumes .tmp file will exist if download is interrupted)
            if (!handleFile(targetFile)) {
                Timber.e("ERROR DURING FILE PROCESSING FOR " + actualFileName);
            }
        } else {
            Timber.e("DOWNLOAD FAILED FOR " + actualFileName + ", STATUS CODE: " + statusCode);
            StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
        }
    // clean up connection
    // EntityUtils.consume(entity);
    // request.abort();
    // request.releaseConnection();
    } catch (IOException ioe) {
        Timber.e(ioe, "DOWNLOAD FAILED FOR " + actualFileName + ", EXCEPTION THROWN");
        StorymakerQueueManager.checkQueueFinished(context, targetFile.getName());
    }
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) InetSocketAddress(java.net.InetSocketAddress) Request(com.squareup.okhttp.Request) Activity(android.app.Activity) IOException(java.io.IOException) Date(java.util.Date) Notification(android.app.Notification) Response(com.squareup.okhttp.Response) HttpResponse(ch.boye.httpclientandroidlib.HttpResponse) Proxy(java.net.Proxy) SocketTimeoutException(java.net.SocketTimeoutException) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) File(java.io.File) ConnectTimeoutException(ch.boye.httpclientandroidlib.conn.ConnectTimeoutException)

Example 4 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project remusic by aa112901.

the class RestServiceFactory method create.

public static <T> T create(final Context context, String baseUrl, Class<T> clazz) {
    final OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.setCache(new Cache(context.getApplicationContext().getCacheDir(), CACHE_SIZE));
    okHttpClient.setConnectTimeout(40, TimeUnit.SECONDS);
    RequestInterceptor interceptor = new RequestInterceptor() {

        @Override
        public void intercept(RequestFacade request) {
            //7-days cache
            request.addHeader("Cache-Control", String.format("max-age=%d,max-stale=%d", Integer.valueOf(60 * 60 * 24 * 7), Integer.valueOf(31536000)));
            request.addHeader("Connection", "keep-alive");
        }
    };
    RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(baseUrl).setRequestInterceptor(interceptor).setClient(new OkClient(okHttpClient));
    return builder.build().create(clazz);
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient) OkClient(retrofit.client.OkClient) RequestInterceptor(retrofit.RequestInterceptor) RestAdapter(retrofit.RestAdapter) Cache(com.squareup.okhttp.Cache)

Example 5 with OkHttpClient

use of com.squareup.okhttp.OkHttpClient in project FastDev4Android by jiangqqlmj.

the class OkhttpDemoActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ok_http_demo_layout);
    btn_one = (Button) this.findViewById(R.id.btn_one);
    btn_two = (Button) this.findViewById(R.id.btn_two);
    top_bar_title = (TextView) this.findViewById(R.id.top_bar_title);
    tv_result = (TextView) this.findViewById(R.id.tv_result);
    top_bar_title.setText("Okhttp实例");
    top_bar_linear_back = (LinearLayout) this.findViewById(R.id.top_bar_linear_back);
    top_bar_linear_back.setOnClickListener(new CustomOnClickListener());
    btn_one.setOnClickListener(new CustomOnClickListener());
    btn_two.setOnClickListener(new CustomOnClickListener());
    client = new OkHttpClient();
}
Also used : OkHttpClient(com.squareup.okhttp.OkHttpClient)

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