I want to customizer my endPoint using Retrofit2 on Android but I have a doubt.
If I do that:
#GET("Search") //i.e https://api.test.com/Search?
Call<Products> getProducts(#Query("one") String one, #Query("two") String two,
#Query("key") String key)
My endPoint could be like this:
//-> https://api.test.com/Search?one=Whatever&two=here&key=SFSDF24242353434
I'm working with this endPoint:
// -> https://example.com/third-party-public/categories/category_id.json
If I use the same ideia as I explained above the result can be is:
#GET("/third-party-public/categories/")
Observable<List<Category>> getCategoryDetail(#Query(".json") String category_id);
The result could be:
// -> https://example.com/third-party-public/categories/.json=1
But I want that result
// -> https://example.com/third-party-public/categories/1.json
How can I setting my #query for get that result?
#GET("/third-party-public/categories/{category_id}.json")
Observable<List<Category>> getCategoryDetail(#Path("category_id") String category_id);
If you set a request with query it sets as query param '?'.
If the result you want is that you simply use it as in path:
#GET("Search/{fileUri}")
Call<Products> getProducts(#Path("fileUri") String fileUri);
You get the value it as "1.json".
Related
I have this api link:
http://www.xxxx.com/?apikey=mykey&i=tt036543
Now by retrofit I am trying to call this api with GET method so I can create this interface:
#GET("/?apikey=mykey&i={movie_id}")
Call<MovieDetailEntity> getSearchedMovie(#Path("movie_id") String id);
But I got this error:
java.lang.IllegalArgumentException: URL query string "apikey=mykey&i={movie_id}" must not have replace block. For dynamic query parameters use #Query.
for method BatmanService.getSearchedMovie
If I use this Query :
#GET("/?apikey=mykey&i=")
Call<MovieDetailEntity> getSearchedMovie(#Query("movie_id") String id);
Then the link changes to :
http://www.xxxx.com/?apikey=mykey&i=&movie_id=tt036543
How can I call this api?
If I remember well once you use #query you should not set the variable at the url. It will use the string inside the #query annotation as the variable name.
This should work:
#GET("/?apikey=mykey")
Call<MovieDetailEntity> getSearchedMovie(#Query("i") String id);
And also this, if you want to pass your api key as well:
#GET("/")
Call<MovieDetailEntity> getSearchedMovie(#Query("apikey") String mykey,
#Query("i") String id);
The ? and & are automatically added for you.
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);
I'm trying to call the youtube API which looks like this:
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=10&playlistId=UU4MePVEgmibN-KAMF4-heTA&key=<KEY>
But I'm getting the error "URL query string" part = snippet {maxResults} {playlistId} {key} "must not have replace block.For dynamic query parameters use #Query." Am I doing something wrong ?? I'm currently doing so:
#GET("playlistItems?part=snippet{maxResults}{playlistId}{key}")
Call<PlaylistItemListResponse> getPlayListItemsList(#Query("maxResults") int maxresults, #Query("playlistId") String playlistId, #Query("key") String key);
You need to fix the URL in the #GET Annotation.
Please check the Retrofit Documentation at: https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/Query.html
Simple Example:
#GET("/friends")
Call friends(#Query("page") int page);
Calling with foo.friends(1) yields /friends?page=1.
Probably better for your case is using a Query Map.
#GET("images")
Call<Example> getimages(#Query("where=item_id") int item_id);
When I use this the equal to sign after where gets encode to %3D which my server doesn't accept.I want = symbol after where in my api call.
And my link is
images?where=item_id=1
Try this way:
#GET("images")
Call<Example> getimages(#Query("where") String item_id);
When you call this method, you have to pass this way:
Service service = retrofit.create(Service.class);
Call<Example> call = service.getimages("item_id=1");
If you can call your Api successfully, you can pass the value dynamically, using string concatenation.
Reason: When passing query parameters you just have to write query parameter in #Query("") and value to it will be assigned on runtime when you will call this method and pass value to "item_id" parameter of getimages method.
To learn more on Retrofit, refer this link: https://futurestud.io/tutorials/tag/retrofit
Add encoded flag.
#GET("images")
Call<Example> getimages(#Query("where=item_id", encoded = true) String item_id);
and encode item_id before pass it to this method.
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 ¶m=value at the end :)