Search in sources :

Example 1 with URLHttpResponse

use of com.kymjs.rxvolley.http.URLHttpResponse in project yoo_home_Android by culturer.

the class OkHttpStack method performRequest.

@Override
public URLHttpResponse performRequest(Request<?> request, ArrayList<HttpParamsEntry> additionalHeaders) throws IOException {
    OkHttpClient client = mClient.clone();
    int timeoutMs = request.getTimeoutMs();
    client.setConnectTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setReadTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    client.setWriteTimeout(timeoutMs, TimeUnit.MILLISECONDS);
    com.squareup.okhttp.Request.Builder okHttpRequestBuilder = new com.squareup.okhttp.Request.Builder();
    okHttpRequestBuilder.url(request.getUrl());
    for (final HttpParamsEntry entry : request.getHeaders()) {
        okHttpRequestBuilder.addHeader(entry.k, entry.v);
    }
    for (final HttpParamsEntry entry : additionalHeaders) {
        okHttpRequestBuilder.addHeader(entry.k, entry.v);
    }
    setConnectionParametersForRequest(okHttpRequestBuilder, request);
    com.squareup.okhttp.Request okHttpRequest = okHttpRequestBuilder.build();
    Call okHttpCall = client.newCall(okHttpRequest);
    Response okHttpResponse = okHttpCall.execute();
    return responseFromConnection(okHttpResponse);
}
Also used : Response(com.squareup.okhttp.Response) URLHttpResponse(com.kymjs.rxvolley.http.URLHttpResponse) Call(com.squareup.okhttp.Call) OkHttpClient(com.squareup.okhttp.OkHttpClient) Request(com.kymjs.rxvolley.http.Request) HttpParamsEntry(com.kymjs.rxvolley.toolbox.HttpParamsEntry)

Example 2 with URLHttpResponse

use of com.kymjs.rxvolley.http.URLHttpResponse in project yoo_home_Android by culturer.

the class OkHttpStack method responseFromConnection.

private URLHttpResponse responseFromConnection(Response okHttpResponse) throws IOException {
    URLHttpResponse response = new URLHttpResponse();
    // contentStream
    int responseCode = okHttpResponse.code();
    if (responseCode == -1) {
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    response.setResponseCode(responseCode);
    response.setResponseMessage(okHttpResponse.message());
    response.setContentStream(okHttpResponse.body().byteStream());
    response.setContentLength(okHttpResponse.body().contentLength());
    response.setContentEncoding(okHttpResponse.header("Content-Encoding"));
    if (okHttpResponse.body().contentType() != null) {
        response.setContentType(okHttpResponse.body().contentType().type());
    }
    // header
    HashMap<String, String> headerMap = new HashMap<>();
    Headers responseHeaders = okHttpResponse.headers();
    for (int i = 0, len = responseHeaders.size(); i < len; i++) {
        final String name = responseHeaders.name(i), value = responseHeaders.value(i);
        if (name != null) {
            headerMap.put(name, value);
        }
    }
    response.setHeaders(headerMap);
    return response;
}
Also used : URLHttpResponse(com.kymjs.rxvolley.http.URLHttpResponse) HashMap(java.util.HashMap) Headers(com.squareup.okhttp.Headers) IOException(java.io.IOException)

Example 3 with URLHttpResponse

use of com.kymjs.rxvolley.http.URLHttpResponse in project yoo_home_Android by culturer.

the class OkHttpStack method responseFromConnection.

private URLHttpResponse responseFromConnection(Response okHttpResponse) throws IOException {
    URLHttpResponse response = new URLHttpResponse();
    // contentStream
    int responseCode = okHttpResponse.code();
    if (responseCode == -1) {
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    response.setResponseCode(responseCode);
    response.setResponseMessage(okHttpResponse.message());
    response.setContentStream(okHttpResponse.body().byteStream());
    response.setContentLength(okHttpResponse.body().contentLength());
    response.setContentEncoding(okHttpResponse.header("Content-Encoding"));
    if (okHttpResponse.body().contentType() != null) {
        response.setContentType(okHttpResponse.body().contentType().type());
    }
    // header
    HashMap<String, String> headerMap = new HashMap<>();
    Headers responseHeaders = okHttpResponse.headers();
    for (int i = 0, len = responseHeaders.size(); i < len; i++) {
        final String name = responseHeaders.name(i), value = responseHeaders.value(i);
        if (name != null) {
            headerMap.put(name, value);
        }
    }
    response.setHeaders(headerMap);
    return response;
}
Also used : URLHttpResponse(com.kymjs.rxvolley.http.URLHttpResponse) HashMap(java.util.HashMap) Headers(okhttp3.Headers) IOException(java.io.IOException)

Example 4 with URLHttpResponse

use of com.kymjs.rxvolley.http.URLHttpResponse in project yoo_home_Android by culturer.

the class OkHttpStack method performRequest.

@Override
public URLHttpResponse performRequest(Request<?> request, ArrayList<HttpParamsEntry> additionalHeaders) throws IOException {
    int timeoutMs = request.getTimeoutMs();
    OkHttpClient client = mClient.newBuilder().readTimeout(timeoutMs, TimeUnit.MILLISECONDS).connectTimeout(timeoutMs, TimeUnit.MILLISECONDS).writeTimeout(timeoutMs, TimeUnit.MILLISECONDS).build();
    okhttp3.Request.Builder okHttpRequestBuilder = new okhttp3.Request.Builder();
    okHttpRequestBuilder.url(request.getUrl());
    for (final HttpParamsEntry entry : request.getHeaders()) {
        okHttpRequestBuilder.addHeader(entry.k, entry.v);
    }
    for (final HttpParamsEntry entry : additionalHeaders) {
        okHttpRequestBuilder.addHeader(entry.k, entry.v);
    }
    setConnectionParametersForRequest(okHttpRequestBuilder, request);
    okhttp3.Request okHttpRequest = okHttpRequestBuilder.build();
    Call okHttpCall = client.newCall(okHttpRequest);
    Response okHttpResponse = okHttpCall.execute();
    return responseFromConnection(okHttpResponse);
}
Also used : Response(okhttp3.Response) URLHttpResponse(com.kymjs.rxvolley.http.URLHttpResponse) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Request(com.kymjs.rxvolley.http.Request) HttpParamsEntry(com.kymjs.rxvolley.toolbox.HttpParamsEntry)

Aggregations

URLHttpResponse (com.kymjs.rxvolley.http.URLHttpResponse)4 Request (com.kymjs.rxvolley.http.Request)2 HttpParamsEntry (com.kymjs.rxvolley.toolbox.HttpParamsEntry)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Call (com.squareup.okhttp.Call)1 Headers (com.squareup.okhttp.Headers)1 OkHttpClient (com.squareup.okhttp.OkHttpClient)1 Response (com.squareup.okhttp.Response)1 Call (okhttp3.Call)1 Headers (okhttp3.Headers)1 OkHttpClient (okhttp3.OkHttpClient)1 Response (okhttp3.Response)1