我调试时,OkHttp响应不起作用

时间:2017-04-18 03:42:08

标签: android debugging okhttp

调试时,响应会出现问题:java.lang.IllegalStateException: closed

@Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        logForRequest(request);
        Response response = chain.proceed(request);
        return logForResponse(response);
    }


   private void logForRequest(Request request) {
    try {

        String url = request.url().toString();
        String method;
        String header = null;
        String contentType = null;
        String content = null;

        Headers headers = request.headers();
        method = "method : " + request.method();
        if (headers != null && headers.size() > 0) {
            header = "headers : " + headers.toString();
        }
        RequestBody requestBody = request.body();
        if (requestBody != null) {
            MediaType mediaType = requestBody.contentType();
            if (mediaType != null) {
                contentType = "requestBody's contentType : " + mediaType.toString();
                if (isText(mediaType)) {
                    content = "requestBody's content : " + bodyToString(request);
                } else {
                    content = "requestBody's content : " + " maybe [file part] , too large too print , ignored!";
                }
            }
        }
        Logger.t(TAG + "-REQUEST").i(url + "\n" + method + "\n" + header + "\n" + contentType
                + "\n" + content + "\n");
    } catch (Throwable e) {
        Logger.t(TAG + "-REQUEST-EXCEPTION").e(e + " request exception");
    }
}


    private Response logForResponse(Response response) {
    try {

        String url;
        String code;
        String protocol;
        String message = null;
        String contentType;
        String content;
        String header = null;
        //===>response log
        Response.Builder builder = response.newBuilder();
        Response clone = builder.build();
        Headers headers = clone.headers();
        url = "url : " + clone.request().url();
        code = "code : " + clone.code();
        protocol = "protocol : " + clone.protocol();


        if (headers != null && headers.size() > 0) {
            header = "headers :" + headers.toString();
        }
        if (!TextUtils.isEmpty(clone.message()))
            message = "message : " + clone.message();

        ResponseBody body = clone.body();
        if (body != null) {
            MediaType mediaType = body.contentType();
            if (mediaType != null) {
                contentType = "responseBody's contentType : " + mediaType.toString();
                if (isText(mediaType)) {
                    String resp = body.string();


                    content = "responseBody's content : " + resp;
                    Logger.t(TAG + "-RESPONSE").i(url + "\n" + code + "\n" + protocol + "\n" + message + "\n"
                            + contentType + "\n" + content + "\n" + header + "\n");
                    body = ResponseBody.create(mediaType, resp);
                    return response.newBuilder().body(body).build();
                } else {
                    content = "responseBody's content : " + " maybe [file part] , too large too print , ignored!";
                    Logger.t(TAG + "-RESPONSE").i(url + "\n" + code + "\n" + protocol + "\n" + message + "\n"
                            + contentType + "\n" + content + "\n");
                }
            } else {
                content = "responseBody's content : " + body.string();
                Logger.t(TAG + "-RESPONSE").i(url + "\n" + code + "\n" + protocol + "\n" + message + "\n" + content + "\n");
            }
        }

    } catch (Throwable e) {
        Logger.t(TAG + "-RESPONSE-EXCEPTION").e(e, "response exception");
    }

    return response;
}

1 个答案:

答案 0 :(得分:-1)

有一个github论坛,所以你可以从这里获得帮助 https://github.com/square/okhttp/issues/1240

尝试在某些更改后使用代码,如:

if (body != null) {
    String resp = body.string();
    ResponseBody body1;
            MediaType mediaType = body.contentType();
            if (mediaType != null) {
                contentType = "responseBody's contentType : " + mediaType.toString();
                if (isText(mediaType)) {
                    content = "responseBody's content : " + resp;
                    Logger.t(TAG + "-RESPONSE").i(url + "\n" + code + "\n" + protocol + "\n" + message + "\n"
                            + contentType + "\n" + content + "\n" + header + "\n");
                    body1 = ResponseBody.create(mediaType, resp);
                    return response.newBuilder().body(body1).build();
                } else {
                    content = "responseBody's content : " + " maybe [file part] , too large too print , ignored!";
                    Logger.t(TAG + "-RESPONSE").i(url + "\n" + code + "\n" + protocol + "\n" + message + "\n"
                            + contentType + "\n" + content + "\n");
                }
            } else {
                content = "responseBody's content : " + body.string();
                Logger.t(TAG + "-RESPONSE").i(url + "\n" + code + "\n" + protocol + "\n" + message + "\n" + content + "\n");
            }
        }