Batch request using Retrofit - android

I'd like to perform batch request using Retrofit. It there any nice way, how to achieve it? Basically what I'm trying to do is to replace some characters in query part of URL (replace block is allowed only in path part of URL - using #Path annotation).
Here is a pseudocode for my problem.
#GET("/v2/multi?requests=/users/self,/venues/search?client_id={client_id}&client_secret={client_secret}&v={v}&ll={ll}&intent={intent}&limit={limit}")
ProfileSearchVenuesResponse searchVenuesAndProfiles(#ReplaceBy("client_id") String clientId,
#ReplaceBy("client_secret") String clientSecret,
#ReplaceBy("v") int version,
#ReplaceBy("ll") String location,
#ReplaceBy("intent") String intent,
#ReplaceBy("limit") int limit);

#Query is what you are looking for:
#GET("/v2/multi?requests=/users/self,/venues/search")
ProfileSearchVenuesResponse searchVenuesAndProfiles(
#Query("client_id") String clientId,
#Query("client_secret") String clientSecret,
#Query("v") int version,
#Query("ll") String location,
#Query("intent") String intent,
#Query("limit") int limit);
In version 1.7.0 of Retrofit (released yesterday) the exception message for attempting to use #Path in the original question instructs you as to the right solution:
URL query string "client_id={client_id}&client_secret={client_secret}&v={v}&ll={ll}&intent={intent}&limit={limit}" must not have replace block. For dynamic query parameters use #Query.

Related

Encoding in retrofit url with GET method

I am not able to add special character "&" in my endpoint of the URL.
Here is my api interface
#GET("api/v1/products/artworks/")
Call<TaskData> getLatestTaskData(#Header("Authorization") String token,
#Query("limit") Integer limit,
#Query("offset") String offset);
URL that is getting called
artworks/?limit=50&offset=0%26ordering%3D-artwork
And this is how I am calling the api
String url = Integer.toString(totalitemsCalledLatest) +"&ordering="+ ordering;
Call<TaskData>
call = taskApiInterface.getLatestTaskData("Token "+ user_token, 50, url);
My ordering string is something like &medium=1&base=2
But it converts automatically & to %26 and = to %3D
edit: assuming you are having connection problems to the api, i think you just need to remove the '/' from the end of your GET URL, like this: api/v1/products/artworks or at least that is one solution that will help you get closer to fixing your problem

problems with operators in url in Retrofit 2

I'm trying to learn how to use Retrofit2, and this is the URL I have to generate:
(baseUrl)/repositories?q=language:Python&sort=stars&page=1
This is the method I'm using:
Call<List<Repo>> javaRepos(
#Query("language") String language,
#Query("sort") String sort,
#Query("page") int page
);
and this is how I'm calling it:
Call<List<Repo>> call = client.javaRepos("Python", "stars", 1);
However, this is the url my code generates:
(baseUrl)/repositories?language=Python&sort=stars&page=1
The differences are:
the q= is missing;
language is followed by a = instead of a :
How can I generate the correct url using #Query parameters (or any other way, actually)?
Looks like you're misinterpreting the query string of your desired url.
q=language:Python&sort=stars&page=1 should be broken down into three key-value pairs:
q - language:Python
sort - stars
page - 1
Note that the first key is q and not language.
With that in mind, your method should look like this (and you'll have to pass "language:Python" instead of just "Python" as the first argument).
Call<List<Repo>> javaRepos(
#Query("q") String language,
#Query("sort") String sort,
#Query("page") int page
);
You need to use Path annotation according to this link : https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/Path.html
#GET("repositories?q=language:{lang}&sort={sort}&page={page}")
Call<List<Repo>> javaRepos(#Path(value = "lang") String lang, #Path(value = "sort") String sort, #Path(value = "page") int page);

Best approach of defining endpoints for pagination retrofit2

I was thinking which should be the preferred way of defining endpoints in Retrofit2 for pagination.
Should we separate the endpoints or should we define only one endpoint method and pass page=null?
#GET("/search/users")
Observable<FetchUserResponse> fetchUsers(#Query("q") String uname,
#Query("sort") String followerspage);
#GET("/search/users")
Observable<FetchUserResponse> fetchUsersPaginationCall(#Query("q") String uname,
#Query("sort") String followerspage,
#Query("page") int page);
or
#GET("/search/users")
Observable<FetchUserResponse> fetchUsers(#Query("q") String uname,
#Query("sort") String followers,
#Query("page") String page);
Adjustments must happen on the API itself, it must set a default on a page if it is null or missing on the query param. You should define one end point only for this.

How to use #Query in URL in Retrofit?

Hi all i call below URL using Retrofit
https://api.stackexchange.com/2.2/me?key=gQJsL7krOvbXkJ0NEI((&site=stackoverflow&order=desc&sort=reputation&access_token=HM*22z8nkaaoyjA8))&filter=default
and for that i created Interface RestInterface
//get UserId
#GET("/me&site=stackoverflow&order=desc&sort=reputation&access_token={access_token}&filter=default")
void getUserId(#Query("key") String apikey,#Path("access_token") String access_token,Callback<UserShortInfo> cb);
When i do this it always add key at the end of the URL(Output below).
I added #Query("key") as Query Parameter becoz it's dynamic.
http://api.stackexchange.com/2.2/me&site=stackoverflow&order=desc&sort=reputation&access_token=p0j3dWLIcYQCycUHPdrA%29%29&filter=default?key=gQJsL7krOvbXkJ0NEI%28%28
and that's the wrong. I got HTTP 400. Also here (( and )) converted into %28%28 and %29%29
Please help me how to make
https://api.stackexchange.com/2.2/me?key=gQJsL7krOvbXkJ0NEI((&site=stackoverflow&order=desc&sort=reputation&access_token=HM*22z8nkaaoyjA8))&filter=default
in Retrofit. I want it add #Query parameter in between URL. not at the end of the URL
Don't put query parameter inside the URL only the path parameter you can add
#GET("/me?site=stackoverflow&order=desc&sort=reputation&filter=default)
void getUserId(#Query("key") String apikey,#Query("access_token") String access_token,Callback<UserShortInfo> cb);
#Query("access_token") --> given key and value will come query URL
While sending request your URL form will like below
/me?key=?site=stackoverflow&order=desc&sort=reputation&filter=default&"your_value"&access_token="your_value"
obviously instead
#GET("/me&site=stackoverflow&order=desc&sort=reputation&access_token={access_token}&filter=default")
void getUserId(#Query("key") String apikey,#Path("access_token") String access_token,Callback<UserShortInfo> cb);
you should use
#GET("/me?site=stackoverflow&order=desc&sort=reputation&filter=default")
void getUserId(#Query("key") String apikey,#Query("access_token") String access_token,Callback<UserShortInfo> cb);
the chanege is /me& to /me? ... (and second is to use access_token as Query param too, not as a Path)
edit more explanations:
After parsing url which looks like (me&)
scheme:host/me&blablalbal=blalbla&blabla=blabla
the path is
me&blablalbal=blalbla&blabla=blabla
and there is no query paramas at all ... so adding params end with adding ?param=value at the end
but with (me?)
scheme:host/me?blablalbal=blalbla&blabla=blabla
the path is
me
and there are already some query parameters ... so addin new end with adding &param=value at the end :)

Retrofit: how fix "only one http method is allowed. found: get and get"?

I have that structure for request.
Request getTransportByStation work perfectly.
But I get exception java.lang.IllegalArgumentException: TransportWebService.getTransportByRoute: Only one HTTP method is allowed. Found: GET and GET.
I found solution only for POST and POST.
interface TransportWebService {
#GET(QUERY_CATEGORY_TRANSPORT + "GetTransportByNextStation/{station}")
Observable<ResponseRouteList> getTransportByStation(
#Path("city") String city,
#Path("station") String station,
#Query("count") int count,
#Query("userid") String userId
);
#GET(QUERY_CATEGORY_TRANSPORT + "GetTransportByRoute/{route}")
Observable<ResponseRouteList> getTransportByRoute(
#Path("city") String city,
#Path("station") String route,
#Query("count") int count,
#Query("userid") String userId
);
#GET(QUERY_CATEGORY_TRANSPORT + "Time")
Observable<Integer> getTime(
#Path("city") String city
);
}
UPD: Retrofit version 1.9.0
Init service like this
private static final TransportWebService SERVICE = Common.getRestAdapter()
.setConverter(new GsonConverter(new Gson())
.build()
.create(TransportWebService.class);
In the second GET method, the second argument (#PATH("station")) should be #PATH("route").
I had the same error found: POST and POST, my issue was that I was missing a path parameter in my URL but I was sending it along with the request.
I was hitting the URL without last parameter.
http:www.webtest.requestService/customerapp/{context}/{token}/services/{flightLegId}/
Actual URL was should be:
http:www.webtest.requestService/customerapp/{context}/{token}/services/{flightLegId}/{flightLegExtId}/
This may happen because of tiny mistake like if you are giving parameter like instead of {id}.
The error the "GET and GET" or "POST and POST" error is masking the root exception. It seems the most common exception is that your argument in the HTTP argument method does not match the argument in your Path parameter.
The {id} in #GET("/objects/{id}") must match id in #Path("id").

Categories

Resources