Lucky Dog

[안드로이드] RxAndroid + Retrofit2 + LazyHolder Singleton 네트워크 통신 본문

안드로이드

[안드로이드] RxAndroid + Retrofit2 + LazyHolder Singleton 네트워크 통신

Poohya 2019. 4. 4. 09:31

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