问题描述
我有一个 HAL 的API,我用在很多情况下工作,我需要发送请求(用不同的方法),我得到的API回来的URL。这意味着我不想为C网址在我的改造API接口的路径,但我想要的只是通过发送改造到该URL一个简单的要求很难$ C $。我使用的是排球了,我知道我可以使用OkHttp为了这个目的,但我不知道是否有做这样的事情在改造的一个很好的方式?
I have a HAL API that I'm working with and in many cases I need to send request (with different methods) to a URL that I get back from API. Meaning I don't want to hardcode the path of URL in my retrofit api interface but what I want is just sending a simple request using retrofit to that URL.I'm using Volley now and I know that I can use OkHttp for this purpose but I was wondering if there is a nice way of doing such thing in Retrofit?
推荐答案
近日广场发布了改造V2 .0.0 BETA ,它有一个内置的动态URL的支持。即使图书馆是Beta版,基于什么杰克沃顿告诉我们DroidCon纽约2015年,所有的API是稳定的并不会改变。我个人将它添加到我的生产因此它取决于你。
Recently Square has released the Retrofit v2.0.0 BETA and it has a built-in support for dynamic URLs. Even though the Library is in Beta, based on what Jake Wharton told us in DroidCon NYC 2015, all the apis are stable and will not change. I'm personally adding it to my production so its up to you.
如果您决定做了升级你会发现下面的链接有用:
杰克·沃顿presentation @ DroidCon纽约市2015年
上的变化很好的指导作用
You will find the following links useful if you decide to do the upgrade:
Jake Wharton presentation @ DroidCon NYC 2015
A very good guide on the changes
在简单的一句话,你现在可以使用API的注解(如@ GET或@POST等)没有任何路径,然后你传递一个@URL来,该方法将用于调用您的API方法。
In simple word, you can now use the api annotations (like @GET or @POST and others) without any path and then you pass in a @URL to your api method that the method will use to call.
----------------改造1.x的
----------------Retrofit 1.x
我想出了一个好办法这样做,并想与大家分享吧。
I figured out a nice way for doing this and would like to share it.
的技巧是使用动态URL作为你的终点在创造RestAdapter,然后对你的API接口一个空路径。
The trick is to use the dynamic URL as your End Point in the creation of RestAdapter and then have a empty path on your API interface.
下面是我做的:
public RestAdapter getHostAdapter(String baseHost){
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(baseHost)
.setRequestInterceptor(requestInterceptor)
.build();
return restAdapter;
}
我建立使用这种方法我restAdapter,然后我有这个在我的界面:
(这是不行的,如果您的网址已经加入到它的查询参数请参阅解决方案,这种情况下,下一个答案。)
public interface General {
@GET("/")
void getSomething(Callback<SomeObject> callback);
}
和最后使用它们像这样的:
and finally using them like this:
getHostAdapter("YOUR_DYNAMIC_URL").create(General.class)
.getSomething(new Callback<SomeObject>(){
...
})
希望它帮助。
Hope it helps.
这篇关于使用与改造绝对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!