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