2025-05-22 19:03:01 +08:00
|
|
|
package com.qxcm.moduleutil.http;
|
|
|
|
|
|
|
|
|
|
//import com.blankj.utilcode.util.LogUtils;
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
|
import android.net.NetworkInfo;
|
|
|
|
|
|
|
|
|
|
import com.franmontiel.persistentcookiejar.ClearableCookieJar;
|
|
|
|
|
import com.franmontiel.persistentcookiejar.PersistentCookieJar;
|
|
|
|
|
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
|
|
|
|
|
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
|
|
|
|
|
import com.qxcm.moduleutil.base.CommonAppContext;
|
2025-05-29 08:59:34 +08:00
|
|
|
import com.qxcm.moduleutil.bean.RealNameBean;
|
2025-05-22 19:03:01 +08:00
|
|
|
import com.qxcm.moduleutil.bean.UserBean;
|
|
|
|
|
import com.qxcm.moduleutil.presenter.BasePresenter;
|
|
|
|
|
import com.qxcm.moduleutil.utils.SystemUtils;
|
|
|
|
|
import com.qxcm.moduleutil.utils.logger.DataLogger;
|
|
|
|
|
import com.qxcm.moduleutil.utils.logger.DataLoggingInterceptor;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.net.Proxy;
|
|
|
|
|
import java.security.KeyManagementException;
|
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
|
|
|
|
|
|
|
import io.reactivex.disposables.Disposable;
|
|
|
|
|
import okhttp3.Cache;
|
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
|
import retrofit2.Retrofit;
|
|
|
|
|
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
|
|
|
|
|
|
|
|
|
public class RetrofitClient {
|
|
|
|
|
|
|
|
|
|
private static RetrofitClient INSTANCE;
|
|
|
|
|
private static ApiServer sApiServer;
|
|
|
|
|
public static final int DEFAULT_TIME_OUT = 60;
|
|
|
|
|
private static OkHttpClient client;
|
|
|
|
|
private final Retrofit mRetrofit;
|
|
|
|
|
|
|
|
|
|
public OkHttpClient getHttpClient() {
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
File cacheFile = new File(CommonAppContext.getInstance().getCacheDir(), "cache");
|
|
|
|
|
Cache cache = new Cache(cacheFile, 1024 * 1024 * 100); // 100MB 的缓存空间
|
|
|
|
|
boolean isNetworkAvailable = checkNetworkConnection(); // 实现这个方法来检查网络状态
|
|
|
|
|
String cacheControl = isNetworkAvailable ? "max-age=0" : "only-if-cached, max-stale=86400";
|
|
|
|
|
|
|
|
|
|
private boolean checkNetworkConnection() {
|
|
|
|
|
ConnectivityManager connectivityManager = (ConnectivityManager) CommonAppContext.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
|
|
|
|
|
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private OkHttpClient provideOkHttpClient() {
|
|
|
|
|
Map<String, String> headers = SystemUtils.getSystemParams();
|
|
|
|
|
SetCookieCache cookieCache = new SetCookieCache();
|
|
|
|
|
ClearableCookieJar cookieJar =
|
|
|
|
|
new PersistentCookieJar(cookieCache, new SharedPrefsCookiePersistor(CommonAppContext.getInstance()));
|
|
|
|
|
try {
|
|
|
|
|
X509TrustManager trustAllCert = new X509TrustManager() {
|
|
|
|
|
@Override
|
|
|
|
|
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
|
|
|
|
|
return new java.security.cert.X509Certificate[]{};
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
SSLContext sslContext = null;
|
|
|
|
|
sslContext = SSLContext.getInstance("SSL");
|
|
|
|
|
sslContext.init(null, new TrustManager[]{trustAllCert}, new SecureRandom());
|
|
|
|
|
|
|
|
|
|
final OkHttpClient client = new OkHttpClient.Builder()
|
|
|
|
|
// .addInterceptor(new LoggerInterceptor("HttpLog", true))
|
|
|
|
|
.addInterceptor(new DataLoggingInterceptor(new DataLogger()))
|
|
|
|
|
.addInterceptor(new AccessTokenInterceptor(headers))
|
|
|
|
|
.proxy(Proxy.NO_PROXY)
|
|
|
|
|
.sslSocketFactory(sslContext.getSocketFactory(), trustAllCert)
|
|
|
|
|
.hostnameVerifier((hostname, session) -> true)
|
|
|
|
|
.cookieJar(cookieJar)
|
|
|
|
|
.connectTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS)
|
|
|
|
|
.readTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS)
|
|
|
|
|
.writeTimeout(DEFAULT_TIME_OUT, TimeUnit.SECONDS)
|
|
|
|
|
.build();
|
|
|
|
|
RetrofitClient.client = client;
|
|
|
|
|
} catch (KeyManagementException | NoSuchAlgorithmException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Retrofit provideRetrofit(OkHttpClient client) {
|
|
|
|
|
return new Retrofit.Builder()
|
|
|
|
|
.addConverterFactory(MyConverterFactory.create())
|
|
|
|
|
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
|
|
|
|
.baseUrl(CommonAppContext.getInstance().getCurrentEnvironment().getServerUrl())
|
|
|
|
|
.client(client)
|
|
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// private static Retrofit mRainRetrofit;
|
|
|
|
|
//
|
|
|
|
|
// public static Retrofit getRainRetrofit() {
|
|
|
|
|
// if (mRainRetrofit == null) {
|
|
|
|
|
// synchronized (RetrofitClient.class) {
|
|
|
|
|
// mRainRetrofit = new Retrofit.Builder()
|
|
|
|
|
// .addConverterFactory(RainConverterFactory.create())
|
|
|
|
|
// .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
|
|
|
|
// .baseUrl(BuildConfig.RAIN_GAME_BASE_URL)
|
|
|
|
|
// .client(client)
|
|
|
|
|
// .build();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// return mRainRetrofit;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
private RetrofitClient() {
|
|
|
|
|
mRetrofit = provideRetrofit(provideOkHttpClient());
|
|
|
|
|
sApiServer = mRetrofit.create(ApiServer.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static RetrofitClient getInstance() {
|
|
|
|
|
if (INSTANCE == null) {
|
|
|
|
|
synchronized (RetrofitClient.class) {
|
|
|
|
|
if (INSTANCE == null) {
|
|
|
|
|
INSTANCE = new RetrofitClient();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return INSTANCE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public <T> T createApiClient(Class<T> apiClientClass) {
|
|
|
|
|
return mRetrofit.create(apiClientClass);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void login() {
|
|
|
|
|
// login("18473492252", "123456");
|
|
|
|
|
// login("18229732986", "123456");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void sendCode(String mobile,String event,BaseObserver<Object> observer) {
|
|
|
|
|
sApiServer.sendCode(mobile,event).compose(new DefaultTransformer<>()).subscribe(observer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void login(String mobile, String password, BaseObserver<List<UserBean>> observer) {
|
|
|
|
|
sApiServer.login(mobile, password).compose(new DefaultTransformer<>()).subscribe(observer);
|
|
|
|
|
}
|
|
|
|
|
public void userLogin(String mobile, String password, BaseObserver<List<UserBean>> observer) {
|
|
|
|
|
sApiServer.userLogin(mobile, password).compose(new DefaultTransformer<>()).subscribe(observer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void upUserNickname(BaseObserver<String> observer){
|
|
|
|
|
sApiServer.upUserNickname().compose(new DefaultTransformer<>()).subscribe(observer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void upUserPic(String sex,BaseObserver<String> observer){
|
|
|
|
|
sApiServer.upUserPic(sex).compose(new DefaultTransformer<>()).subscribe(observer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void switchAccounts(String loginId, BaseObserver<List<UserBean>> observer){
|
|
|
|
|
sApiServer.switchAccounts(loginId).compose(new DefaultTransformer<>()).subscribe(observer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void userUpdate(Map<String, String> map, BaseObserver<UserBean> observer) {
|
|
|
|
|
sApiServer.userUpdate(map).compose(new DefaultTransformer<>()).subscribe(observer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void authorization(BaseObserver<String> observer){
|
|
|
|
|
sApiServer.authorization().compose(new DefaultTransformer<>()).subscribe(observer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void oauthLogin(String netease_token, BaseObserver<List<UserBean>> observer) {
|
|
|
|
|
sApiServer.oauthLogin(netease_token).compose(new DefaultTransformer<>()).subscribe(observer);
|
|
|
|
|
}
|
|
|
|
|
public void authCode(String netease_token,int type, BaseObserver<List<UserBean>> observer) {
|
|
|
|
|
if (type==1){
|
|
|
|
|
sApiServer.authCode1(netease_token).compose(new DefaultTransformer<>()).subscribe(observer);
|
|
|
|
|
}else {
|
|
|
|
|
sApiServer.authCode(netease_token).compose(new DefaultTransformer<>()).subscribe(observer);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-29 08:59:34 +08:00
|
|
|
|
|
|
|
|
public void realName(String real_name, String card_number,BaseObserver<RealNameBean> observer){
|
|
|
|
|
sApiServer.realName(real_name,card_number).compose(new DefaultTransformer<>()).subscribe(observer);
|
|
|
|
|
}
|
2025-05-22 19:03:01 +08:00
|
|
|
}
|