use of okhttp3.RequestBody in project chefly_android by chef-ly.
the class HttpConnection method downloadFromFeed.
public String downloadFromFeed(RequestMethod requestPackage) throws IOException {
String address = requestPackage.getEndpoint();
String encodedParams = requestPackage.getEncodedParams();
//check for get request
if (requestPackage.getMethod().equals("GET") && encodedParams.length() > 0) {
address = String.format("%s?%s", address, encodedParams);
}
//create request object
OkHttpClient client;
if (BuildConfig.DEBUG) {
client = new OkHttpClient.Builder().addInterceptor(new LoggingInterceptor()).build();
} else {
client = new OkHttpClient();
}
Request.Builder requestBuilder = new Request.Builder().url(address);
// Add required HTTP Header
Map<String, String> headers = requestPackage.getHeaders();
if (headers.size() > 0) {
for (String k : headers.keySet()) {
if (k != null) {
requestBuilder.addHeader(k, headers.get(k));
}
}
}
//check for post request
if (requestPackage.getMethod().equals("POST")) {
//extract the parameters from the request
//get the reference of the pair
Map<String, String> params = requestPackage.getParams();
String msg = "";
if (params.size() == 1) {
for (String key : params.keySet()) msg = params.get(key);
} else {
JSONObject parameter = new JSONObject(params);
msg = parameter.toString();
}
RequestBody body = RequestBody.create(JSON, msg);
Log.d(TAG, msg);
requestBuilder.post(body);
//requestBuilder.addHeader("content-type", "application/json; charset=utf-8");
requestBuilder.addHeader("content-type", "application/x-www-form-urlencoded");
}
Request request = requestBuilder.build();
//get response with the use of the method newCall
//synchronous request
Response response = client.newCall(request).execute();
//check if the request is successful
if (response.isSuccessful()) {
this.statusMessage = response.message();
return response.body().string();
} else {
Log.d(TAG, response.toString());
throw new IOException("Exception: response code " + response.code());
}
}
use of okhttp3.RequestBody in project openstack4j by ContainX.
the class VolumeBackupTests method createVolumeBackupFromSnapshotV1.
@Test
public void createVolumeBackupFromSnapshotV1() throws Exception {
respondWith("/storage/v1/volumebackup_create_response.json");
final String name = "backup1122";
VolumeBackupCreate create = Builders.volumeBackupCreate().volumeId("999b49ff-a813-45cc-aef3-3ec82f089490").container("container123").description("description123").name(name).incremental(false).snapshotId("b4b3258d-555a-4fce-8f53-69cc2ae96d3c").build();
VolumeBackup backup = osv3().blockStorage().backups().create(create);
RecordedRequest request = server.takeRequest();
assertNotNull(request.getHeader("X-Auth-Token"));
assertTrue(request.getPath().matches("/v[123]/\\p{XDigit}*/backups"));
assertEquals(request.getMethod(), "POST");
String requestBody = request.getBody().readUtf8();
assertTrue(requestBody.contains("\"volume_id\" : \"999b49ff-a813-45cc-aef3-3ec82f089490\""));
assertTrue(requestBody.contains("\"snapshot_id\" : \"b4b3258d-555a-4fce-8f53-69cc2ae96d3c\""));
assertEquals(backup.getName(), name);
assertNotNull(backup.getId());
assertEquals(backup.getId(), "7069c687-c85c-45ca-befa-aa78a971fdfe");
}
use of okhttp3.RequestBody in project openstack4j by ContainX.
the class VolumeBackupTests method createVolumeBackupV1.
@Test
public void createVolumeBackupV1() throws Exception {
respondWith("/storage/v1/volumebackup_create_response.json");
final String name = "backup1122";
VolumeBackupCreate create = Builders.volumeBackupCreate().volumeId("999b49ff-a813-45cc-aef3-3ec82f089490").container("container123").description("description123").name(name).incremental(false).build();
VolumeBackup backup = osv3().blockStorage().backups().create(create);
RecordedRequest request = server.takeRequest();
assertNotNull(request.getHeader("X-Auth-Token"));
assertTrue(request.getPath().matches("/v[123]/\\p{XDigit}*/backups"));
assertEquals(request.getMethod(), "POST");
String requestBody = request.getBody().readUtf8();
assertTrue(requestBody.contains("\"volume_id\" : \"999b49ff-a813-45cc-aef3-3ec82f089490\""));
assertEquals(backup.getName(), name);
assertNotNull(backup.getId());
assertEquals(backup.getId(), "7069c687-c85c-45ca-befa-aa78a971fdfe");
}
use of okhttp3.RequestBody in project Varis-Android by dkhmelenko.
the class BuildsDetailsPresenter method restartBuild.
/**
* Restarts build process
*/
public void restartBuild() {
RequestBody emptyBody = RequestBody.create(MediaType.parse("application/json"), "");
Disposable subscription = mTravisRestClient.getApiService().restartBuild(mBuildId, emptyBody).onErrorReturn(throwable -> new Object()).flatMap(new Function<Object, SingleSource<BuildDetails>>() {
@Override
public SingleSource<BuildDetails> apply(@NonNull Object o) throws Exception {
return mTravisRestClient.getApiService().getBuild(mRepoSlug, mBuildId);
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe((buildDetails, throwable) -> {
if (throwable == null) {
handleBuildDetails(buildDetails);
} else {
handleLoadingFailed(throwable);
}
});
mSubscriptions.add(subscription);
}
use of okhttp3.RequestBody in project xmall by Exrick.
the class QiniuUtil method qiniuBase64Upload.
public static String qiniuBase64Upload(String data64) {
String key = renamePic(".png");
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
// 服务端http://up-z2.qiniup.com
String url = "http://up-z2.qiniup.com/putb64/-1/key/" + UrlSafeBase64.encodeToString(key);
RequestBody rb = RequestBody.create(null, data64);
Request request = new Request.Builder().url(url).addHeader("Content-Type", "application/octet-stream").addHeader("Authorization", "UpToken " + getUpToken()).post(rb).build();
System.out.println(request.headers());
OkHttpClient client = new OkHttpClient();
okhttp3.Response response = null;
try {
response = client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(response);
return origin + key;
}
Aggregations