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);
}
}
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);
}
}
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();
}
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;
}
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;
}
}
Aggregations