- I have defined the classes as below.
- I have used dagger with Retrofit here
What I am trying to do::
I am trying to catch the OfflineException
in the request below how to catch it in Main Activity properly.
RequestInterceptor.java
public class RequestInterceptor implements Interceptor {
ConnectivityManager connectivityManager;
Application mApplication;
@Inject
public RequestInterceptor(Application mApplication) {
this.mApplication = mApplication;
connectivityManager = (ConnectivityManager) mApplication.getSystemService(Context.CONNECTIVITY_SERVICE);
}
@Override
public Response intercept(Chain chain) throws IOException {
if (isConnected()) {
throw new OfflineException();
}
Request.Builder r = chain.request().newBuilder();
return chain.proceed(r.build());
}
protected boolean isConnected() {
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnectedOrConnecting();
}
public class OfflineException extends IOException {
@Override
public String getMessage() {
return mApplication.getResources().getString(R.string.app_no_connectivity);
}
}
}
NetModule.java
import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.concurrent.TimeUnit;
import javax.inject.Singleton;
import commons.LinksAndKeys;
import dagger.Module;
import dagger.Provides;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofitDagger.retrofitUtils.RequestInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
@Module
public class NetModule {
String mBaseUrl;
Application mApplication;
// Constructor needs one parameter to instantiate.
public NetModule(String baseUrl, Application application) {
this.mBaseUrl = baseUrl;
this.mApplication = application;
}
// Dagger will only look for methods annotated with @Provides
@Provides
@Singleton
// Application reference must come from AppModule.class
SharedPreferences providesSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}
@Provides
@Singleton
Application providesApplication() {
return mApplication;
}
@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
}
@Provides
@Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
return gsonBuilder.create();
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient(Cache cache) {
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.cache(cache);
client.addInterceptor(new RequestInterceptor(mApplication));
client.readTimeout(LinksAndKeys.READ_TIMEOUT, TimeUnit.SECONDS);
client.connectTimeout(LinksAndKeys.CONNECTION_TIMEOUT, TimeUnit.SECONDS);
return client.build();
}
@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(mBaseUrl)
.client(okHttpClient)
.build();
return retrofit;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Inject
Retrofit retrofit;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((App) getApplication()).getNetComponent().inject(this);
//Create textview and findViewByID
textView = (TextView) findViewById(R.id.textview_post);
//Create a retrofit call object
Call<List<Post>> posts = retrofit.create(Restapi.class).getPosts();
//Enque the call
posts.enqueue(new Callback<List<Post>>() {
@Override
public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
//Set the response to the textview
textView.setText(response.body().get(0).getBody());
}
@Override
public void onFailure(Call<List<Post>> call, Throwable t) {
//Set the error to the textview
textView.setText(t.toString());
}
});
}
}