Retrofit Query Annotation Without Ampersand

风格不统一 提交于 2020-01-17 05:28:10

问题


I am trying to make an API call to a mobile backend with Retrofit 2.0. In my API call, i have to make necessary call to this URL

https://api.backendless.com/v1/data/Users?where=followings.objectId=%270B3BA7F9-260F-B378-FF9A-3C2448B8A700%27

To form this URL in Retrofit i have been using below interface

@GET("Users?where=")
Call<List<User>> getFollowers(@Query("followings.objectId") String objectId);

This interface call puts an ampersand before the query parameters and generates a URL like below

https://api.backendless.com/v1/data/Users?where=&followings.objectId=%270B3BA7F9-260F-B378-FF9A-3C2448B8A700%27

I tried to overcome this with Path annotation but i keep getting "URL query string must not have replace block. For dynamic query parameters using @Query" error.

API that i am trying to connect requires a "where=" clause for filtering by design. I have no permission to change that. What i want is somehow tell Retrofit not to put an ampersand sign before the query parameter or any workarounds for this issue.

Any help is appreciated.


回答1:


@GET("{path}") Call> getFollowers(@Path("path") path, @Query("followings.objectId") String objectId);

getFollowers("Users?where=", ...)




回答2:


For those who seek for similar answers, I came up with below solution

I declared my interface with @Url

@GET
Call<List<User>> getFollowers(@Url String objectId);

and generated related URL part as an extension method

public String toFollowerRequest(){
    return RestGlobals.BASE_URL + "Users?where=followings.objectId=%27" + objectId + "%27";
}


来源:https://stackoverflow.com/questions/38092895/retrofit-query-annotation-without-ampersand

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