package com.xhsyb.kankan.http; import android.content.Context; import android.text.TextUtils; import com.facebook.appevents.AppEventsConstants; import com.xhsyb.kankan.BookApplication; import com.xhsyb.kankan.bean.appDomainBean; import com.xhsyb.kankan.http.OkHttpRetryInterceptor; import com.xhsyb.kankan.utils.ACache; import com.xhsyb.kankan.utils.AppUtils; import com.xhsyb.kankan.utils.SPUtils; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; import me.jessyan.retrofiturlmanager.RetrofitUrlManager; import okhttp3.Cache; import okhttp3.CacheControl; import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.logging.HttpLoggingInterceptor; import org.json.JSONArray; import org.json.JSONException; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; public class RetrofitHttpUtil { public static ApiService apiService; public static List<String> localurls = new ArrayList(); public static List<String> urls = new ArrayList(); private int maxCacheTime = 60; private JSONArray newList; public static ApiService getApiService() { if (apiService == null) { synchronized (RetrofitHttpUtil.class) { if (apiService == null) { new RetrofitHttpUtil(); } } } return apiService; } private RetrofitHttpUtil() { final Context instance = BookApplication.getInstance(); urls.clear(); localurls.clear(); appDomainBean appdomainbean = (appDomainBean) ACache.get(instance).getAsObject("app_domain"); if (!(appdomainbean == null || appdomainbean.getApi_domains() == null || appdomainbean.getApi_domains().size() == 0)) { urls.addAll(appdomainbean.getApi_domains()); } String str = (String) SPUtils.get("UrlLists", "[\"https://app.kangyankun.com\",\"https://app.yinfangjituan.com\",\"https://app.16769app.com\",\"https://app.vaavb.com\",\"https://app.vvzvz.com\",\"https://app.langshilunzh.com\"]"); if (!TextUtils.isEmpty(str)) { try { this.newList = new JSONArray(str); } catch (JSONException e) { e.printStackTrace(); } } String str2 = (String) SPUtils.get("baseUrl", ""); for (int i = 0; i < this.newList.length(); i++) { try { localurls.add((String) this.newList.get(i)); } catch (JSONException e2) { e2.printStackTrace(); } } int indexOf = localurls.indexOf(str2); if (indexOf != -1) { Collections.swap(localurls, 0, indexOf); } urls.addAll(localurls); str2 = TextUtils.isEmpty(str2) ? urls.get(0) : str2; BookApplication.http_url = str2; Cache cache = new Cache(new File(AppUtils.getCacheDir(instance), "httpCache"), 52428800); AnonymousClass1 r3 = new Interceptor() { /* class com.xhsyb.kankan.http.RetrofitHttpUtil.AnonymousClass1 */ @Override // okhttp3.Interceptor public Response intercept(Interceptor.Chain chain) throws IOException { Request request = chain.request(); if (!AppUtils.isNetworkConnected(instance)) { request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build(); } else if (AppUtils.isNetworkConnected(instance)) { request = request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build(); } Response proceed = chain.proceed(request); if (!AppUtils.isNetworkConnected(instance)) { return proceed; } Response.Builder newBuilder = proceed.newBuilder(); return newBuilder.header("Cache-Control", "public,max-age=" + RetrofitHttpUtil.this.maxCacheTime).removeHeader("Pragma").build(); } }; AnonymousClass2 r5 = new Interceptor() { /* class com.xhsyb.kankan.http.RetrofitHttpUtil.AnonymousClass2 */ @Override // okhttp3.Interceptor public Response intercept(Interceptor.Chain chain) throws IOException { return chain.proceed(chain.request().newBuilder().removeHeader("User-Agent").header("User-Agent", AppUtils.getUserAgent(instance)).header("Accept", "application/octet-stream").header("Content-Type", "application/octet-stream; charset=utf-8").header("User-Id", AppUtils.getUserId(instance)).header("Client-Type", AppEventsConstants.EVENT_PARAM_VALUE_YES).header("Client-Version", AppUtils.getVersionName(instance)).header("Client-Channel", "1001").header("Accept-Language", AppUtils.getRedBookLanguage()).build()); } }; HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(); httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE); apiService = (ApiService) new Retrofit.Builder().client(RetrofitUrlManager.getInstance().with(new OkHttpClient.Builder()).readTimeout(15, TimeUnit.SECONDS).connectTimeout(20, TimeUnit.SECONDS).writeTimeout(20, TimeUnit.SECONDS).followRedirects(false).addInterceptor(r3).addNetworkInterceptor(r3).addInterceptor(r5).addInterceptor(httpLoggingInterceptor).addInterceptor(new OkHttpRetryInterceptor.Builder().buildRetryCount(urls.size() - 1).buildRetryInterval(500).buildUrls(urls).build()).cache(cache).retryOnConnectionFailure(true).build()).baseUrl(str2).addConverterFactory(MyGsonConverterFactory.create()).addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build().create(ApiService.class); } }