我正在使用Rxjava错误处理,
默认RxJavaCallAdapterFactory
可以处理许多错误,
但是我的FCM服务器已经死亡,我仍然会崩溃。
这是
/**
* Created by Administrator on 16/09/2016.
* In Dagger 2 frameworks.
* Network Module for implement Retrofit + Gson structure
*/
@Module
public class NetworkModule {
private final String baseURL;
public NetworkModule(String baseURL) {
this.baseURL = baseURL;
}
@Singleton
@Provides
FCMApi provideFCMApi(@Named("fcm") Retrofit retrofit) {
return retrofit.create(FCMApi.class);
}
@Singleton
@Provides
@Named("fcm")
Retrofit provideFCMRetrofit(@Named("fcm") OkHttpClient okHttpClient, Context context) {
return new Retrofit.Builder()
.baseUrl(context.getString(R.string.host_fcm))
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
// TODO CRASH IN HERE, IF ENABLE, HOW TO AVOID THIS RXJAVA ERROR HANDLING WITHOUT RUNTIME WARNING
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
}
@Singleton
@Provides
@Named("fcm")
OkHttpClient getOkHttpClientForFCM(Context context, @Named("fcm") Interceptor interceptor) {
OkHttpClient.Builder okClientBuilder = new OkHttpClient.Builder();
okClientBuilder.addInterceptor(interceptor);
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel( HttpLoggingInterceptor.Level.BASIC);
// Can be Level.BASIC, Level.HEADERS, or Level.BODY
// See http://square.github.io/okhttp/3.x/logging-interceptor/ to see the options.
okClientBuilder.addInterceptor(httpLoggingInterceptor);
}
final File baseDir = context.getCacheDir();
if (baseDir != null) {
final File cacheDir = new File(baseDir, "HttpResponseCache");
okClientBuilder.cache(new Cache(cacheDir, 50 * 1024 * 1024));
}
return okClientBuilder.build();
}
@Singleton @Provides @Named("fcm")
Interceptor provideInterceptorForFCM(final Context context) {
return chain -> {
/**
* Add header with following attributes as agree with Web Server
* - Token
*/
Request original = chain.request();
// Add header to request
Request.Builder requestBuilder = original.newBuilder();
requestBuilder
.addHeader("X-Auth-Token", context.getString(R.string.x_auth_token))
.addHeader("X-Asukabu-Client-Device-ID", getAndroidId(context));
Request request = requestBuilder.build();
return chain.proceed(request);
};
}
知道如何避免此次崩溃的人,
或者如果启用,可以跳过Retrofit 2中的错误处理,
请告诉我,
谢谢,
p / s:我使用的是Dagger2。
答案 0 :(得分:1)
rxJava中有方法.onErrorReturn(),你可以返回空对象而不是错误