Search in sources :

Example 1 with Response

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

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

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

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

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

the class BasicAuthenticator method getOrgUnitData.

/**
     * Returns the orgunit data from the given server according to its current version
     */
static JSONObject getOrgUnitData(String url, String orgUnitNameOrCode) {
    //Version is required to choose which field to match
    String serverVersion = getServerVersion(url);
    //No version -> No data
    if (serverVersion == null) {
        return null;
    }
    try {
        String urlOrgUnitData = getOrgUnitDataUrl(url, serverVersion, orgUnitNameOrCode);
        Response response = executeCall(null, urlOrgUnitData, "GET");
        //Error -> null
        if (!response.isSuccessful()) {
            Log.e(TAG, "getOrgUnitData (" + response.code() + "): " + response.body().string());
            throw new IOException(response.message());
        }
        //{"organisationUnits":[{}]}
        JSONObject jsonResponse = parseResponse(response.body().string());
        JSONArray orgUnitsArray = (JSONArray) jsonResponse.get(TAG_ORGANISATIONUNITS);
        //0| >1 matches -> Error
        if (orgUnitsArray.length() == 0 || orgUnitsArray.length() > 1) {
            Log.e(TAG, String.format("getOrgUnitData(%s,%s) -> Found %d matches", url, orgUnitNameOrCode, orgUnitsArray.length()));
            return null;
        }
        return (JSONObject) orgUnitsArray.get(0);
    } catch (Exception ex) {
        Log.e(TAG, String.format("getOrgUnitData(%s,%s): %s", url, orgUnitNameOrCode, ex.toString()));
        return null;
    }
}
Also used : Response(com.squareup.okhttp.Response) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) IOException(java.io.IOException) JSONException(org.json.JSONException) ShowException(org.eyeseetea.malariacare.views.ShowException) IOException(java.io.IOException)

Aggregations

Response (com.squareup.okhttp.Response)108 Request (com.squareup.okhttp.Request)75 IOException (java.io.IOException)72 OkHttpClient (com.squareup.okhttp.OkHttpClient)33 RequestBody (com.squareup.okhttp.RequestBody)24 JSONException (org.json.JSONException)16 JSONObject (org.json.JSONObject)15 FormEncodingBuilder (com.squareup.okhttp.FormEncodingBuilder)14 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 ApiCallException (org.eyeseetea.malariacare.domain.exception.ApiCallException)10 Callback (com.squareup.okhttp.Callback)9 SocketTimeoutException (java.net.SocketTimeoutException)8 File (java.io.File)7 Buffer (okio.Buffer)7 ShowException (org.eyeseetea.malariacare.views.ShowException)7 MediaType (com.squareup.okhttp.MediaType)5 InputStream (java.io.InputStream)5 ResponseBody (com.squareup.okhttp.ResponseBody)4 ApiException (io.kubernetes.client.ApiException)4 FileOutputStream (java.io.FileOutputStream)4