我用Retrofit 2,okhttp和okhttp:logging-interceptor创建了项目.
private static APIInterface apiInterface; private static RestClient restClient; private static HttpLoggingInterceptor interceptor; OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setConnectTimeout(30, TimeUnit.SECONDS); okHttpClient.setReadTimeout(30, TimeUnit.SECONDS); okHttpClient.interceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request original = chain.request(); Request.Builder requestBuilder = original.newBuilder() .header("Accept", "application/json") .header("X-Parse-Application-Id", Constants.PARSE_APP_ID) .header("X-Parse-REST-API-Key", Constants.PARSE_REST_API) .method(original.method(), original.body()); Request request = requestBuilder.build(); return chain.proceed(request); } }); interceptor = new HttpLoggingInterceptor(); // got crash here interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); okHttpClient.interceptors().add(interceptor);
这是我的踪迹:
java.lang.VerifyError: com/squareup/okhttp/logging/HttpLoggingInterceptor at com.rocker.rest.RestClient.setupRestClient(RestClient.java:62) at com.rocker.rest.RestClient.(RestClient.java:39) at com.rocker.fragment.HistoryFragment.onCreateView(HistoryFragment.java:38)
我没有使用平方的okio!
你读过这个吗? https://futurestud.io/blog/retrofit-2-log-requests-and-responses
Retrofit 2完全依赖OkHttp进行任何网络操作。由于OkHttp是Retrofit 2的对等依赖项,因此,将Retrofit 2作为稳定版本发布后,您无需添加其他依赖项。
OkHttp 2.6.0附带日志拦截器作为内部依赖项,您可以将其直接用于Retrofit客户端。改造2.0.0-beta2仍使用OkHttp 2.5.0。将来的发行版将使对更高的OkHttp版本的依赖性增加。因此,您需要手动导入日志记录拦截器。将以下行添加到build.gradle文件中的gradle导入中,以获取日志记录拦截器依赖项。
编译'com.squareup.okhttp:logging-interceptor:2.6.0'