use of okhttp3.MediaType in project WeexErosFramework by bmfe.
the class WeexOkhttp3Interceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
String requestId = String.valueOf(mNextRequestId.getAndIncrement());
Request request = chain.request();
mEventReporter = NetworkEventReporterManager.get();
RequestBodyHelper requestBodyHelper = null;
if (mEventReporter != null) {
requestBodyHelper = new RequestBodyHelper(mEventReporter, requestId);
OkHttpInspectorRequest inspectorRequest = new OkHttpInspectorRequest(requestId, request, requestBodyHelper);
mEventReporter.requestWillBeSent(inspectorRequest);
}
Response response;
try {
response = chain.proceed(request);
} catch (IOException e) {
if (mEventReporter != null) {
mEventReporter.httpExchangeFailed(requestId, e.toString());
}
throw e;
}
if (mEventReporter != null) {
if (requestBodyHelper.hasBody()) {
requestBodyHelper.reportDataSent();
}
Connection connection = chain.connection();
mEventReporter.responseHeadersReceived(new OkHttpInspectorResponse(requestId, request, response));
ResponseBody body = response.body();
MediaType contentType = null;
InputStream responseStream = null;
if (body != null) {
contentType = body.contentType();
responseStream = body.byteStream();
}
responseStream = mEventReporter.interpretResponseStream(requestId, contentType != null ? contentType.toString() : null, response.header("Content-Encoding"), responseStream, new DefaultResponseHandler(mEventReporter, requestId));
if (responseStream != null) {
response = response.newBuilder().body(new ForwardingResponseBody(body, responseStream)).build();
}
}
return response;
}
use of okhttp3.MediaType in project WeexErosFramework by bmfe.
the class AxiosManager method createRequestBodyByMediaType.
private RequestBody createRequestBodyByMediaType(Map<String, String> header, String content) {
if (header != null && !TextUtils.isEmpty(header.get("Content-Type"))) {
String s = header.get("Content-Type");
MediaType mediaType = null;
try {
mediaType = MediaType.parse(s);
} catch (Exception e) {
e.printStackTrace();
}
if (mediaType == null) {
mediaType = MediaType.parse(DEFAULT_MEDIATYPE);
}
return RequestBody.create(mediaType, content);
}
return RequestBody.create(MediaType.parse(DEFAULT_MEDIATYPE), content);
}
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 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 OkHttpRequestManager method postString.
@Override
public void postString(final String host, final String suffix, Map<String, String> headers, final String params, final HttpCallBack callback, final Object tag, final long connTimeOut, final long readTimeOut, final long writeTimeOut) {
final String url = host + suffix;
// 组成的URL 为空或者不是http或https开头都是非法URL
if (isEmpty(url) || (!url.startsWith(Constants.FILE_HTTP) && !url.startsWith(Constants.FILE_HTTPS))) {
callback.onFailure(-3, "非法URL");
callback.onFinish();
return;
}
if (StringUtils.isEmpty(headers)) {
headers = this.headers;
}
OkHttpUtils.postString().url(url).headers(headers).tag(tag).content(params).mediaType(MediaType.parse("application/json; charset=utf-8")).build().connTimeOut(connTimeOut).readTimeOut(readTimeOut).writeTimeOut(writeTimeOut).execute(new StringCallback() {
@Override
public void onResponse(String response, int id) {
try {
JSONObject json = new JSONObject(response);
String ret = json.getString(Constants.RESULT);
if (ERROE_3001.equals(ret) || ERROE_3002.equals(ret)) {
// 相关操作
// EventBus.getDefault().post(
// new EventBusBean(new Intent(
// AppConfig.ACTION_PLOGIN_OUT)));
// EventBus.getDefault().post(
// new EventBusBean(new Intent(
// AppConfig.ACTION_AGAIN_TOKEN)));
} else {
callback.onSuccess(response);
callback.onFinish();
}
} catch (JSONException e) {
callback.onSuccess(response);
callback.onFinish();
}
}
@Override
public void onError(Call call, Exception e, int id) {
// + e + " " + id);
if (!isEmpty(e)) {
if (e instanceof java.net.SocketTimeoutException) {
callback.onFailure(-5, "请求超时");
callback.onFinish();
} else if (e instanceof java.net.ConnectException) {
callback.onFailure(-4, "连接超时");
callback.onFinish();
} else {
dealError(e, callback);
}
}
}
});
}
Aggregations