Guest User

Untitled

a guest
Feb 17th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. public void getMobileDataUsage(OnDatastoreResponse onDatastoreResponse)
  2.  
  3. @Singleton
  4. public class APIClient {
  5.  
  6. private static final String TAG = "APIClient";
  7.  
  8. private APIInterface apiInterface;
  9. private Retrofit retrofit;
  10.  
  11. @Inject
  12. public APIClient(Context context) {
  13. // use 10MB cache
  14. long cacheSize = 10 * 1024 * 1024;
  15. Cache cache = new Cache(context.getCacheDir(), cacheSize);
  16.  
  17. HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
  18. interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
  19. OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).cache(cache).build();
  20.  
  21. retrofit = new Retrofit.Builder()
  22. .baseUrl(BuildConfig.BASE_URL)
  23. .addConverterFactory(GsonConverterFactory.create())
  24. .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
  25. .client(client)
  26. .build();
  27.  
  28. this.apiInterface = retrofit.create(APIInterface.class);
  29. }
  30.  
  31. public void getMobileDataUsage(OnDatastoreResponse onDatastoreResponse) {
  32. String resourceId = "a807b7ab-6cad-4aa6-87d0-e283a7353a0f";
  33. Integer limit = null;
  34.  
  35. Single<DatastoreResponse> datastoreResponse = apiInterface.getMobileDataUsage(resourceId, limit);
  36. datastoreResponse.subscribeOn(Schedulers.io())
  37. .observeOn(AndroidSchedulers.mainThread())
  38. .subscribe(new DisposableSingleObserver<DatastoreResponse>() {
  39. @Override
  40. public void onSuccess(DatastoreResponse datastoreResponse) {
  41. if (datastoreResponse.getSuccess()) {
  42. Log.d(TAG, "onSuccess: " + datastoreResponse.getSuccess());
  43.  
  44. onDatastoreResponse.onSuccessDatastoreResponse(datastoreResponse);
  45.  
  46. } else {
  47. Log.e(TAG, "onSuccess: " + datastoreResponse.getSuccess());
  48. onDatastoreResponse.onErrorResponse(new Exception("Datastore response not successful"));
  49. }
  50. }
  51.  
  52. @Override
  53. public void onError(Throwable e) {
  54. Log.e(TAG, "onError: " + e.getMessage(), e);
  55. onDatastoreResponse.onErrorResponse(e);
  56. }
  57. });
  58. }
  59.  
  60. }
  61.  
  62. @Module
  63. public class ApplicationContextModule {
  64.  
  65. private final Context context;
  66.  
  67. public ApplicationContextModule(Context context) {
  68. this.context = context;
  69. }
  70.  
  71. @Provides
  72. Context provideApplicationContext() {
  73. return context;
  74. }
  75.  
  76. }
  77.  
  78. @Singleton
  79. @Component(modules = {ApplicationContextModule.class, DataModule.class})
  80. public interface ApplicationComponent {
  81.  
  82. void inject(MobileDataUsageActivity mobileDataUsageActivity);
  83.  
  84. APIClient apiClient();
  85.  
  86. Context context();
  87.  
  88. }
  89.  
  90. public class MyApplication extends Application {
  91.  
  92. private ApplicationComponent applicationComponent;
  93.  
  94. @Override
  95. public void onCreate() {
  96. super.onCreate();
  97.  
  98. applicationComponent = DaggerApplicationComponent
  99. .builder()
  100. .applicationContextModule(new ApplicationContextModule(this))
  101. .dataModule(new DataModule())
  102. .build();
  103.  
  104. }
  105.  
  106. public ApplicationComponent getApplicationComponent() {
  107. return applicationComponent;
  108. }
  109. }
  110.  
  111. ((MyApplication) getApplication()).getApplicationComponent().inject(this);
  112.  
  113. public class MobileDataRepository {
  114.  
  115. private static final String TAG = "MobileDataRepository";
  116.  
  117. @Inject
  118. APIClient apiClient;
  119.  
  120. private List<Quarter> quarterList = new ArrayList<>();
  121. private List<Year> yearList = new ArrayList<>();
  122.  
  123. private MutableLiveData<List<Year>> mutableYearList = new MutableLiveData<>();
  124.  
  125. public LiveData<List<Year>> getYearlyMobileDataUsage() {
  126. apiClient.getMobileDataUsage(new OnDatastoreResponse() {
  127. @Override
  128. public void onSuccessDatastoreResponse(DatastoreResponse datastoreResponse) {
  129.  
  130. for (QuarterResponse q : datastoreResponse.getResult().getRecords()) {
  131. Log.d(TAG, "Quarter: " + q.get_id() + " : " + q.getQuarter());
  132.  
  133. String quarterInfo[] = q.getQuarter().split("-");
  134. String year = quarterInfo[0];
  135. String quarterName = quarterInfo[1];
  136.  
  137. quarterList.add(new Quarter(q.get_id(), q.getVolume_of_mobile_data(), Integer.parseInt(year), quarterName));
  138. }
  139. mutableYearList.setValue(yearList);
  140. }
  141.  
  142. @Override
  143. public void onErrorResponse(Throwable e) {
  144.  
  145. }
  146. });
  147.  
  148. return mutableYearList;
  149.  
  150. }
  151.  
  152. }
  153.  
  154. 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