클래스에서 static으로 변수를 지정하고 statice메소드로 HttpConnection객체를 받아오면 됩니다.
저는 Retrofit을 사용중이라 내용은 다르지만 원하시는 형태는 맞습니다.
아래는 제가 사용하는 방식의 기본형태인데요
class ApiServiceGenerator {
private static Retrofit retrofit;
static {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(interceptor)
.build();
retrofit = new Retrofit.Builder()
.baseUrl("")
.client(client)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
}
static <T> T generate(Class<T> service) {
return retrofit.create(service);
}
}
static {}에서 HttpConnection를 생성하고 static HttpConnection getHttpConnection()메소드를 만드셔서 사용하시면 되겠네요