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;
}
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();
}
}
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());
}
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();
}
}
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);
}
}
Aggregations