Retrofit2 no key name on URL parameter - android

I'm using Retrofit2
I need to ask api with simply get looking like that
http://1.1.1.1/get?key=value1&value2
How can I have query with only value? As value2 in above example?
I've tried like here:
Retrofit no key name on URL parameter
Retrofit change ? sign to %3F.
#Query("") will do something like this &=value2
#QueryMap("") with empty value will od something like this &value2=
Any ideas?

In this kind of case you can directly call entire URL.
#GET()
Call<ResponseVO> test(#Url() String url);
And call
test("http://1.1.1.1/get?key=value1&value2")

With the latest version of Retrofit2 #QueryName
#GET("http://1.1.1.1/get")
Call<Object> getYourData(#Query("key") String key, #QueryName String value);
When you call the method, each query name is appended to the URL. You can see the JavaDoc here: https://square.github.io/retrofit/2.x/retrofit/retrofit2/http/QueryName.html

Please encode query parameter value value1&value2 to value1%26value2 like
http://1.1.1.1/get?key=value1&value2
to
http://1.1.1.1/get?key=value1%26value2
when you passing in
#GET("http://1.1.1.1/get?")
Call<Object> getYourData(#Query("key") String value);

#GET("http://1.1.1.1/get?key=value")
Call<Object> getYourData(#Query("value") String value);
And then when you call "getYourData" just put your value1 and value2 in separate strings and concatenate them and pass that new string to "getYourData" method.
P.S dont forget this sign "&" when merging two strings together.

Related

Why we add Query parameters in Get request?

I am very much new to android and I was studying the Retrofit 2 for networking, to send the Get why we are use the Query parameter?
It is not necessary to send Query Parameters with GET requests. It is something related to how the end point is configured on the API you are trying to consume.
While designing APIs especially GET methods certain parameters can be kept optional by specifying them as query parameters.
#GET("location")
Response getUser(#QueryParam("name") String name);
can be called by both
/location
/location?name=test
Query Parameter is not merely confined to GET requests. It can be used with other methods too e.g., DELETE, etc.
This is a concept related to HTTP methods
Retrofit uses annotations to translate defined keys and values into appropriate format. Using the #Query("key") String value annotation will add a query parameter with name key and the respective string value to the request url (of course you can use other types than string :)).
Actually, there are APIs with endpoints allowing you to pass (optionally) multiple query parameters. You want to avoid a service method declaration like the one below with “endless” options for request parameters:
public interface NewsService() {
#GET("/news")
Call<List<News>> getNews(
#Query("page") int page,
#Query("order") String order,
#Query("author") String author,
#Query("published_at") Date date,
…
);
}
You could call the .getNews service method with null values for each of the parameters to make them optional. Retrofit will ignore null values and don’t map them as query parameters. However, there is a better solution to work with complex API endpoints having various options for query parameters. Don’t worry, Retrofit got you covered!
You can explore more from the given link below:-
https://futurestud.io/tutorials/retrofit-2-add-multiple-query-parameter-with-querymap
Lets say you have following api to call:
https://api.themoviedb.org/3/movie/now_playing/api_key=1
So for you to pass the value for "api_key" dynamically, you should use #Query("api_key") as:
#GET("movie/now_playing")
Call<MovieData> getMovieData(#Query("api_key") String apiKey);
So here is a simple way to understand it for those that might want to use Retrofit query. Please check as follows ....
If you specify #GET("Search?one=5"), then any #Query("two") must be appended using &, producing something like Search?one=5&two=7.
If you specify #GET("Search"), then the first #Query must be appended using ?, producing something like Search?two=7.
That's how Retrofit works.
When you specify #GET("Search?"), Retrofit thinks you already gave some query parameter, and appends more query parameters using &.
Remove the ?, and you will get the desired result.
enter String BASE_URL = "https://api.test.com/";
String API_KEY = "SFSDF24242353434";
#GET("Search") //i.e https://api.test.com/Search?
Call<Products> getProducts(
#Query("one") String one,
#Query("two") String two,
#Query("key") String key
)
Result:
https://api.test.com/Search?one=Whatever&two=here&key=SFSDF24242353434

Retrofit creating endpoints using #Path and #Query parameter

I had to call api endpoints on this query url /api/AssignedStaffClassSection/?StaffId=3071 I tried this way but failed.
Option 1 using #Query parameter
#GET(WebSchoolApi._SAPI + "/AssignedStaffClassSection/")
Call<StaffAssignedClassSectionModel> getAssignedClassSection(#Query("StaffId") String staffId);
Option 2 using #Path parameter
#GET(WebSchoolApi._SAPI + "/AssignedStaffClassSection/?StaffId={staffId}")
Call<StaffAssignedClassSectionModel> getAssignedClassSection(#Path("staffId") String _staffid);
Is there any mistake that I made over?
Thank your for your help.
For GET request you mast to use:
example.com/api/AssignedStaffClassSection/?StaffId=3071
Correct:
#GET(WebSchoolApi._SAPI + "/AssignedStaffClassSection/")
Call<StaffAssignedClassSectionModel> getAssignedClassSection(#Query("StaffId") String staffId);
You should to use #Path for
example.com/api/AssignedStaffClassSection/3071/StaffId
And it will looks like:
#GET(WebSchoolApi._SAPI + "/AssignedStaffClassSection/{staffId}/StaffId")
Call<StaffAssignedClassSectionModel> getAssignedClassSection(#Path("staffId") String _staffid);
Crash may produce if you use wrong tool.

GET URL with constant and dynamic params, with Retrofit2

I have a GET URL like that:
http://myrestapi.com/?method=search&name=nametosearch&format=json
I've written my service like that:
#GET("?method=search")
Observable<List<Album>> getAlbums(#Query("name") String searchedName);
Unfortunately, I don't know how to add &format=json at the end.
I've tried:
#GET("?method=search&name={searched_name}&format=json")
Observable<List<Album>> getAlbums(#Path("searched_name") String searchedName);
But it doesn't work since searched_name is not a Path element.
Can you please help me with that?
If you append &format=json after ?method=search and use #Query("name") then name will be appended after the format parameter. If the server is handling the parameters correctly the order shouldn't matter.
i.e.
#GET("?method=search&format=json")
Single<List<Album>> getAlbums(#Query("name") String name);
Would translate to: http://myrestapi.com/?method=search&format=json&name=name

Retrofit 2 error URL query string must not have replace block

I am trying to call a legacy API using Retrofit 2, this is the URL "/api/0.3/v3/?endpoint=/admin/customers/6728382/addresses.json" and this is the interface method
#GET("/api/0.3/v3/?endpoint=/admin/customers/{customerId}/addresses.json")
Single<GetCustomerAddressesResponse> getUserAddresses(#Path("customerId") String customerId);
However I am getting this error,
"URL query string
"endpoint=//admin/customers/{customerId}/addresses.json" must not have
replace block. For dynamic query parameters use #Query."
How can I fix this?
I think id has to be int type. Try to change String to int.
"URL query string "endpoint=//admin/customers/{customerId}/addresses.json" must not have replace block. For dynamic query parameters use #Query."
As it suggests you should use #Query instead of #Path.
Single<GetCustomerAddressesResponse> getUserAddresses(#Query("customerId") String customerId);
You may like to read this in details about #Query annotation https://square.github.io/retrofit/2.x/retrofit/index.html?retrofit2/http/Query.html

How to query a param in the middle of an URL with Retrofit

I'm reading post and docs about Retrofit 1 & 2. I have the next source code to get a repo from an user.
#GET("users/{user}/repos")
Call<List<GithubRepo>> getRepos(#Path("user") String user);
In retrofit2 I see that now we need to change #Path with #Query, but I don't know if the using method is the same. It's like the next one or I need to change something more?
#GET("users/{user}/repos")
Call<List<GithubRepo>> getRepos(#Query("user") String user);
Thank you
both are different #Query is used
when you have to assign some value in
URL like www.xxx.com/user=name (mostly #query is used to search the user details )
we use like this ....
#GET("users/repos")
Call<List<GithubRepo>> getRepos(#Query("user") String user);
and #path is used when you change the path or URL or keyword of URL
like www.xxx.com/sam ,www.xxx.com/sushan ,etc (mostly #path is used to
fetch data of different user)
we use like this ....
#GET("users/{user}/repos")
Call<List<GithubRepo>> getRepos(#Path("user") String user); //here url changes with the value of String user
NOTE:- #Query always come at end of the URL . And #Path is used anywhere in the URL
Query parameters can also be added.
#GET("group/{id}/users")
Call<List<User>> groupList(#Path("id") int groupId, #Query("sort") String sort);
Nothing must change.

Categories

Resources