Reputation: 53600
This is my first try in developing a class based on Dagger and RxJava. I created a NetworkModule
and it contains following code.
@Module
public class NetworkModule {
private static final long CONNECTION_TIMEOUT = 60 * 1000; // one minute
private static final long CACHE_TIME = 432000; // 5days
private File cacheFile;
public NetworkModule(File cacheFile) {
this.cacheFile = cacheFile;
}
@Provides
@Singleton
Retrofit provideCall() {
return new Retrofit.Builder()
.baseUrl(BuildConfig.VIEWLORIUM_API_URL_BASE)
.client(getHttpClientBuilder())
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
}
private OkHttpClient getHttpClientBuilder() {
// Setup caching file
Cache cache = null;
try {
cache = new Cache(cacheFile, 10 * 1024 * 1024);
} catch (Exception e) {
e.printStackTrace();
}
// Setup Logging interceptor
HttpLoggingInterceptor mLoggingInterceptor = new HttpLoggingInterceptor();
mLoggingInterceptor.setLevel(ViewloriumApp.DEBUG ? HttpLoggingInterceptor.Level.BODY :
HttpLoggingInterceptor.Level.NONE);
// Setup OkHttpClient
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS) //
.addInterceptor(mLoggingInterceptor)
.addNetworkInterceptor(new MyInterceptor())
.cache(cache);
// Add StethoInterceptor to display server requests/responses in chrome://inspect/#devices
if (ViewloriumApp.DEBUG) {
builder.addNetworkInterceptor(new StethoInterceptor());
}
return builder.build();
}
@Provides
@Singleton
@SuppressWarnings("unused")
public NetworkService providesNetworkService(Retrofit retrofit) {
return retrofit.create(NetworkService.class);
}
@Provides
@Singleton
@SuppressWarnings("unused")
public Service providesService(NetworkService networkService) {
return new Service(networkService);
}
private class MyInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
// Customize the request
Request request = original.newBuilder()
.header("Content-Type", "application/json")
.removeHeader("Pragma")
.header("Cache-Control", String.format(Locale.US, "max-age=%d", CACHE_TIME))
.build();
okhttp3.Response response = chain.proceed(request);
response.cacheResponse();
// Customize or return the response
return response;
}
}
}
I want to integrate HttpLoggingInterceptor
into OkHttpClient
. However when I launch the app, I'm getting following error:
E/AndroidRuntime: FATAL EXCEPTION: RxIoScheduler-2
Process: com.my.package, PID: 3105
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:62)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NoSuchMethodError: okhttp3.internal.Platform.log
at okhttp3.logging.HttpLoggingInterceptor$Logger$1.log(HttpLoggingInterceptor.java:109)
at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:157)
at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:190)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:163)
at okhttp3.RealCall.execute(RealCall.java:57)
at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:171)
at rx.internal.operators.OperatorSubscribeOn$1$1$1.request(OperatorSubscribeOn.java:80)
at rx.Subscriber.setProducer(Subscriber.java:211)
at rx.internal.operators.OperatorSubscribeOn$1$1.setProducer(OperatorSubscribeOn.java:76)
at rx.Subscriber.setProducer(Subscriber.java:205)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50)
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
at rx.Observable.unsafeSubscribe(Observable.java:8666)
at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:220)
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
However, When I'm delete (or comment out) the line, .addInterceptor(mLoggingInterceptor)
then application works fine and I'm able to get response from server (but I'm not able to see logs).
This is list of my dependencies
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
// Support Libraries
compile 'com.android.support:support-v4:24.2.1'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
// Network
compile 'com.google.code.gson:gson:2.6.1'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
// RxJava & RxAndroid
compile 'io.reactivex:rxjava:1.1.6'
compile 'io.reactivex:rxandroid:1.2.1'
// View Injection
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
// Dagger
compile 'com.google.dagger:dagger:2.5'
apt 'com.google.dagger:dagger-compiler:2.5'
provided 'javax.annotation:jsr250-api:1.0'
// Logging
compile 'com.jakewharton.timber:timber:3.1.0'
// Image Loading
compile 'com.squareup.picasso:picasso:2.5.2'
// Facebook Stetho
compile 'com.facebook.stetho:stetho-okhttp3:1.3.1'
// Testing
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
}
Upvotes: 1
Views: 3896
Reputation: 1960
mvnrepository reports that retrofit 2.1 depends on okhttp 3.3, so i suggest you to change logging-interceptor version to 3.3 too
Upvotes: 4