HttpLoggingInterceptor上的应用程序崩溃 [英] App crash on HttpLoggingInterceptor

查看:300
本文介绍了HttpLoggingInterceptor上的应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Retrofit 2,okhttp和okhttp:logging-interceptor创建了项目.

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);

这是我的踪迹:

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)

我不是在用okio by squareup!

I'm not using okio by squareup!

推荐答案

您已经阅读了吗? https://futurestud.io/blog/retrofit-2-log-requests和回应

Retrofit 2完全依赖OkHttp进行任何网络操作. 由于OkHttp是Retrofit 2的对等依赖项,因此您无需添加 Retrofit 2作为稳定版本发布后,会产生额外的依赖关系 释放.

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附带一个日志拦截器作为内部 依赖关系,您可以将其直接用于Retrofit客户. 改造2.0.0-beta2仍使用OkHttp 2.5.0.将来的版本将 将依赖性提高到更高的OkHttp版本.这就是为什么你需要 手动导入日志记录拦截器.将以下行添加到 您的gradle导入build.gradle文件中以获取日志记录 拦截器依赖性.

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.

编译'com.squareup.okhttp:logging-interceptor:2.6.0'

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

这篇关于HttpLoggingInterceptor上的应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆