381 lines
16 KiB
Java
381 lines
16 KiB
Java
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.blankj.utilcode.util.ToastUtils;
|
||
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;
|
||
import com.qxcm.moduleutil.bean.AlbumBean;
|
||
import com.qxcm.moduleutil.bean.CircleListBean;
|
||
import com.qxcm.moduleutil.bean.CommentBean;
|
||
import com.qxcm.moduleutil.bean.ExpandColumnBean;
|
||
import com.qxcm.moduleutil.bean.GiftLabelBean;
|
||
import com.qxcm.moduleutil.bean.HeatedBean;
|
||
import com.qxcm.moduleutil.bean.NewsDataBean;
|
||
import com.qxcm.moduleutil.bean.RealNameBean;
|
||
import com.qxcm.moduleutil.bean.RewardUserBean;
|
||
import com.qxcm.moduleutil.bean.RoonGiftModel;
|
||
import com.qxcm.moduleutil.bean.UserBean;
|
||
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 okhttp3.Cache;
|
||
import okhttp3.OkHttpClient;
|
||
import retrofit2.Call;
|
||
import retrofit2.Callback;
|
||
import retrofit2.Response;
|
||
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())/**/
|
||
// .addConverterFactory(GsonConverterFactory.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);
|
||
}
|
||
}
|
||
|
||
public void realName(String real_name, String card_number,BaseObserver<RealNameBean> observer){
|
||
sApiServer.realName(real_name,card_number).compose(new DefaultTransformer<>()).subscribe(observer);
|
||
}
|
||
//获取扩列数据
|
||
public void getExpandColumn(String type,String page,String page_limit,BaseObserver<List<ExpandColumnBean>> observer){
|
||
sApiServer.getExpandColumn(type).compose(new DefaultTransformer<>()).subscribe(observer);
|
||
}
|
||
//获取官方公告数据或者系统消息,根据type区分 type=1是系统消息,type=2是官方公告
|
||
public void getOfficialNotice(String page,String page_limit,String type,BaseObserver<List<NewsDataBean>> observer){
|
||
sApiServer.getOfficialNotice(page,page_limit,type).compose(new DefaultTransformer<>()).subscribe(observer);
|
||
}
|
||
|
||
public void getAlbumList(String page,String page_limit,BaseObserver<List<AlbumBean>> observer){
|
||
sApiServer.getAlbumList(page,page_limit).compose(new DefaultTransformer<>()).subscribe(observer);
|
||
}
|
||
|
||
public void createAlbum(String name,String image,BaseObserver<String> observer) {
|
||
sApiServer.createAlbum(name,image).enqueue(new Callback<BaseModel<String>>() {
|
||
@Override
|
||
public void onResponse(Call<BaseModel<String>> call, Response<BaseModel<String>> response) {
|
||
if (response.code()==200){
|
||
BaseModel<String> string=response.body();
|
||
if (string!=null){
|
||
int code=string.getCode();
|
||
if (code==1){
|
||
observer.onNext(string.getMsg());
|
||
}else if (code==301){
|
||
try {
|
||
CommonAppContext.getInstance().clearLoginInfo();
|
||
} catch (ClassNotFoundException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
com.blankj.utilcode.util.ToastUtils.showShort(string.getMsg());
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(Call<BaseModel<String>> call, Throwable t) {
|
||
com.blankj.utilcode.util.ToastUtils.showShort(t.toString());
|
||
}
|
||
});
|
||
}
|
||
|
||
public void getRewardList(String id, int page, int page_limit,BaseObserver<List<RewardUserBean> > observer){
|
||
sApiServer.getRewardList(id,page,page_limit).compose(new DefaultTransformer<>()).subscribe(observer);
|
||
|
||
}
|
||
|
||
public void getGiftLabel(String have_hot,BaseObserver<List<GiftLabelBean>> observer){
|
||
sApiServer.getGiftLabel(have_hot).compose(new DefaultTransformer<>()).subscribe(observer);
|
||
}
|
||
|
||
public void getGiftList( int type, BaseObserver<List<RoonGiftModel>> observer){
|
||
sApiServer.getGiftList(type).compose(new DefaultTransformer<>()).subscribe(observer);
|
||
}
|
||
|
||
public void topicList(String page,String page_limit,BaseObserver<List<HeatedBean>> observer){
|
||
sApiServer.topicList(page,page_limit).compose(new DefaultTransformer<>()).subscribe(observer);
|
||
}
|
||
|
||
public void publishZone(String images, String content, String topic_id, String room_id,String ip,BaseObserver<String> observer){
|
||
|
||
sApiServer.publishZone(images,content,topic_id,room_id,ip).enqueue(new Callback<BaseModel<String>>() {
|
||
@Override
|
||
public void onResponse(Call<BaseModel<String>> call, Response<BaseModel<String>> response) {
|
||
if (response.code()==200){
|
||
BaseModel<String> string=response.body();
|
||
if (string!=null){
|
||
int code=string.getCode();
|
||
if (code==1){
|
||
observer.onNext(string.getMsg());
|
||
}else if (code==301){
|
||
try {
|
||
CommonAppContext.getInstance().clearLoginInfo();
|
||
} catch (ClassNotFoundException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
com.blankj.utilcode.util.ToastUtils.showShort(string.getMsg());
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(Call<BaseModel<String>> call, Throwable t) {
|
||
com.blankj.utilcode.util.ToastUtils.showShort(t.toString());
|
||
}
|
||
});
|
||
}
|
||
|
||
public void getCategories(BaseObserver<List<HeatedBean>> observer){//动态顶部热门话题
|
||
sApiServer.getCategories().compose(new DefaultTransformer<>()).subscribe(observer);
|
||
}
|
||
|
||
public void getCircleList(String page,String page_limit,BaseObserver<List<CircleListBean>> observer){//动态列表
|
||
sApiServer.getCircleList(page,page_limit).compose(new DefaultTransformer<>()).subscribe(observer);
|
||
}
|
||
|
||
public void likeZone(String zone_id,BaseObserver<String> observer){
|
||
sApiServer.likeZone(zone_id).enqueue(new Callback<BaseModel<String>>() {
|
||
@Override
|
||
public void onResponse(Call<BaseModel<String>> call, Response<BaseModel<String>> response) {
|
||
if (response.code()==200){
|
||
BaseModel<String> string=response.body();
|
||
if (string!=null){
|
||
int code=string.getCode();
|
||
if (code==1){
|
||
observer.onNext(string.getMsg());
|
||
}else if (code==301){
|
||
try {
|
||
CommonAppContext.getInstance().clearLoginInfo();
|
||
} catch (ClassNotFoundException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
com.blankj.utilcode.util.ToastUtils.showShort(string.getMsg());
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(Call<BaseModel<String>> call, Throwable t) {
|
||
com.blankj.utilcode.util.ToastUtils.showShort(t.toString());
|
||
}
|
||
});
|
||
}
|
||
|
||
public void getCommentList(String id, String page, String page_limit, BaseObserver<CommentBean> observer){
|
||
sApiServer.getCommentList(id,page,page_limit).compose(new DefaultTransformer<>()).subscribe(observer);
|
||
}
|
||
|
||
public void commentZone(String id, String content, String pid, String reply_to,BaseObserver<String> observer){
|
||
sApiServer.commentZone(id,content,pid,reply_to).enqueue(new Callback<BaseModel<String>>() {
|
||
@Override
|
||
public void onResponse(Call<BaseModel<String>> call, Response<BaseModel<String>> response) {
|
||
if (response.code()==200){
|
||
BaseModel<String> string=response.body();
|
||
if (string!=null){
|
||
int code=string.getCode();
|
||
if (code==1){
|
||
observer.onNext(string.getMsg());
|
||
}else if (code==301){
|
||
try {
|
||
CommonAppContext.getInstance().clearLoginInfo();
|
||
} catch (ClassNotFoundException e) {
|
||
throw new RuntimeException(e);
|
||
}
|
||
com.blankj.utilcode.util.ToastUtils.showShort(string.getMsg());
|
||
}
|
||
}
|
||
|
||
}else {
|
||
ToastUtils.showShort("评论失败");
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onFailure(Call<BaseModel<String>> call, Throwable t) {
|
||
com.blankj.utilcode.util.ToastUtils.showShort(t.toString());
|
||
}
|
||
});
|
||
}
|
||
|
||
}
|