问题
Network call are made like this
@GET("/api/video/{slug}/show")
void getVideoData(@Path("slug") String slug,Callback<VideoContainerGSON>cb);
Now I need to add wariable path before "/api" e.g:
/en/api/video/{slug}/show
Or
/sp/video/{slug}/show
That parameter is global wide, so without setting alteration all network call will use same language path.
Question: Is there a way to alter it without method signature or I must change method signature to
@GET("/{lang}/api/video/{slug}/show")
void getVideoData(@Path("lang") String lang, @Path("slug") String slug,Callback<VideoContainerGSON>cb);
回答1:
You can use string xmls to resolve your issue. You can put your api root in string.xml and override it in other xmls for example:
in values/string.xml
<string name="api_root">http://yourapiroot.com/en</string>in values-es/string.xml
<string name="api_root">http://yourapiroot.com/sp</string>
and when you create your adapter set api root from resources as is shown in the code below:
new RestAdapter.Builder()
.setEndpoint(context.getString(R.string.api_root))
.build()
if you do not want to make the rest api from device language you can set params which defines language in api root in method where you build RestAdapter
And after that you start your paths from /api/... For example:
@GET("/api/video/{slug}/show")
void getVideoData(@Path("slug") String slug,Callback<VideoContainerGSON>cb);
来源:https://stackoverflow.com/questions/29095399/is-it-possible-to-manipulate-retrofit-url-without-method-parameter