应用程序在 HttpLoggingInterceptor 上崩溃

App crash on HttpLoggingInterceptor

我使用 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.<clinit>(RestClient.java:39)
   at com.rocker.fragment.HistoryFragment.onCreateView(HistoryFragment.java:38)

我没有使用 squareup 的 okio!

你读过这个吗? https://futurestud.io/blog/retrofit-2-log-requests-and-responses

Retrofit 2 completely relies on OkHttp for any network operation. Since OkHttp is a peer dependency of Retrofit 2, you won’t need to add an additional dependency once Retrofit 2 is released as a stable release.

OkHttp 2.6.0 ships with a logging interceptor as an internal dependency and you can directly use it for your Retrofit client. Retrofit 2.0.0-beta2 still uses OkHttp 2.5.0. Future releases will bump the dependency to higher OkHttp versions. That’s why you need to manually import the logging interceptor. Add the following line to your gradle imports within your build.gradle file to fetch the logging interceptor dependency.

compile 'com.squareup.okhttp:logging-interceptor:2.6.0'