use of okhttp3.RequestBody in project curb by irijwj.
the class RetrofitGetData method postAnswer.
public static String postAnswer(String strEntity) {
RequestBody description = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
Call<String> postAnswerCall = serverInterface.postAnswer(description);
Response<String> response;
String postResult = null;
try {
response = postAnswerCall.execute();
if (response.isSuccessful()) {
postResult = response.body();
}
} catch (IOException e) {
e.printStackTrace();
}
return postResult;
}
use of okhttp3.RequestBody in project Varis-Android by dkhmelenko.
the class BuildsDetailsPresenter method cancelBuild.
/**
* Cancels build process
*/
public void cancelBuild() {
RequestBody emptyBody = RequestBody.create(MediaType.parse("application/json"), "");
Disposable subscription = mTravisRestClient.getApiService().cancelBuild(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 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 mobile-sdk-android by meniga.
the class MenigaHttpLogger method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (logLevel == LogLevel.NONE) {
return chain.proceed(request);
}
boolean logBody = logType == LogType.BODY_ONLY || logType == LogType.BODY_AND_HEADERS;
boolean logHeaders = logType == LogType.HEADERS_ONLY || logType == LogType.BODY_AND_HEADERS;
RequestBody requestBody = request.body();
boolean hasRequestBody = requestBody != null;
String reqUrl = request.url().toString();
String requestStartMessage = request.method() + REQUEST_START + reqUrl;
if (!logHeaders && hasRequestBody) {
requestStartMessage += " (" + requestBody.contentLength() + "-byte body)";
}
log(requestStartMessage);
if (logHeaders) {
if (hasRequestBody) {
// them to be included (when available) so their values are known.
if (requestBody.contentType() != null) {
log(getHeaderString("Content-Type", requestBody.contentType().toString()));
}
if (requestBody.contentLength() != -1) {
log(getHeaderString("Content-Length", Long.toString(requestBody.contentLength())));
}
}
Headers headers = request.headers();
for (int i = 0, count = headers.size(); i < count; i++) {
String name = headers.name(i);
// Skip headers from the request body as they are explicitly logged above.
if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Length".equalsIgnoreCase(name)) {
log(getHeaderString(name, headers.value(i)));
}
}
}
if (!logBody) {
log(REQUEST_END + request.method());
} else if (!hasRequestBody) {
log(BODY);
log(NO_BODY);
} else if (bodyEncoded(request.headers())) {
log(REQUEST_END + request.method() + " (encoded body omitted)");
} else {
Buffer buffer = new Buffer();
try {
requestBody.writeTo(buffer);
} catch (ConcurrentModificationException ex) {
log("Error logging body - got ConcurrentModificationException");
}
Charset charset = UTF8;
MediaType contentType = requestBody.contentType();
if (contentType != null) {
charset = contentType.charset(UTF8);
}
log(BODY);
if (isPlaintext(buffer)) {
log(buffer.readString(charset));
log(REQUEST_END + request.method() + " (" + requestBody.contentLength() + "-byte body)");
} else {
log("<" + requestBody.contentLength() + "-BYTE BINARY BODY OMITTED");
log(REQUEST_END + request.method());
}
}
long startNs = System.nanoTime();
Response response;
try {
response = chain.proceed(request);
} catch (Exception e) {
log("<---- HTTP FAILED: " + e);
throw e;
}
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
ResponseBody responseBody = response.body();
long contentLength = responseBody.contentLength();
String bodySize = contentLength != -1 ? contentLength + "-byte" : "unknown-length";
log(RESPONSE_START + reqUrl);
log("(" + tookMs + "ms" + (!logHeaders ? ", " + bodySize + " body" : "") + ")");
if (logHeaders) {
Headers headers = response.headers();
for (int i = 0, count = headers.size(); i < count; i++) {
log(getHeaderString(headers.name(i), headers.value(i)));
}
}
if (!logBody) {
log(RESPONSE_END);
} else if (!HttpHeaders.hasBody(response)) {
log(BODY);
log(NO_BODY);
log(RESPONSE_END);
} else if (bodyEncoded(response.headers())) {
log(BODY);
log("<ENCODED BODY OMITTED>");
log(RESPONSE_END);
} else {
BufferedSource source = responseBody.source();
// Buffer the entire body.
source.request(Long.MAX_VALUE);
Buffer buffer = source.buffer();
Charset charset = UTF8;
MediaType contentType = responseBody.contentType();
if (contentType != null) {
try {
charset = contentType.charset(UTF8);
} catch (UnsupportedCharsetException e) {
log(BODY);
log("<ERROR DECODING BODY; CHARSET IS LIKELY MALFORMED.>");
log(RESPONSE_END);
return response;
}
}
if (!isPlaintext(buffer)) {
log(BODY);
log("<BINARY " + buffer.size() + "-BYTE BODY OMITTED>");
log(RESPONSE_END);
return response;
}
if (contentLength != 0) {
log(BODY + " (" + buffer.size() + "-byte body)");
log(buffer.clone().readString(charset));
}
log(RESPONSE_END);
}
return response;
}
use of okhttp3.RequestBody in project FlareBot by FlareBot.
the class WebUtils method post.
public static Response post(String url, MediaType type, String body, boolean sendAPIAuth, boolean compress) throws IOException {
Request.Builder request = new Request.Builder().url(url);
if (sendAPIAuth)
request.addHeader("Authorization", FlareBot.instance().getApiKey());
if (compress)
request.addHeader("Content-Encoding", "gzip");
RequestBody requestBody = RequestBody.create(type, body);
request = request.post(requestBody);
return request(request);
}
Aggregations