Retrofit 2 Query, get back response?

Stoffe1985 Source

How could i access with the correct syntax: api/kiosk/search/staff?companyID={companyID}&locationID={locationID}&name={name}

Right now i do this:

@GET("api/kiosk/search/staff?companyID={companyID}&locationID={locationID}&name={name}")
Call<Staff[]> staffItems(@Path("companyID") String companyID,@Query("locationId") String locationId, @Query("name") String name, @Header("Authorization")String auth);
javaandroidretrofit2

Answers

answered 5 days ago Santanu Sur #1

You can use dynamic Url with retrofit.. just like the following piece of code..

@GET
public Call<ResponseBody> profilePicture(@Url String url);

Your Url goes here... For eg your retro builder looks like this ..

Retrofit retrofit = Retrofit.Builder()  
.baseUrl("https://your.api.url/");
.build();

  UserService service = retrofit.create(UserService.class);  
  service.profilePicture("https://s3.amazon.com/profile-picture/path");
  // request url results in:
  // https://s3.amazon.com/profile-picture/path

Note - if you pass a completely different Url to the service... okhttp will automatically take the url that you pass , in this case .. https://s3.amazon.com/profile-picture/path .On the other hand if you pass something like this as the url -> service.profilePicture("profile-picture/path"); the full URL will be https://your.api.url/profile-picture/path

So , go ahead and build your dynamic url like this :-

String url = "api/kiosk/search/staff?companyID="+companyID"+"&locationID= "+locationID+"&name="+name"; and then pass the url like ..

and your interface call statement would look like this ...

@GET Call<Staff[]> staffItems(@Url url);

retrofit.getStaffItems(url);

answered 5 days ago ヅ fix #2

Try this

@GET("api/kiosk/search/staff")
Call<Staff[]> staffItems(@Query("companyID") String companyID,@Query("locationId") String locationId, @Query("name") String 
name, @Header("Authorization")String auth);

comments powered by Disqus