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
Related
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.
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.
I have an URL like http://www.example.com/index.php?apiex/teacher/2 and I want http://www.example.com/index.php?apiex/teacher/{teacher_id}, not like this http://www.example.com/index.php?apiex/teacher?teacher_id=2.
Using like getTeacher(#Path("teacher_id") String teacherId) gives me an exception:
java.lang.IllegalArgumentException: URL query string "apiex/teacher/{teacher_id}" must not have replace block. For dynamic query parameters use #Query.
My Retrofit interface is like this:
#GET("apiex/teacher/{teacher_id}")
Observable<List<Teachers>>getTeacher(#Path("teacher_id") String teacherId);
and the base URL is http://www.example.com/index.php?. And also I cant remove ? from this. I saw this, but I wanted it like above.
make the baseurl like this http://www.example.com/index.php/ instead of
http://www.example.com/index.php? as it sees what after ? as query that's why it says that the block after the ? is query
I am trying to replace String in my GET url. Query looks as follow:
#GET("read/something/Books?$filter=(substringof('{filter}',Description)+or+substringof('{filter}',Code)+or+substringof('{filter}',Title)+or+substringof('{filter}',Barcode))")
Call<ApiResponse<Book>> getFilteredBooks(#Path("filter") String filter);
So I want to replace {filter} with dynamic string.
I get an error:
java.lang.IllegalArgumentException: URL query string "$filter=(substringof('{filter}',Description)+or+substringof('{filter}',Code)+or+substringof('{filter}',Title)+or+substringof('{filter}',Barcode))" must not have replace block. For dynamic query parameters use #Query.
I couldn't find any other suitable annotation that would work as expected.
You should use query parameters, like:
#GET("read/something/Books")
Call<ApiResponse<Book>> getFilteredBooks(#Query("$filter") String filter);
This will create url like .../read/something/Books?$filter={parameter you sent}.
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.