Search in sources :

Example 81 with MediaType

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;
}
Also used : InputStream(java.io.InputStream) Request(okhttp3.Request) Connection(okhttp3.Connection) IOException(java.io.IOException) DefaultResponseHandler(com.taobao.weex.devtools.inspector.network.DefaultResponseHandler) ResponseBody(okhttp3.ResponseBody) Response(okhttp3.Response) RequestBodyHelper(com.taobao.weex.devtools.inspector.network.RequestBodyHelper) MediaType(okhttp3.MediaType)

Example 82 with MediaType

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);
}
Also used : MediaType(okhttp3.MediaType) IrregularUrlException(com.eros.framework.http.okhttp.exception.IrregularUrlException) IOException(java.io.IOException)

Example 83 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 84 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 85 with MediaType

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);
                }
            }
        }
    });
}
Also used : Call(okhttp3.Call) JSONObject(org.json.JSONObject) StringCallback(org.yh.library.okhttp.callback.StringCallback) JSONException(org.json.JSONException) IOException(java.io.IOException) JSONException(org.json.JSONException)

Aggregations

MediaType (okhttp3.MediaType)103 RequestBody (okhttp3.RequestBody)95 Request (okhttp3.Request)86 Response (okhttp3.Response)78 IOException (java.io.IOException)73 ResponseBody (okhttp3.ResponseBody)36 BufferedSink (okio.BufferedSink)34 Buffer (okio.Buffer)30 Charset (java.nio.charset.Charset)26 Headers (okhttp3.Headers)19 BufferedSource (okio.BufferedSource)19 Map (java.util.Map)17 OkHttpClient (okhttp3.OkHttpClient)17 InputStream (java.io.InputStream)12 HashMap (java.util.HashMap)12 MultipartBody (okhttp3.MultipartBody)12 MockResponse (okhttp3.mockwebserver.MockResponse)11 EOFException (java.io.EOFException)10 GzipSink (okio.GzipSink)10 JSONObject (org.json.JSONObject)10