App crash on HttpLoggingInterceptor

喜夏-厌秋 提交于 2019-12-19 06:53:36

问题


I've created project with Retrofit 2, okhttp and 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);

Here is my trace:

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)

I'm not using okio by squareup!


回答1:


Have you read this? 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'



来源:https://stackoverflow.com/questions/34316028/app-crash-on-httplogginginterceptor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!