打印Retrofit请求地址,参数,以及返回结果

时间:2025-01-27 07:19:04

当然,肯定是通过添加拦截器的方式来了

public class CustomLogInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = ();
        Response response = (request);
        printInfo(request, response);
        return response;
    }

    private void printInfo(Request request, Response response) {
        StringBuilder builder = new StringBuilder();
            String logInfo = "START HTTP->"
                    .concat("请求方法-->:")
                    .concat(())
                    .concat(" ")
                    .concat(().toString())
                    .concat(" \r\n ")
                    .concat("请求头-->:")
                    .concat(getRequestHeaders(request))
                    .concat(" \r\n ")
                    .concat("请求参数-->:")
                    .concat(getRequestParams(request))
                    .concat(" \r\n ")
                    .concat("返回结果-->:")
                    .concat(getResponseText(response))
                    .concat(" \r\n ")
                    .concat("END HTTP->");

            ("network",logInfo);
    }

    private String getResponseText(Response response) {
        String str = "Empty!";
        try {
            ResponseBody body = ();
            if (body != null && () != 0) {
                BufferedSource source = ();
                (Long.MAX_VALUE);
                Buffer buffer = ();
                MediaType mediaType = ();
                if (mediaType != null) {
                    @SuppressWarnings("CharsetObjectCanBeUsed") Charset charset = (
                            ("UTF-8"));
                    if (charset != null) {
                        str = ().readString(charset);
                    }
                }
            }
        } catch (Exception e) {
            ();
        }
        return str;
    }

    private String getRequestParams(Request request) {
        String str = "Empty!";
        try {
            RequestBody body = ();
            if (body != null) {
                Buffer buffer = new Buffer();
                (buffer);
                MediaType mediaType = ();
                if (mediaType != null) {
                    @SuppressWarnings("CharsetObjectCanBeUsed") Charset charset = (
                            ("UTF-8"));
                    if (charset != null) {
                        str = (charset);
                    } else {
                        str = buffer.readUtf8();
                    }
                } else {
                    str = buffer.readUtf8();
                }
            }
        } catch (Exception e) {
            ();
        }
        return str;
    }

    private String getRequestHeaders(Request request) {
        Headers headers = ();
        String str = "Empty!";
        StringBuilder builder = new StringBuilder();
        if (() > 0) {
            for (int i = 0, count = (); i < count; i++) {
                ((i) + ": " + (i)).append(" \r\n ");
            }
            return ();
        } else {
            return "Empty!";
        }
    }
}

之后把这个拦截器添加到

 okHttpClient = new ();
(new CustomLogInterceptor());//添加其他设置也是通过OkHttpClient来添加的

最后我们把okhttpclient放到Retrofit中即可

 retrofit = new ().baseUrl(//请求头).client(());

这样就可以把我们需要看到的东西给打印出来了.