Search in sources :

Example 1 with Request

use of com.squareup.okhttp.Request 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 Request

use of com.squareup.okhttp.Request 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 Request

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

the class HttpUtil method downMp3.

public static void downMp3(final String url, final String name) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
                mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
                Request request = new Request.Builder().url(url).build();
                Response response = mOkHttpClient.newCall(request).execute();
                if (response.isSuccessful()) {
                    FileOutputStream fo = new FileOutputStream("/storage/emulated/0/" + name + ".mp3");
                    byte[] c = new byte[1024];
                    while (response.body().source().read(c) != -1) {
                        fo.write(c);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}
Also used : Response(com.squareup.okhttp.Response) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) FileOutputStream(java.io.FileOutputStream) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with Request

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

the class HttpUtil method getResposeString.

public static String getResposeString(String action1) {
    try {
        mOkHttpClient.setConnectTimeout(1000, TimeUnit.MINUTES);
        mOkHttpClient.setReadTimeout(1000, TimeUnit.MINUTES);
        Request request = new Request.Builder().url(action1).build();
        Response response = mOkHttpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            String c = response.body().string();
            Log.e("billboard", c);
            return c;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Response(com.squareup.okhttp.Response) FormEncodingBuilder(com.squareup.okhttp.FormEncodingBuilder) Request(com.squareup.okhttp.Request) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with Request

use of com.squareup.okhttp.Request in project weex-example by KalicyZhou.

the class WXWebSocketManager method connect.

public void connect(String url) {
    try {
        mHttpClient = (OkHttpClient) Class.forName("com.squareup.okhttp.OkHttpClient").newInstance();
    } catch (Exception e) {
        isSupportWebSocket = false;
        return;
    }
    mHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
    mHttpClient.setWriteTimeout(10, TimeUnit.SECONDS);
    // Disable timeouts for read
    mHttpClient.setReadTimeout(0, TimeUnit.MINUTES);
    Request request = new Request.Builder().url(url).build();
    WebSocketCall call = WebSocketCall.create(mHttpClient, request);
    call.enqueue(this);
}
Also used : WebSocketCall(com.squareup.okhttp.ws.WebSocketCall) Request(com.squareup.okhttp.Request) IOException(java.io.IOException)

Aggregations

Request (com.squareup.okhttp.Request)111 Response (com.squareup.okhttp.Response)75 IOException (java.io.IOException)60 OkHttpClient (com.squareup.okhttp.OkHttpClient)37 RequestBody (com.squareup.okhttp.RequestBody)30 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)19 UnsupportedEncodingException (java.io.UnsupportedEncodingException)12 File (java.io.File)11 Callback (com.squareup.okhttp.Callback)10 InputStream (java.io.InputStream)7 Buffer (okio.Buffer)6 HttpUrl (com.squareup.okhttp.HttpUrl)5 MediaType (com.squareup.okhttp.MediaType)5 SocketTimeoutException (java.net.SocketTimeoutException)5 HashMap (java.util.HashMap)5 Call (com.squareup.okhttp.Call)4 ResponseBody (com.squareup.okhttp.ResponseBody)4 FileOutputStream (java.io.FileOutputStream)4 Activity (android.app.Activity)3 Intent (android.content.Intent)3