me.jessyan.retrofiturlmanager.RetrofitUrlManager Java Examples
The following examples show how to use
me.jessyan.retrofiturlmanager.RetrofitUrlManager.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ApiServiceModule.java From v9porn with MIT License | 5 votes |
@Singleton @Provides OkHttpClient providesOkHttpClient(CommonHeaderInterceptor commonHeaderInterceptor, HttpLoggingInterceptor httpLoggingInterceptor, RulerCookie rulerCookie, MyProxySelector myProxySelector, AddressHelper addressHelper) { OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.addInterceptor(commonHeaderInterceptor); builder.addInterceptor(httpLoggingInterceptor); builder.cookieJar(rulerCookie); builder.proxySelector(myProxySelector); // builder.sslSocketFactory(new TLSSocketFactory(),TLSSocketFactory.DEFAULT_TRUST_MANAGERS); //动态baseUrl RetrofitUrlManager.getInstance().putDomain(Api.GITHUB_DOMAIN_NAME, Api.APP_GITHUB_DOMAIN); RetrofitUrlManager.getInstance().putDomain(Api.MEI_ZI_TU_DOMAIN_NAME, Api.APP_MEIZITU_DOMAIN); RetrofitUrlManager.getInstance().putDomain(Api.MM_99_DOMAIN_NAME, Api.APP_99_MM_DOMAIN); RetrofitUrlManager.getInstance().putDomain(Api.XICI_DAILI_DOMAIN_NAME, Api.APP_PROXY_XICI_DAILI_DOMAIN); RetrofitUrlManager.getInstance().putDomain(Api.HUA_BAN_DOMAIN_NAME, Api.APP_HUA_BAN_DOMAIN); RetrofitUrlManager.getInstance().putDomain(Api.DOU_BAN_DOMAIN_NAME, Api.APP_DOU_BAN_DOMAIN); if (!TextUtils.isEmpty(addressHelper.getVideo9PornAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.PORN9_VIDEO_DOMAIN_NAME, addressHelper.getVideo9PornAddress()); } if (!TextUtils.isEmpty(addressHelper.getForum9PornAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.PORN9_FORUM_DOMAIN_NAME, addressHelper.getForum9PornAddress()); } if (!TextUtils.isEmpty(addressHelper.getPavAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.PA_DOMAIN_NAME, addressHelper.getPavAddress()); } if (!TextUtils.isEmpty(addressHelper.getAxgleAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.AXGLE_DOMAIN_NAME, addressHelper.getAxgleAddress()); } if (!TextUtils.isEmpty(addressHelper.getKeDouWoAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.KE_DOU_WO_DOMAIN_NAME, addressHelper.getKeDouWoAddress()); } return RetrofitUrlManager.getInstance().with(builder).build(); }
Example #2
Source File: SettingActivity.java From v9porn with MIT License | 5 votes |
/** * 刷新为原地址或者最新地址 * * @param key key */ private void resetOrUpdateAddress(String key) { switch (key) { case AppPreferencesHelper.KEY_SP_PORN_91_VIDEO_ADDRESS: // 全局 BaseUrl 的优先级低于 Domain-Name header 中单独配置的,其他未配置的接口将受全局 BaseUrl 的影响 if (!TextUtils.isEmpty(presenter.getVideo9PornAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.PORN9_VIDEO_DOMAIN_NAME, presenter.getVideo9PornAddress()); } break; case AppPreferencesHelper.KEY_SP_FORUM_91_PORN_ADDRESS: if (!TextUtils.isEmpty(presenter.getForum9PornAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.PORN9_FORUM_DOMAIN_NAME, presenter.getForum9PornAddress()); } break; case AppPreferencesHelper.KEY_SP_PIG_AV_ADDRESS: if (!TextUtils.isEmpty(presenter.getPavAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.PA_DOMAIN_NAME, presenter.getPavAddress()); } break; case AppPreferencesHelper.KEY_SP_AXGLE_ADDRESS: if (!TextUtils.isEmpty(presenter.getAxgleAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.AXGLE_DOMAIN_NAME, presenter.getAxgleAddress()); } break; case AppPreferencesHelper.KEY_SP_KE_DOU_WO_ADDRESS: if (!TextUtils.isEmpty(presenter.getKeDouWoAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.KE_DOU_WO_DOMAIN_NAME, presenter.getKeDouWoAddress()); } break; default: } }
Example #3
Source File: ApiServiceModule.java From v9porn with MIT License | 5 votes |
@Singleton @Provides OkHttpClient providesOkHttpClient(CommonHeaderInterceptor commonHeaderInterceptor, HttpLoggingInterceptor httpLoggingInterceptor, RulerCookie rulerCookie, MyProxySelector myProxySelector, AddressHelper addressHelper) { OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.addInterceptor(commonHeaderInterceptor); builder.addInterceptor(httpLoggingInterceptor); builder.cookieJar(rulerCookie); builder.proxySelector(myProxySelector); // builder.sslSocketFactory(new TLSSocketFactory(),TLSSocketFactory.DEFAULT_TRUST_MANAGERS); //动态baseUrl RetrofitUrlManager.getInstance().putDomain(Api.GITHUB_DOMAIN_NAME, Api.APP_GITHUB_DOMAIN); RetrofitUrlManager.getInstance().putDomain(Api.MEI_ZI_TU_DOMAIN_NAME, Api.APP_MEIZITU_DOMAIN); RetrofitUrlManager.getInstance().putDomain(Api.MM_99_DOMAIN_NAME, Api.APP_99_MM_DOMAIN); RetrofitUrlManager.getInstance().putDomain(Api.XICI_DAILI_DOMAIN_NAME, Api.APP_PROXY_XICI_DAILI_DOMAIN); RetrofitUrlManager.getInstance().putDomain(Api.HUA_BAN_DOMAIN_NAME, Api.APP_HUA_BAN_DOMAIN); if (!TextUtils.isEmpty(addressHelper.getVideo9PornAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.PORN9_VIDEO_DOMAIN_NAME, addressHelper.getVideo9PornAddress()); } if (!TextUtils.isEmpty(addressHelper.getForum9PornAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.PORN9_FORUM_DOMAIN_NAME, addressHelper.getForum9PornAddress()); } if (!TextUtils.isEmpty(addressHelper.getPavAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.PA_DOMAIN_NAME, addressHelper.getPavAddress()); } if (!TextUtils.isEmpty(addressHelper.getAxgleAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.AXGLE_DOMAIN_NAME, addressHelper.getAxgleAddress()); } return RetrofitUrlManager.getInstance().with(builder).build(); }
Example #4
Source File: SettingActivity.java From v9porn with MIT License | 5 votes |
/** * 刷新为原地址或者最新地址 * * @param key key */ private void resetOrUpdateAddress(String key) { switch (key) { case AppPreferencesHelper.KEY_SP_PORN_91_VIDEO_ADDRESS: // 全局 BaseUrl 的优先级低于 Domain-Name header 中单独配置的,其他未配置的接口将受全局 BaseUrl 的影响 if (!TextUtils.isEmpty(presenter.getVideo9PornAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.PORN9_VIDEO_DOMAIN_NAME, presenter.getVideo9PornAddress()); } break; case AppPreferencesHelper.KEY_SP_FORUM_91_PORN_ADDRESS: if (!TextUtils.isEmpty(presenter.getForum9PornAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.PORN9_FORUM_DOMAIN_NAME, presenter.getForum9PornAddress()); } break; case AppPreferencesHelper.KEY_SP_PIG_AV_ADDRESS: if (!TextUtils.isEmpty(presenter.getPavAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.PA_DOMAIN_NAME, presenter.getPavAddress()); } break; case AppPreferencesHelper.KEY_SP_AXGLE_ADDRESS: if (!TextUtils.isEmpty(presenter.getAxgleAddress())) { RetrofitUrlManager.getInstance().putDomain(Api.AXGLE_DOMAIN_NAME, presenter.getAxgleAddress()); } break; default: } }
Example #5
Source File: RecommendModel.java From LQRBiliBlili with MIT License | 5 votes |
@Inject public RecommendModel(IRepositoryManager repositoryManager, Gson gson, Application application) { super(repositoryManager); mGson = gson; mApplication = application; RetrofitUrlManager.getInstance().putDomain("recommend", Api.RECOMMEND_BASE_URL); }
Example #6
Source File: LiveModel.java From LQRBiliBlili with MIT License | 5 votes |
@Inject public LiveModel(IRepositoryManager repositoryManager, Gson gson, Application application) { super(repositoryManager); mGson = gson; mApplication = application; RetrofitUrlManager.getInstance().putDomain("live", Api.LIVE_BASE_URL); }
Example #7
Source File: VideoDetailPresenter.java From LQRBiliBlili with MIT License | 5 votes |
@Inject public VideoDetailPresenter(VideoDetailContract.Model model, VideoDetailContract.View rootView , RxErrorHandler handler, Application application , ImageLoader imageLoader, AppManager appManager) { super(model, rootView); this.mErrorHandler = handler; this.mApplication = application; this.mImageLoader = imageLoader; this.mAppManager = appManager; RetrofitUrlManager.getInstance().putDomain("video_detail_summary", Api.VIDEO_DETAIL_SUMMARY_BASE_URL); RetrofitUrlManager.getInstance().putDomain("video_detail_reply", Api.VIDEO_DETAIL_REPLY_BASE_URL); }
Example #8
Source File: MyRetrofitConfiguration.java From LQRBiliBlili with MIT License | 5 votes |
@Override public void configRetrofit(Context context, Retrofit.Builder builder) { // 配置多BaseUrl支持 OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); if (BuildConfig.DEBUG) { clientBuilder.addInterceptor(new LoggingInterceptor());//使用自定义的Log拦截器 } clientBuilder.addInterceptor(new UserAgentInterceptor());//使用自定义User-Agent builder.client(RetrofitUrlManager.getInstance().with(clientBuilder) .build()); }
Example #9
Source File: AppLifecyclesImpl.java From lifecycle-component with Apache License 2.0 | 4 votes |
@Override public void onCreate(@NonNull Application application) { //使用 RetrofitUrlManager 切换 BaseUrl RetrofitUrlManager.getInstance().putDomain(GOLD_DOMAIN_NAME, GOLD_DOMAIN); }
Example #10
Source File: AppLifecyclesImpl.java From lifecycle-component with Apache License 2.0 | 4 votes |
@Override public void onCreate(@NonNull Application application) { //使用 RetrofitUrlManager 切换 BaseUrl RetrofitUrlManager.getInstance().putDomain(GANK_DOMAIN_NAME, GANK_DOMAIN); }
Example #11
Source File: AppLifecyclesImpl.java From lifecycle-component with Apache License 2.0 | 4 votes |
@Override public void onCreate(@NonNull Application application) { //使用 RetrofitUrlManager 切换 BaseUrl RetrofitUrlManager.getInstance().putDomain(ZHIHU_DOMAIN_NAME, ZHIHU_DOMAIN); }
Example #12
Source File: HttpModule.java From Awesome-WanAndroid with Apache License 2.0 | 4 votes |
@Singleton @Provides OkHttpClient provideClient(OkHttpClient.Builder builder) { // 网络请求质量监控 builder.eventListenerFactory(OkHttpEventListener.FACTORY); // httpDns 优化 builder.dns(OkHttpDns.getIns(WanAndroidApp.getAppComponent().getContext())); File cacheFile = new File(Constants.PATH_CACHE); Cache cache = new Cache(cacheFile, 1024 * 1024 * 50); Interceptor cacheInterceptor = chain -> { Request request = chain.request(); if (!CommonUtils.isNetworkConnected()) { // 无网时强制使用数据缓存,以提升用户体验。 request = request.newBuilder() .cacheControl(CacheControl.FORCE_CACHE) .build(); } Response response = chain.proceed(request); if (CommonUtils.isNetworkConnected()) { int maxAge = 0; // 有网络时, 不缓存, 最大保存时长为0 response.newBuilder() .header("Cache-Control", "public, max-age=" + maxAge) .removeHeader("Pragma") .build(); } else { // 无网络时,设置超时为4周 int maxStale = 60 * 60 * 24 * 28; response.newBuilder() .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale) .removeHeader("Pragma") .build(); } return response; }; // 缓存优化 builder.addNetworkInterceptor(cacheInterceptor); builder.addInterceptor(cacheInterceptor); builder.cache(cache); if (BuildConfig.DEBUG) { HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(); loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC); builder.addInterceptor(loggingInterceptor); builder.addNetworkInterceptor(new StethoInterceptor()); } //设置超时 builder.connectTimeout(10, TimeUnit.SECONDS); builder.readTimeout(20, TimeUnit.SECONDS); builder.writeTimeout(20, TimeUnit.SECONDS); //错误重连 builder.retryOnConnectionFailure(true); //cookie认证 builder.cookieJar(new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(WanAndroidApp.getInstance()))); return RetrofitUrlManager.getInstance().with(builder).build(); }
Example #13
Source File: GlobalConfiguration.java From MVPArms with Apache License 2.0 | 4 votes |
@Override public void applyOptions(@NonNull Context context, @NonNull GlobalConfigModule.Builder builder) { if (!BuildConfig.LOG_DEBUG) { //Release 时, 让框架不再打印 Http 请求和响应的信息 builder.printHttpLogLevel(RequestInterceptor.Level.NONE); } builder.baseurl(Api.APP_DOMAIN) //强烈建议自己自定义图片加载逻辑, 因为 arms-imageloader-glide 提供的 GlideImageLoaderStrategy 并不能满足复杂的需求 //请参考 https://github.com/JessYanCoding/MVPArms/wiki#3.4 .imageLoaderStrategy(new GlideImageLoaderStrategy()) //想支持多 BaseUrl, 以及运行时动态切换任意一个 BaseUrl, 请使用 https://github.com/JessYanCoding/RetrofitUrlManager //如果 BaseUrl 在 App 启动时不能确定, 需要请求服务器接口动态获取, 请使用以下代码 //以下方式是 Arms 框架自带的切换 BaseUrl 的方式, 在整个 App 生命周期内只能切换一次, 若需要无限次的切换 BaseUrl, 以及各种复杂的应用场景还是需要使用 RetrofitUrlManager 框架 //以下代码只是配置, 还要使用 Okhttp (AppComponent 中提供) 请求服务器获取到正确的 BaseUrl 后赋值给 GlobalConfiguration.sDomain //切记整个过程必须在第一次调用 Retrofit 接口之前完成, 如果已经调用过 Retrofit 接口, 此种方式将不能切换 BaseUrl // .baseurl(new BaseUrl() { // @Override // public HttpUrl url() { // return HttpUrl.parse(sDomain); // } // }) //可根据当前项目的情况以及环境为框架某些部件提供自定义的缓存策略, 具有强大的扩展性 // .cacheFactory(new Cache.Factory() { // @NonNull // @Override // public Cache build(CacheType type) { // switch (type.getCacheTypeId()){ // case CacheType.EXTRAS_TYPE_ID: // return new IntelligentCache(500); // case CacheType.CACHE_SERVICE_CACHE_TYPE_ID: // return new Cache(type.calculateCacheSize(context));//自定义 Cache // default: // return new LruCache(200); // } // } // }) //若觉得框架默认的打印格式并不能满足自己的需求, 可自行扩展自己理想的打印格式 (以下只是简单实现) // .formatPrinter(new FormatPrinter() { // @Override // public void printJsonRequest(Request request, String bodyString) { // Timber.i("printJsonRequest:" + bodyString); // } // // @Override // public void printFileRequest(Request request) { // Timber.i("printFileRequest:" + request.url().toString()); // } // // @Override // public void printJsonResponse(long chainMs, boolean isSuccessful, int code, // String headers, MediaType contentType, String bodyString, // List<String> segments, String message, String responseUrl) { // Timber.i("printJsonResponse:" + bodyString); // } // // @Override // public void printFileResponse(long chainMs, boolean isSuccessful, int code, String headers, // List<String> segments, String message, String responseUrl) { // Timber.i("printFileResponse:" + responseUrl); // } // }) //可以自定义一个单例的线程池供全局使用 // .executorService(Executors.newCachedThreadPool()) //这里提供一个全局处理 Http 请求和响应结果的处理类, 可以比客户端提前一步拿到服务器返回的结果, 可以做一些操作, 比如 Token 超时后, 重新获取 Token .globalHttpHandler(new GlobalHttpHandlerImpl(context)) //用来处理 RxJava 中发生的所有错误, RxJava 中发生的每个错误都会回调此接口 //RxJava 必须要使用 ErrorHandleSubscriber (默认实现 Subscriber 的 onError 方法), 此监听才生效 .responseErrorListener(new ResponseErrorListenerImpl()) .gsonConfiguration((context1, gsonBuilder) -> {//这里可以自己自定义配置 Gson 的参数 gsonBuilder .serializeNulls()//支持序列化值为 null 的参数 .enableComplexMapKeySerialization();//支持将序列化 key 为 Object 的 Map, 默认只能序列化 key 为 String 的 Map }) .retrofitConfiguration((context1, retrofitBuilder) -> {//这里可以自己自定义配置 Retrofit 的参数, 甚至您可以替换框架配置好的 OkHttpClient 对象 (但是不建议这样做, 这样做您将损失框架提供的很多功能) // retrofitBuilder.addConverterFactory(FastJsonConverterFactory.create());//比如使用 FastJson 替代 Gson }) .okhttpConfiguration((context1, okhttpBuilder) -> {//这里可以自己自定义配置 Okhttp 的参数 // okhttpBuilder.sslSocketFactory(); //支持 Https, 详情请百度 okhttpBuilder.writeTimeout(10, TimeUnit.SECONDS); //使用一行代码监听 Retrofit/Okhttp 上传下载进度监听,以及 Glide 加载进度监听, 详细使用方法请查看 https://github.com/JessYanCoding/ProgressManager ProgressManager.getInstance().with(okhttpBuilder); //让 Retrofit 同时支持多个 BaseUrl 以及动态改变 BaseUrl, 详细使用方法请查看 https://github.com/JessYanCoding/RetrofitUrlManager RetrofitUrlManager.getInstance().with(okhttpBuilder); }) .rxCacheConfiguration((context1, rxCacheBuilder) -> {//这里可以自己自定义配置 RxCache 的参数 rxCacheBuilder.useExpiredDataIfLoaderNotAvailable(true); //想自定义 RxCache 的缓存文件夹或者解析方式, 如改成 FastJson, 请 return rxCacheBuilder.persistence(cacheDirectory, new FastJsonSpeaker()); //否则请 return null; return null; }); }