Search in sources :

Example 21 with MediaType

use of okhttp3.MediaType in project YhLibraryForAndroid by android-coco.

the class LoggerInterceptor method logForResponse.

private Response logForResponse(Response response, long duration) {
    try {
        // ===>response LogUtils
        LogUtils.e(tag, "============服务器回复'LogUtils============开始");
        Response.Builder builder = response.newBuilder();
        Response clone = builder.build();
        LogUtils.e(tag, "请求url : " + clone.request().url());
        LogUtils.e(tag, "请求code : " + clone.code());
        LogUtils.e(tag, "请求协议protocol : " + clone.protocol());
        if (!TextUtils.isEmpty(clone.message())) {
            LogUtils.e(tag, "是否成功 : " + clone.message());
            if (showResponse) {
                ResponseBody body = clone.body();
                if (body != null) {
                    MediaType mediaType = body.contentType();
                    if (mediaType != null) {
                        LogUtils.e(tag, "响应主体的类型 : " + mediaType.toString());
                        if (isText(mediaType)) {
                            String resp = body.string();
                            LogUtils.e(tag, "响应主体的内容 : " + resp);
                            body = ResponseBody.create(mediaType, resp);
                            LogUtils.e(tag, "============服务器回复'LogUtils==========结束" + duration + "毫秒");
                            return response.newBuilder().body(body).build();
                        } else {
                            LogUtils.e(tag, "响应主体的内容 : " + " 也许[文件部分],太大了太大了,忽略了!");
                        }
                    }
                }
            }
        }
        LogUtils.e(tag, "============服务器回复'LogUtils==========结束" + duration + "毫秒");
    } catch (Exception e) {
        LogUtils.e(tag, "============服务器回复打印异常:" + e.getMessage());
    }
    return response;
}
Also used : Response(okhttp3.Response) MediaType(okhttp3.MediaType) IOException(java.io.IOException) ResponseBody(okhttp3.ResponseBody)

Example 22 with MediaType

use of okhttp3.MediaType in project YhLibraryForAndroid by android-coco.

the class LoggerInterceptor method logForRequest.

private void logForRequest(Request request) {
    try {
        String url = request.url().toString();
        Headers headers = request.headers();
        LogUtils.e(tag, "============请求'LogUtils=====开始");
        LogUtils.e(tag, "请求方式 : " + request.method());
        LogUtils.e(tag, "请求URL : " + url);
        if (headers != null && headers.size() > 0) {
            LogUtils.e(tag, "请求头 : " + headers.toString());
        }
        RequestBody requestBody = request.body();
        if (requestBody != null) {
            MediaType mediaType = requestBody.contentType();
            if (mediaType != null) {
                LogUtils.e(tag, "请求参数类型 : " + mediaType);
                LogUtils.e(tag, "请求参数内容 : " + bodyToString(request).replace("&", "   "));
            // if (isText(mediaType))
            // {
            // LogUtils.e(tag, "请求参数内容 : " + bodyToString(request));
            // } else
            // {
            // LogUtils.e(tag, "请求参数内容 : " + " 也许[文件部分],太大了太大了,忽略了!");
            // }
            }
        }
        LogUtils.e(tag, "============请求'LogUtils============结束");
    } catch (Exception e) {
    // e.printStackTrace();
    }
}
Also used : Headers(okhttp3.Headers) MediaType(okhttp3.MediaType) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Example 23 with MediaType

use of okhttp3.MediaType in project LabDayApp by jakdor.

the class RetrofitBuilderTest method createServiceWithInterceptor.

/**
 * test RetrofitBuilder in isolation from AuthenticationInterceptor
 */
@Test
public void createServiceWithInterceptor() throws Exception {
    Request request = new Request.Builder().header("Authorization", dummyToken).get().url(dummyApiUrl).build();
    ResponseBody dummyResponse = new ResponseBody() {

        @Nullable
        @Override
        public MediaType contentType() {
            return null;
        }

        @Override
        public long contentLength() {
            return 0;
        }

        @Override
        public BufferedSource source() {
            return null;
        }
    };
    when(authenticationInterceptor.intercept(any())).thenReturn(new Response.Builder().request(request).protocol(Protocol.HTTP_2).code(200).body(dummyResponse).message(message).build());
    LabService labService = retrofitBuilder.createService(dummyApiUrl, LabService.class, dummyToken);
    TestObserver<AppData> testObserver = labService.getAppData().test();
    testObserver.assertSubscribed();
    testObserver.onComplete();
    InOrder order = inOrder(authenticationInterceptor);
    order.verify(authenticationInterceptor, calls(1)).setAuthToken(dummyToken);
    order.verify(authenticationInterceptor, calls(1)).intercept(any());
}
Also used : Response(okhttp3.Response) LabService(com.jakdor.labday.common.network.LabService) InOrder(org.mockito.InOrder) AppData(com.jakdor.labday.common.model.AppData) RetrofitBuilder(com.jakdor.labday.common.network.RetrofitBuilder) Request(okhttp3.Request) ResponseBody(okhttp3.ResponseBody) Test(org.junit.Test)

Example 24 with MediaType

use of okhttp3.MediaType in project BaseProject by feer921.

the class HttpLoggingInterceptor method bodyToString.

private void bodyToString(Request request) {
    try {
        final Request copy = request.newBuilder().build();
        final Buffer buffer = new Buffer();
        copy.body().writeTo(buffer);
        Charset charset = UTF8;
        MediaType contentType = copy.body().contentType();
        if (contentType != null) {
            charset = contentType.charset(UTF8);
        }
        log("\tbody:" + buffer.readString(charset));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Buffer(okio.Buffer) Request(okhttp3.Request) Charset(java.nio.charset.Charset) MediaType(okhttp3.MediaType) IOException(java.io.IOException)

Example 25 with MediaType

use of okhttp3.MediaType in project android-client by GenesisVision.

the class OAuthOkHttpClient method execute.

public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers, String requestMethod, Class<T> responseClass) throws OAuthSystemException, OAuthProblemException {
    MediaType mediaType = MediaType.parse("application/json");
    Request.Builder requestBuilder = new Request.Builder().url(request.getLocationUri());
    if (headers != null) {
        for (Entry<String, String> entry : headers.entrySet()) {
            if (entry.getKey().equalsIgnoreCase("Content-Type")) {
                mediaType = MediaType.parse(entry.getValue());
            } else {
                requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }
    }
    RequestBody body = request.getBody() != null ? RequestBody.create(mediaType, request.getBody()) : null;
    requestBuilder.method(requestMethod, body);
    try {
        Response response = client.newCall(requestBuilder.build()).execute();
        return OAuthClientResponseFactory.createCustomResponse(response.body().string(), response.body().contentType().toString(), response.code(), responseClass);
    } catch (IOException e) {
        throw new OAuthSystemException(e);
    }
}
Also used : OAuthClientResponse(org.apache.oltu.oauth2.client.response.OAuthClientResponse) Response(okhttp3.Response) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) Request(okhttp3.Request) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) MediaType(okhttp3.MediaType) IOException(java.io.IOException) RequestBody(okhttp3.RequestBody)

Aggregations

MediaType (okhttp3.MediaType)99 RequestBody (okhttp3.RequestBody)86 Request (okhttp3.Request)82 Response (okhttp3.Response)74 IOException (java.io.IOException)67 ResponseBody (okhttp3.ResponseBody)35 Buffer (okio.Buffer)30 BufferedSink (okio.BufferedSink)28 Charset (java.nio.charset.Charset)25 Headers (okhttp3.Headers)19 BufferedSource (okio.BufferedSource)19 Map (java.util.Map)17 OkHttpClient (okhttp3.OkHttpClient)15 HashMap (java.util.HashMap)12 InputStream (java.io.InputStream)11 MultipartBody (okhttp3.MultipartBody)11 MockResponse (okhttp3.mockwebserver.MockResponse)11 EOFException (java.io.EOFException)10 List (java.util.List)9 Connection (okhttp3.Connection)9