
我需要从USER输入的域中调用API,我需要在调用插入数据之前编辑我的Retrofit单例.
有没有办法“重置”我的单身人士,迫使它重新创建?
要么
有没有办法在调用之前用我的数据(可能在Interceptor中)更新我的baseUrl?
码
单身
@ProvIDes@SingletonRetrofit provIDeRetrofit(SharedPreferences prefs) { String APIUrl = "https://%1s%2s"; APIUrl = String.format(APIUrl,prefs.getString(ACCOUNT_SUBDOMAIN,null),prefs.getString(ACCOUNT_DOMAIN,null)); OkhttpClIEnt httpClIEnt = new OkhttpClIEnt.Builder() .addInterceptor(new headerInterceptor()) .build(); return new Retrofit.Builder() .baseUrl(APIUrl) .addConverterFactory(GsonConverterFactory.create()) .clIEnt(httpClIEnt) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build();}@ProvIDes@SingletonAPI provIDeAPI(Retrofit retrofit) { return retrofit.create(API.class);} API
@FormUrlEncoded@POST("endpoint")Observable<Response> logIn(@FIEld("login") String login,@FIEld("password") String password); 它现在如何运作
好的想法是在API调用之前通过SharedPrefs保存用户域数据,并使用格式化的String修改baseUrl.
解决方法 我在这里看到2个选项:>按照预期使用匕首.为每个baseUrl创建自己的Retrofit客户端,或者
>在发送请求之前使用拦截器修改请求
匕首的方法
如果您使用暴力网址,这可能不是正确的选择,因为它依赖于为每个网站创建一个新的Retrofit实例.
现在每次网址更改时,您只需通过为其提供新的UrlModule来重新创建以下演示的UrlComponent.
清理
清理你的@Singleton模块,以便它提供GsonConverterFactory和RxJavaCallAdapterFactory以正确使用dagger而不重新创建共享对象.
@Modulepublic class SingletonModule { @ProvIDes @Singleton GsonConverterFactory provIDeOkhttpClIEnt() {/**/} @ProvIDes @Singleton RxJavaCallAdapterFactory provIDeOkhttpClIEnt() {/**/}}@Singleton@Component(modules = SingletonModule.class)interface SingletonComponent { // sub component UrlComponent plus(UrlModule component);} Url Scoped
引入@UrlScope来扩展您的Retrofit实例.
@Scope@Retention(RetentionPolicy.RUNTIME)public @interface UrlScope {} 然后创建一个子组件
@SubComponent(modules=UrlModule.class)public interface UrlComponent {} 还有一个模块
@Moduleclass UrlModule { private final String mUrl; UrlModule(String url) { mUrl = url; } @ProvIDes String provIDeUrl() { return mUrl; } @ProvIDes @UrlScope OkhttpClIEnt provIDeOkhttpClIEnt(String url) { return new OkhttpClIEnt.Builder().build(); } @ProvIDes @UrlScope Retrofit provIDeRetrofit(OkhttpClIEnt clIEnt) { return new Retrofit.Builder().build(); }} 使用范围改造
实例化组件并使用它.
class Dagger { public voID demo() { UrlModule module = new UrlModule(/*some url*/); SingletonComponent singletonComponent = DaggerSingletonComponent.create(); UrlComponent urlComponent = singletonComponent.plus(module); urlComponent.getRetrofit(); // done. }} Okhttp方法
提供适当范围的拦截器(在本例中为@Singleton)并实现相应的逻辑.
@Moduleclass SingletonModule { @ProvIDes @Singleton GsonConverterFactory provIDeGsonConverter() { /**/ } @ProvIDes @Singleton RxJavaCallAdapterFactory provIDeRxJavaCallAdapter() { /**/ } @ProvIDes @Singleton MyAPIInterceptor provIDeMyAPIInterceptor() { /**/ } @ProvIDes @Singleton OkhttpClIEnt provIDeOkhttpClIEnt(MyAPIInterceptor interceptor) { return new OkhttpClIEnt.Builder().build(); } @ProvIDes @Singleton Retrofit provIDeRetrofit(OkhttpClIEnt clIEnt) { return new Retrofit.Builder().build(); }}@Singleton@Component(modules = SingletonModule.class)interface SingletonComponent { Retrofit getRetrofit(); MyAPIInterceptor getInterceptor();} todo实现MyAPIInterceptor.您将需要为基本URL设置一个setter,然后只需重写/修改通过的请求.
然后,再次,继续使用它.
class Dagger { public voID demo() { SingletonComponent singletonComponent = DaggerSingletonComponent.create(); MyService service = singletonComponent.getRetrofit().create(MyService.class); MyAPIInterceptor interceptor = singletonComponent.getInterceptor(); interceptor.setBaseUrl(myUrlA); service.doA(); interceptor.setBaseUrl(someOtherUrl); service.dob(); }} 作为第三种方法,您还可以使用反射直接更改基本URL – 我为了完整性而添加了最后一个.
总结以上是内存溢出为你收集整理的android – Dagger Retrofit动态URL全部内容,希望文章能够帮你解决android – Dagger Retrofit动态URL所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)