Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. public class BaseApi {
  2. private static BaseApi instance = null;
  3. public static CryptoApi service;
  4. private static OkHttpClient client;
  5. private static Retrofit retrofit;
  6.  
  7.  
  8. public BaseApi() {
  9. HttpLoggingInterceptor logger = new HttpLoggingInterceptor();
  10. logger.setLevel(HttpLoggingInterceptor.Level.BODY);
  11. client = new OkHttpClient.Builder()
  12. .connectTimeout(60, TimeUnit.SECONDS)
  13. .readTimeout(30, TimeUnit.SECONDS)
  14. .addInterceptor(logger)
  15. .build();
  16.  
  17. retrofit = new Retrofit.Builder()
  18. .baseUrl(BuildConfig.BASE_URL)
  19. .client(client)
  20. .addConverterFactory(GsonConverterFactory.create())
  21. .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
  22. .build();
  23.  
  24. service = retrofit.create(CryptoApi.class);
  25. }
  26.  
  27. public static BaseApi getInstance() {
  28. if (instance == null) {
  29. instance = new BaseApi();
  30. }
  31. return instance;
  32. }
  33. }
  34.  
  35. public interface CryptoApi {
  36.  
  37. @GET("?limit=5")
  38. Observable<List<Crypto>> getCrypto();
  39.  
  40. }
  41.  
  42. private void getCrypto() {
  43. Subscription subscription = BaseApi.getInstance().service.getCrypto()
  44. .subscribeOn(Schedulers.io())
  45. .observeOn(AndroidSchedulers.mainThread())
  46. .subscribe(crypto -> {
  47. Log.d("Crypto", crypto.toString())
  48. }, throwable -> {
  49. Log.e("User from server error", String.valueOf(throwable));
  50. });
  51. addSubscription(subscription);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement