cool hit counter Solving Retrofit Multiple BaseUrl and Dynamically Changing BaseUrl at Runtime (II)_Intefrankly

Solving Retrofit Multiple BaseUrl and Dynamically Changing BaseUrl at Runtime (II)


Original article address: https://www.jianshu.com/p/35a8959c2f86

preamble

me (located) at prior to of essay Solving Retrofit Multiple BaseUrl and Dynamically Changing BaseUrl at Runtime In this article, we present 4 common solutions to this problem on the market and open source our own optimized solution RetrofitUrlManager, current (located) at again because of Everyone brings this series of Second article, This article focuses on RetrofitUrlManager in connection with BaseUrl Replacement logic of Major upgrades, reason because of this one Upgrade for RetrofitUrlManager important enough that it will make RetrofitUrlManager able to handle more complex needs with ease, so a separate article is being written so that more people can know

Github : you of Star be I insist. of power ✊

Why not use a solution with multiple Retrofit instances?

In the previous article Solving Retrofit Multiple BaseUrl and Dynamically Changing BaseUrl at Runtime in,4 variety of options of distinction harmony I'll describe all the shortcomings. of It's clear., Suggest not having read this article of You can check out this article, broaden one's knowledge, (located) at afterwards of I'm often asked in time because of What doesn't? using multiple Retrofit instantiated options, several Retrofit Instances don't seem to take up much resources, do they?

Before I answer so that anyone reading this can understand what I'm talking about, so I'll paste it again previous article Part of the description of this programme in

Common folk solutions: I've seen a lot of open source before of polymerization App source code (computing), Like some integration intimate knowledge、 Douban, PRC social networking website、 Gank Data from multiple platforms such as of App, reason because of Individual platforms of the domain name different, So most of these types of App will create a separate Retrofit targets, i.e. different of BaseUrl Using different of Retrofit object to create the ApiService Make a request, This way simply adding a different of BaseUrl, Then you need to recreate a new of Retrofit targets

me (located) at Re-answered in this post this one issues, because of differently of BaseUrl Both create one with all the other configuration properties exactly the same of Retrofit Instances are not only a waste of resources, but also an increase in interface management costs, which is the most important point, for example

All of our usual projects in the ApiService (not) at all be using the same Retrofit instantiated Retrofit#create(ApiService) method to instantiate and then start the interface of requesting

however be When there are multiple Retrofit instances later, we have to distinguish in our development not only which interfaces use which ApiService What else is there to distinguish between ApiService Which one needs to be used Retrofit instances are instantiated, if ApiService Using the wrong Retrofit Instances are instantiated, that this one ApiService of All interface requests are doomed to complete failure

The more complex the project, and the more developers there are, the greater the risk of error, and the less scalable it is, and the more painful it will be to make changes later on, as more interfaces are added to the project, and Retrofit instantiated increase (BaseUrl of increase), this one Management costs can be geometrically multiplied of increase

using multiple Retrofit The upfront investment cost of the instantiated solution is too high and may affect the way the previous project management interface, some encapsulation over Retrofit of sports event, It may also need a major overhaul, For older projects of access disadvantage, pertaining to the use of RetrofitUrlManager Not only can you meet the needs of multiple BaseUrl and dynamic changes at runtime BaseUrl of need, Also features hot-swappable and less invasive of distinction, (located) at The use of the process will not affect the previous of Interface Management Method harmony Usage, It also has an extremely strong of scalability, Can be added later of extremely complex of BaseUrl Replacement needs

Problems with upgrading the previous RetrofitUrlManager

Before this upgrade of RetrofitUrlManager version, which simply sets the previous article of Ideas fully realized, With the whole framework of infrastructural, however be (located) at Dynamic replacement BaseUrl Not strong enough in terms of, Most trolled by everyone of even Replacement only BaseUrl of the domain name

For example, a program that needs to replace BaseUrl of Url address because of "https:www.google.com/api/v2" of which "https:www.google.com/api" be We passed it on to Retrofit of BaseUrl and at this point we use RetrofitUrlManager framework, want to put BaseUrl Replace with "https:www.github.com" , we expect the replaced URL address be "https:www.github.com/v2", But after using the frame replacement of reality URL address be "https:www.github.com/api/v2", "/api" act as BaseUrl Part of it is not being used by the new BaseUrl swap out, merely be Replaced BaseUrl Domain name in

RetrofitUrlManager be How to improve of

Analyze before you improve because of What would do that?? reason because of RetrofitUrlManager The framework intercepts the interceptor in the URL address be Retrofit Already put BaseUrl and the relative paths in the interface annotations are merged to get the final path address, so the framework doesn't know the relative paths you pass to the Retrofit of BaseUrl In addition to the domain name it also contains the following "/api" The framework is not known BaseUrl of specific value, So the framework will just default to all of BaseUrl Both contain only domain names, so it's only possible to replace them

Advanced mode

It is also easy to solve such problems by telling RetrofitUrlManager The frame you pass to Retrofit of BaseUrl The specific value is sufficient, so the framework was upgraded with the addition of RetrofitUrlManager#startAdvancedModel(String) The method, in App Initialize by passing you to the Retrofit of BaseUrl Also pass this method to enable advanced mode, which can replace having multiple pathSegments of BaseUrl No longer limited to replacing domain names only

what? be pathSegment?

(located) at "https://www.github.com/wiki/part?name=jess" in of which of "/wiki" harmony "/part" even pathSegment, PathSize even pathSegment Number of

this one URL addresses PathSize even 2, the first pathSegment be "/wiki" The second one. pathSegment be "/part", can be roughly of understandable because of domain name There's a couple of them behind us. "/" PathSize even how many

Advanced mode of Replacement rules

  1. worn (with age) URL address because of "https://www.github.com/wiki/part" , you are in App Initialize by passing in RetrofitUrlManager#startAdvancedModel(String) of BaseUrl because of "https://www.github.com/wiki" You may want to replace it with the BaseUrl address be "https://www.google.com/api" , the final URL address because of "http://www.google.com/api/part"
  2. worn (with age) URL address because of "https://www.github.com/wiki/part" , you are in App Initialize by passing in RetrofitUrlManager#startAdvancedModel(String) of BaseUrl because of "https://www.github.com/wiki" You may want to replace it with the BaseUrl address be "https://www.google.com" , the final URL address because of "http://www.google.com/part"
  3. worn (with age) URL address because of "https://www.github.com/wiki/part" , you are in App Initialize by passing in RetrofitUrlManager#startAdvancedModel(String) of BaseUrl because of "https://www.github.com" You may want to replace it with the BaseUrl address be "https://www.google.com/api" , the final URL address because of "http://www.google.com/api/wiki/part"

Super Mode

Super Mode is an enhanced version of Advanced Mode, with a higher priority than Advanced Mode, and it is logical that Advanced Mode will be able to meet most of the needs in development, so what is Super Mode again? Then we'll have to start with the advanced mode

Advanced mode of principle

(located) at Advanced mode in which you need (located) at App Initialize by passing you to the Retrofit of BaseUrl The same passes to RetrofitUrlManager#startAdvancedModel(String) one copy, used to turn on Advanced mode, Successfully opened Advanced mode back, this one pass on (to sb) RetrofitUrlManager#startAdvancedModel(String) of BaseUrl will then be replaced as a frame BaseUrl benchmarks

What do you mean by benchmarking? using this BaseUrl turn on Advanced mode, doesn't mean that the frame can only replace domain name because of "www.github.com" The first two pathSegments because of "/wiki/part" of URL, Just have domain name and Greater than or equal to two pathSegments of URL All can be replaced by frames, therefore Advanced mode will only save incoming RetrofitUrlManager#startAdvancedModel(String) of BaseUrl of Format ( save pathSegments Number of), not at all be Save specific of happen to

Limitations of the advanced model

If passed to RetrofitUrlManager#startAdvancedModel(String) of BaseUrl because of "https://www.github.com/wiki/part" (PathSize = 2), the framework will then take all of the project's URL in of domain name and domain name afterwards of The first two pathSegments act as Replaceable of BaseUrl as a whole, meaning that the framework will only put URL in of domain name and The first two pathSegments Cut and replace with your desired BaseUrl

That's when the server made a sudden adjustment, Project in of part of URL Simply place the "https://www.github.com/wiki" (PathSize = 1) Replace with, second pathSegment "/part" No longer as BaseUrl part of the process cannot be replaced, it must be preserved

At this point there are multiple items in the project that need to be replaced of BaseUrl Format (PathSize different), some URL Just replace the domain name and The first two pathSegments, some of them Just replace the domain name and the previous one pathSegments , but when advanced mode is turned on, only a BaseUrl format, it is tricky to implement this requirement using the advanced mode

This need is a rather sick one that many people may not encounter, but I want you to know that when you do, don't be afraid, because RetrofitUrlManager The super model has been thought through for you

Super Mode of usage

Super Mode and Advanced mode different of be, turn on Super Mode There is no need to call API You only need to turn on Super Mode in the Url Tail plus RetrofitUrlManager#IDENTIFICATION_PATH_SIZE (#baseurl_path_size=) + PathSize, This makes it clear that of Told the framework, (located) at this one URL in the BaseUrl contains several pathSegments This corresponds to each URL All of them can specify their own need to be replaced by the BaseUrl of format, This makes it easier than Advanced mode can only (located) at App When initializing, specify a global BaseUrl format Much more flexible , if When you turn it on Advanced mode of It's also on Super Mode, because Super Mode of Priority is higher Advanced mode, So it will only be executed Super Mode

Supermode replacement rules

  1. worn (with age) URL address because of "https://www.github.com/wiki/part#baseurl_path_size=1""#baseurl_path_size=1" indicates that where BaseUrl because of "https://www.github.com/wiki" You may want to replace it with the BaseUrl address be "https://www.google.com/api" , the final URL address because of "http://www.google.com/api/part"
  2. worn (with age) URL address because of "https://www.github.com/wiki/part#baseurl_path_size=1""#baseurl_path_size=1" indicates that where BaseUrl because of "https://www.github.com/wiki" You may want to replace it with the BaseUrl address be "https://www.google.com" , the final URL address because of "http://www.google.com/part"
  3. worn (with age) URL address because of "https://www.github.com/wiki/part#baseurl_path_size=0""#baseurl_path_size=0" indicates that where BaseUrl because of "https://www.github.com" You may want to replace it with the BaseUrl address be "https://www.google.com/api" , the final URL address because of "http://www.google.com/api/wiki/part"
  4. worn (with age) URL address because of "https://www.github.com/wiki/part/issues/1#baseurl_path_size=3""#baseurl_path_size=3" indicates that where BaseUrl because of "https://www.github.com/wiki/part/issues" You may want to replace it with the BaseUrl address be "https://www.google.com/api" , the final URL address because of "http://www.google.com/api/1"

Comparison of the three models

(located) at before upgrading, The framework then has only one default of Normal mode ( Replacement only domain name), (located) at Added after upgrade Advanced mode harmony Super Mode, These two patterns make the framework more powerful, (located) at topmost of The content is also detailed of These two patterns are described, current (located) at Just come conclude the next three modes, So that everyone can follow their own of The requirements are selected to best suit of mode

Degree of freedom to replace BaseUrl (extensibility)

Normal mode < Advanced mode < Super Mode

  • Normal mode: Only replace domain names
  • Advanced mode: Replacement only (located) at RetrofitUrlManager#startAdvancedModel(String) The fixed incoming BaseUrl format
  • Super Mode: Each URL are free to specify replaceable BaseUrl format

Complexity in use

Normal mode < Advanced mode < Super Mode

  • Normal mode: No need to do much configuration
  • Advanced Mode: In App Called once on initialization RetrofitUrlManager#startAdvancedModel(String) can then (do sth)
  • Super Mode: Each Need to turn on Super Mode of URL The tails all need to be added RetrofitUrlManager#IDENTIFICATION_PATH_SIZE (#baseurl_path_size=) + PathSize

conclude

As you can see, the more freedom you have, the more complex it is to use, so you can choose the corresponding mode according to your needs, and you can also upgrade or downgrade the three modes at will as your needs change

This update allows RetrofitUrlManager The ability to take it up a notch, enough to handle a variety of complex BaseUrl replacement needs, precisely because RetrofitUrlManager extremely strong of scalability, current (located) at It can even be done, Enables the server to dynamically control the project via remote of several BaseUrl

If you have any more questions or needs, you can ask me Issues , if RetrofitUrlManager Can bring you substance of assist, Please don't be stingy with you of Star


Hello My name is Jessyan, if you like my articles, you can follow me on the following platforms

-- The end


Recommended>>
1、Special byte alignment issues
2、Recommended penetration test plugins for FirefoxChrome
3、Information Monthly August News A mustread collection series
4、Plain Bayesian classification the Laplace correction
5、A full explanation of how the ValueAnimator works

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号