| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 | 
| 12 | 13 | 14 | 15 | 16 | 17 | 18 | 
| 19 | 20 | 21 | 22 | 23 | 24 | 25 | 
| 26 | 27 | 28 | 29 | 30 | 31 | 
													Tags
													
											
												
												- RX
- 하드코드검색
- hardcoded string search
- RxJava2
- rxandroid
- Android
- Rx자바
- deprecating the install_referrer
- Switch to the Play Referrer API by March 1
- RXjava
- 안드로이드
- java
- andorid studio
- Retrofit2
- android P
													Archives
													
											
												
												- Today
- Total
Lucky Dog
[안드로이드] RxAndroid + Retrofit2 + LazyHolder Singleton 네트워크 통신 본문
RxAndroid + Retrofit2 + LazyHolder singleton (Initialization-on-demand holder idiom) 패턴을 이용한 네트워크 통신 예제
코드가 간결해지고 측정은 안해봤지만 왠지 속도도 빨라진 것 같은 느낌적인 느낌 :D
build.gradle
dependencies {
...
  implementation 'com.squareup.retrofit2:retrofit:2.4.0'
  implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
  implementation 'com.squareup.okhttp3:okhttp:3.10.0'     // Dependencies okio 1.14.0
  implementation 'com.squareup.okhttp3:logging-interceptor:3.10.0'
  implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
  implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
  implementation 'io.reactivex.rxjava2:rxjava:2.2.6'
}NetworkHandler
public class NetworkHandler {
    private static final String TAG = NetworkHandler.class.getSimpleName();
    private static HttpLoggingInterceptor logging =
            new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
    private static class LazyHolder {
        static final NetworkHandler INSTANCE = new NetworkHandler();
    }
    private static ServerInterface serverInterface;
    private NetworkHandler() {
        init();
    }
    public static ServerInterface getServerInterface() {
        return serverInterface;
    }
    private void init() {
        OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
        okHttpBuilder.addInterceptor(logging);
        okHttpBuilder.connectTimeout(30, TimeUnit.SECONDS);
        okHttpBuilder.readTimeout(30, TimeUnit.SECONDS);
        okHttpBuilder.writeTimeout(30, TimeUnit.SECONDS);
        okHttpBuilder.retryOnConnectionFailure(true);
        OkHttpClient okHttpClient = okHttpBuilder.build();
        serverInterface = new Retrofit.Builder()
                .baseUrl(Config.SERVER_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build()
                .create(ServerInterface.class);
    }
    public static NetworkHandler getInstance() {
        return LazyHolder.INSTANCE;
    }
}ServerInterface
public interface RetrofitInterface {
...
    @POST("CheckStatus")
    Observable<ResponseModel.CheckStatus> CheckStatus(@Body String body);
}Usage
RequestModel.CheckStatus reqCheckStatus = new RequestModel.CheckStatus();
reqCheckStatus.setId(id);
String param = reqCheckStatus.toJson();
ServerInterface serverInterface = RetrofitHandler.getInstance().getServerInterface();
Observable<ResponseModel.CheckStatus> observable = serverInterface.CheckStatus(param);
Disposable checkStatusDisposable = observable.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(respCheckStatus -> {
                        // response handling
                    }, 
                    e->{
                        // error handling
                    },
                    ()->{}
                    );
        }
			  Comments