我使用Retrofit 2 和 OkHttp3处理来自服务器的数据,在使用Min_SDK 17时出现错误,我的设备的 API也是17
我尝试了这个答案:How to fix Expected Android API level 21+ but was 19 in Android
我也尝试了这个答案https://github.com/square/okhttp/issues/4597#issuecomment-461204144
但我得到同样的错误。
我的 API 客户端
public class APIClient {
private static Retrofit retrofit;
private static OkHttpClient okHttpClient;
public static Retrofit getInstanceRetrofit(){
if(okHttpClient==null) {
initOkHttp();
}
if(retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(Const.URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
private static void initOkHttp() {
int REQUEST_TIMEOUT = 60;
OkHttpClient.Builder httpClient = new OkHttpClient().newBuilder()
.connectTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(REQUEST_TIMEOUT, TimeUnit.SECONDS);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient.addInterceptor(interceptor);
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
.addHeader("Accept", "application/json")
.addHeader("Content-Type", "application/json");
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
okHttpClient = httpClient.build();
}
}
万千封印
慕桂英546537
江户川乱折腾
相关分类