绝对URL的改进和使用

时间:2014-07-16 13:58:49

标签: retrofit

如何处理某些调用的Retrofit绝对URL?

我正在使用匕首,改装和OKHttp的组合。我提供了一个@Singleton RestAdapter,其中包含选定的端点(生产,登台等),它是从BASE URL形成的。

@Provides
    @Singleton
    RestAdapter providesRestAdapter(Endpoint endpoint,
                                    ObjectMapper jacksonObjectMapper,
                                    Client client,
                                    ApiRequestInterceptor headers) {

        return new RestAdapter.Builder() //
                .setClient(client) //
                .setEndpoint(endpoint) //
                .setConverter(new JacksonConverter(jacksonObjectMapper))
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setRequestInterceptor(headers) //
                .build();
    }

问题是REST服务在某些情况下会返回要以ABSOLUTE格式加载的内容的URL,这意味着http://someurl.com/resource?sort=ascendent而不是首选/ resouce?sort = ascendent(没有BASE_URL)

现在问题是我如何创建服务接口来处理这种情况?

我在想这样的事情:

public interface PlaceholdersService {

    @GET("{placeholderHref}")
    public void getPlaceholder(@EncodedPath("placeholderHref") String placeholderHref);

}

问题是我不确定是否会添加先前在创建RestAdapter时在端点中设置的BASE URL(目前无法测试)。

另一种方法是专门为这种情况创建一个新的RestAdapter,将Endpoint设置为null或空字符串固定点,如Endpoints.newFixedEndpoint(null)或Endpoints.newFixedEndpoint(“”)。

有什么建议吗?

由于

2 个答案:

答案 0 :(得分:0)

挖掘一点我刚才意识到在Github https://github.com/square/retrofit/issues/333

这个方式存在一个未解决的问题

已将讨论移至库的V2,以讨论该版本是否支持。

这意味着如果支持,暂时不会这样做。您是否有任何建议或解决方法可以建议暂时超越此问题?

答案 1 :(得分:0)

我有类似的问题,我通过创建具有指定URL的新适配器并在我的界面中使用空路径来解决它。 Here是我的问题和我自己的答案。

希望它有所帮助。