Is it possible to manipulate retrofit url without method parameter?

感情迁移 提交于 2020-01-14 04:30:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!