android - 应用程序在 HttpLoggingInterceptor 上崩溃

标签 android retrofit okhttp

我已经使用 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'

关于android - 应用程序在 HttpLoggingInterceptor 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34316028/

相关文章:

android - JsonArray 作为空字符串解析问题与改造

asynchronous - 在 Kotlin 中等待多个回调/lambdas 的结果

java - 使用位置回调获取位置更新

java - 如何处理屏幕方向变化

android - 如何查找 apk 是 32 位还是 64 位

android - 如何将整数 ArrayList 添加到 MultipartBody

android - OkHttp/Android 不返回完整结果

android - 如何使用 gradle 从 jitpack.io 导入库?

android - 像开放API一样通过Retrofit请求

android - 改造 2 : Get JSON from Response body