티스토리 뷰
1. HttpApiClient 설계
public class HttpApiClient {
private static final String TAG = "HttpApiClient";
private Retrofit retrofit;
private IApiService apiService;
private NetworkStateListener listener;
private Context context;
private String url;
public interface NetworkStateListener {
void onNetworkStateListener(boolean isEnable);
}
public void setOnNetworkStateListener(NetworkStateListener listener) {
this.listener = listener;
}
public HttpApiClient(String url, Context context) {
this.url = url;
this.context = context;
LogUtil.d(TAG, "Api Url : " + url);
}
public void setup() {
if (Utility.getNetworkConnectedStatus(context) == Utility.CONNECT_NETWORK ||
Utility.getNetworkConnectedStatus(context) == Utility.CONNECT_NETWORK_BUT_NOT_USE_MOBILE_DATA) {
retrofit = new Retrofit.Builder()
.baseUrl(url)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
// TODO: Create retrofit Service. url 에 따라 retrofit 을 위한 interface service 를 다르게 해야함
apiService = retrofit.create(IApiService.class);
listener.onNetworkStateListener(true);
} else {
listener.onNetworkStateListener(false);
}
}
public IApiService getIVersionService() { return apiService; }
}
Utility ,,,,
// WIFI 가 꺼져있고 device 에서 3G/LTE 가 사용 가능하나 설정에서 3G/LTE 가 꺼져있는 경우
public static int getNetworkConnectedStatus(Context context) {
try {
boolean isDataConnect = new LoginInfoDBHelper(context).isDataConnect();
NetworkInfo networkInfo = getNetworkInfo(context);
if (networkInfo != null) {
if (networkInfo.isConnectedOrConnecting()) {
boolean isWifiConn = networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
boolean isMobileConn = networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
// LogUtil.d(TAG, "isDataConnect : " + isDataConnect + ", isWifiConn : " + isWifiConn + ", isMobileConn : " + isMobileConn);
if (isWifiConn || (isDataConnect && isMobileConn)) {
return CONNECT_NETWORK;
} else {
if (!isDataConnect && isMobileConn) {
return CONNECT_NETWORK_BUT_NOT_USE_MOBILE_DATA;
}
}
}
}
return DISCONNECT_NETWORK;
} catch (Exception e) {
LogUtil.d(TAG, "getNetworkConnectedStatus() : " + e.getMessage());
return CONNECT_ERROR;
}
}
// Intro 부터 시작시에는 server 로 부터 data 를 받아 serialize 하여 MainActivity 로 전달함.
// Main 에서는 serialize 정보가 없을 시 requestData 를 실행하고 있으면 data 를 사용함. (onResume or onStart)
private void requestServerData() {
if (networkFl.isShown()) {
networkFl.setVisibility(View.GONE);
}
startAnimLoading();
final HttpApiClient apiClient = new HttpApiClient(HttpUrl.getAPiUrl(this), this);
apiClient.setOnNetworkStateListener(new HttpApiClient.NetworkStateListener() {
@Override
public void onNetworkStateListener(boolean isEnable) {
if (isEnable) {
Observable<IApiService.ApiServiceData> apiServiceObservable =
apiClient.getIVersionService().getData(
AppInfo.OS,
AppInfo.getPagkageName(IntroActivity.this),
AppInfo.getVersionName(IntroActivity.this));
apiServiceObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<IApiService.ApiServiceData>() {
@Override
public void onCompleted() {}
@Override
public void onError(Throwable e) {
LogUtil.d(TAG, "IApiService onError : " + e.getMessage());
showErrorPopup(true);
}
@Override
public void onNext(IApiService.ApiServiceData apiServiceData) {
LogUtil.d(TAG, apiServiceData.server.node[0]);
// Insert DB serverNode column
String serverNode = apiServiceData.server.node[0];
String chatNotice = apiServiceData.chatMessage.intro;
boolean isBroadcast = apiServiceData.broadcast;
String updateVersion = apiServiceData.update.android.version; // 강제 업데이트가 되어야 하는 버전
new ServerInfoDBHelper(IntroActivity.this)
.insertDB(serverNode, chatNotice, isBroadcast);
new LinkInfoDBHelper(IntroActivity.this)
.insertDB(apiServiceData.link);
new BannerInfoDBHelper(IntroActivity.this)
.insertDB(apiServiceData.banner);
new SocialLoginInfoDBHelper(IntroActivity.this)
.insertDB(apiServiceData.socialLogin);
LogUtil.d(TAG, "updateVersion : " + updateVersion + ", appVersion : " + appVersion);
int appVersionNum = Utility.getVersionNum(appVersion);
int updateVersionNum = Utility.getVersionNum(updateVersion);
if (updateVersionNum > appVersionNum) {
// 1. server update android version 을 체크하여 강제 업데이트
isForceUpdate = true;
showVersionPopup(true);
} else {
// 2. google version 과 비교하여 사용자 업데이트
requestHttpGooglePlayVersion();
}
}
});
} else {
networkFl.setVisibility(View.VISIBLE);
}
}
});
apiClient.setup();
}
'tip' 카테고리의 다른 글
tip34. 콜백 리스너 사용하기 (어댑터에서 콜백 사용) (0) | 2018.08.02 |
---|---|
tip6. 디버그 모드 구분하기 (0) | 2018.05.25 |