use of com.squareup.okhttp.RequestBody 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.RequestBody 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.RequestBody in project openzaly by akaxincom.
the class HttpTestPost method post.
static String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder().url(url).post(body).build();
Response response = client.newCall(request).execute();
System.out.println("response = " + response.isSuccessful());
if (response.isSuccessful()) {
return response.body().string();
} else {
throw new IOException("Unexpected code " + response);
}
}
use of com.squareup.okhttp.RequestBody in project xDrip by NightscoutFoundation.
the class GzipRequestInterceptor method forceContentLength.
/**
* https://github.com/square/okhttp/issues/350
*/
private RequestBody forceContentLength(final RequestBody requestBody) throws IOException {
final Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
return new RequestBody() {
@Override
public MediaType contentType() {
return requestBody.contentType();
}
@Override
public long contentLength() {
return buffer.size();
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.write(buffer.snapshot());
}
};
}
use of com.squareup.okhttp.RequestBody in project xDrip-plus by jamorham.
the class DisplayQRCode method uploadBytes.
public static void uploadBytes(byte[] result, final int callback_option) {
final PowerManager.WakeLock wl = JoH.getWakeLock("uploadBytes", 1200000);
if ((result != null) && (result.length > 0)) {
final byte[] mykey = CipherUtils.getRandomKey();
byte[] crypted_data = CipherUtils.encryptBytes(JoH.compressBytesToBytes(result), mykey);
if ((crypted_data != null) && (crypted_data.length > 0)) {
Log.d(TAG, "Before: " + result.length + " After: " + crypted_data.length);
final OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(15, TimeUnit.SECONDS);
client.setReadTimeout(30, TimeUnit.SECONDS);
client.setWriteTimeout(30, TimeUnit.SECONDS);
toast("Preparing");
try {
send_url = xdrip.getAppContext().getString(R.string.wserviceurl) + "/joh-setsw";
final String bbody = Base64.encodeToString(crypted_data, Base64.NO_WRAP);
Log.d(TAG, "Upload Body size: " + bbody.length());
final RequestBody formBody = new FormEncodingBuilder().add("data", bbody).build();
new Thread(new Runnable() {
public void run() {
try {
final Request request = new Request.Builder().header("User-Agent", "Mozilla/5.0 (jamorham)").header("Connection", "close").url(send_url).post(formBody).build();
Log.i(TAG, "Uploading data");
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
final String reply = response.body().string();
Log.d(TAG, "Got success response length: " + reply.length() + " " + reply);
if ((reply.length() == 35) && (reply.startsWith("ID:"))) {
switch(callback_option) {
case 1:
{
if (mInstance != null) {
mInstance.display_final_all_settings_qr_code(reply.substring(3, 35), mykey);
} else {
Log.e(TAG, "mInstance null");
}
break;
}
case 2:
{
GcmActivity.backfillLink(reply.substring(3, 35), JoH.bytesToHex(mykey));
break;
}
default:
{
toast("Invalid callback option on upload");
}
}
} else {
Log.d(TAG, "Got unhandled reply: " + reply);
toast(reply);
}
} else {
toast("Error please try again");
}
} catch (Exception e) {
Log.e(TAG, "Got exception in execute: " + e.toString());
e.printStackTrace();
toast("Error with connection");
}
}
}).start();
} catch (Exception e) {
toast(e.getMessage());
Log.e(TAG, "General exception: " + e.toString());
} finally {
JoH.releaseWakeLock(wl);
}
} else {
toast("Something went wrong preparing the settings");
}
} else {
toast("Could not read data somewhere");
}
}
Aggregations