我正在尝试让我的网络类记录传入的JSON,我想使用OkHttp3中的HttpLoggingInterceptor . 问题是我的Retrofit对象不会从okhttp3获取OkHttpClient . 如果你知道我做错了什么,请告诉我 .
摇篮
compile 'com.squareup.retrofit:retrofit:2.0.0-beta4'
compile 'com.squareup.okhttp3:okhttp:3.1.2'
compile 'com.squareup.retrofit:converter-jackson:2.0.0-beta2'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
改造和客户端设置
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit.Retrofit;
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
retrofit = new Retrofit.Builder().baseUrl(URL).client(client).addConverterFactory(JacksonConverterFactory.create()).build();
API = retrofit.create(com.glasshouse.glasshouse.Network.API.class);
Android工作室说Retrofit需要okhttp.OkHttpClient而不是okhttp3.OkHttpClient但如果使用那个,我不能使用HttpLoggingInterceptor ...
谢谢你的回答 .
3 回答
作为其他人的参考,您需要在您的gradle中编译“com.squareup.retrofit2”而不是“com.squareup.retrofit” .
是的,没关系 - 对于那些面临类似问题的人,使用改造2而不是改造
(需要导入retrofit2.Retrofit不能导入retrofit.Retrofit)
我使用以下配置进行改造,效果很好 .
在build.gradle中:
改造配置: