Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void getMobileDataUsage(OnDatastoreResponse onDatastoreResponse)
- @Singleton
- public class APIClient {
- private static final String TAG = "APIClient";
- private APIInterface apiInterface;
- private Retrofit retrofit;
- @Inject
- public APIClient(Context context) {
- // use 10MB cache
- long cacheSize = 10 * 1024 * 1024;
- Cache cache = new Cache(context.getCacheDir(), cacheSize);
- HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
- interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
- OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).cache(cache).build();
- retrofit = new Retrofit.Builder()
- .baseUrl(BuildConfig.BASE_URL)
- .addConverterFactory(GsonConverterFactory.create())
- .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
- .client(client)
- .build();
- this.apiInterface = retrofit.create(APIInterface.class);
- }
- public void getMobileDataUsage(OnDatastoreResponse onDatastoreResponse) {
- String resourceId = "a807b7ab-6cad-4aa6-87d0-e283a7353a0f";
- Integer limit = null;
- Single<DatastoreResponse> datastoreResponse = apiInterface.getMobileDataUsage(resourceId, limit);
- datastoreResponse.subscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new DisposableSingleObserver<DatastoreResponse>() {
- @Override
- public void onSuccess(DatastoreResponse datastoreResponse) {
- if (datastoreResponse.getSuccess()) {
- Log.d(TAG, "onSuccess: " + datastoreResponse.getSuccess());
- onDatastoreResponse.onSuccessDatastoreResponse(datastoreResponse);
- } else {
- Log.e(TAG, "onSuccess: " + datastoreResponse.getSuccess());
- onDatastoreResponse.onErrorResponse(new Exception("Datastore response not successful"));
- }
- }
- @Override
- public void onError(Throwable e) {
- Log.e(TAG, "onError: " + e.getMessage(), e);
- onDatastoreResponse.onErrorResponse(e);
- }
- });
- }
- }
- @Module
- public class ApplicationContextModule {
- private final Context context;
- public ApplicationContextModule(Context context) {
- this.context = context;
- }
- @Provides
- Context provideApplicationContext() {
- return context;
- }
- }
- @Singleton
- @Component(modules = {ApplicationContextModule.class, DataModule.class})
- public interface ApplicationComponent {
- void inject(MobileDataUsageActivity mobileDataUsageActivity);
- APIClient apiClient();
- Context context();
- }
- public class MyApplication extends Application {
- private ApplicationComponent applicationComponent;
- @Override
- public void onCreate() {
- super.onCreate();
- applicationComponent = DaggerApplicationComponent
- .builder()
- .applicationContextModule(new ApplicationContextModule(this))
- .dataModule(new DataModule())
- .build();
- }
- public ApplicationComponent getApplicationComponent() {
- return applicationComponent;
- }
- }
- ((MyApplication) getApplication()).getApplicationComponent().inject(this);
- public class MobileDataRepository {
- private static final String TAG = "MobileDataRepository";
- @Inject
- APIClient apiClient;
- private List<Quarter> quarterList = new ArrayList<>();
- private List<Year> yearList = new ArrayList<>();
- private MutableLiveData<List<Year>> mutableYearList = new MutableLiveData<>();
- public LiveData<List<Year>> getYearlyMobileDataUsage() {
- apiClient.getMobileDataUsage(new OnDatastoreResponse() {
- @Override
- public void onSuccessDatastoreResponse(DatastoreResponse datastoreResponse) {
- for (QuarterResponse q : datastoreResponse.getResult().getRecords()) {
- Log.d(TAG, "Quarter: " + q.get_id() + " : " + q.getQuarter());
- String quarterInfo[] = q.getQuarter().split("-");
- String year = quarterInfo[0];
- String quarterName = quarterInfo[1];
- quarterList.add(new Quarter(q.get_id(), q.getVolume_of_mobile_data(), Integer.parseInt(year), quarterName));
- }
- mutableYearList.setValue(yearList);
- }
- @Override
- public void onErrorResponse(Throwable e) {
- }
- });
- return mutableYearList;
- }
- }
- java.lang.RuntimeException: Unable to start activity ComponentInfo{com.channa.mobiledatausageapp/com.channa.mobiledatausageapp.view.MobileDataUsageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.channa.mobiledatausageapp.network.APIClient.getMobileDataUsage(com.channa.mobiledatausageapp.network.action.OnDatastoreResponse)' on a null object reference
Add Comment
Please, Sign In to add comment