Search in sources :

Example 96 with RequestBody

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;
}
Also used : IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 97 with RequestBody

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);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function) BuildDetails(com.khmelenko.lab.varis.network.response.BuildDetails) NonNull(io.reactivex.annotations.NonNull) RequestBody(okhttp3.RequestBody)

Example 98 with RequestBody

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);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) Function(io.reactivex.functions.Function) BuildDetails(com.khmelenko.lab.varis.network.response.BuildDetails) NonNull(io.reactivex.annotations.NonNull) RequestBody(okhttp3.RequestBody)

Example 99 with RequestBody

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;
}
Also used : Buffer(okio.Buffer) ConcurrentModificationException(java.util.ConcurrentModificationException) HttpHeaders(okhttp3.internal.http.HttpHeaders) Headers(okhttp3.Headers) Request(okhttp3.Request) Charset(java.nio.charset.Charset) IOException(java.io.IOException) EOFException(java.io.EOFException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) ConcurrentModificationException(java.util.ConcurrentModificationException) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) MediaType(okhttp3.MediaType) RequestBody(okhttp3.RequestBody) BufferedSource(okio.BufferedSource)

Example 100 with RequestBody

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);
}
Also used : Request(okhttp3.Request) RequestBody(okhttp3.RequestBody)

Aggregations

RequestBody (okhttp3.RequestBody)178 Request (okhttp3.Request)141 IOException (java.io.IOException)75 Response (okhttp3.Response)74 Test (org.junit.Test)53 ResponseBody (okhttp3.ResponseBody)32 Call (okhttp3.Call)31 MultipartBody (okhttp3.MultipartBody)28 MediaType (okhttp3.MediaType)27 FormBody (okhttp3.FormBody)25 Callback (okhttp3.Callback)23 Buffer (okio.Buffer)23 MockResponse (okhttp3.mockwebserver.MockResponse)18 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)16 TestClients.clientRequest (keywhiz.TestClients.clientRequest)15 BufferedSink (okio.BufferedSink)15 Headers (okhttp3.Headers)12 HttpUrl (okhttp3.HttpUrl)11 JSONObject (org.json.JSONObject)11 Body (retrofit2.http.Body)11