Reputation: 247
I want to add or append my url in interface of retrofit. Code of interface is given below.
public interface PostInterface {
@POST("api/v1/app/user/resetpassword/token")
@Headers({
"Content-Type: application/json"
})
Call<JsonObject> getResult(@Body JsonObject body);
}
In the given url @POST("api/v1/wallet/user/resetpassword/token") i want to append token value.Which is value of a variable of an activity.
and my activity code is given below from where i am call the method.
try {
JsonObject params = new JsonObject();
params.addProperty("email", email);
params.addProperty("signup_mode", "mobile");
PostInterface apiService =TestApiClient.getClient(this).create(PostInterface.class);
Call call = apiService.getResult(params);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
Upvotes: 2
Views: 7351
Reputation: 247
Hey i got the answer of my question. Hope if any body is having such problem it can help in future.
Modification in interface:
@POST
@Headers({"Content-Type: application/json"})
Call<JsonObject> getResult(@Url String url, @Body JsonObject body);
Now inside your activity call back please do the following changes
try {
JsonObject params = new JsonObject();
params.addProperty("email", email);
params.addProperty("signup_mode", "mobile");
String url= Constants.BASE_URL+"api/v1/wallet/user/changepassword/"+userIdStr;
PostInterface apiService = TestApiClient.getClient(this).create(PostInterface.class);
Call call = apiService.getChangePassword(url,params);
call.enqueue(new Callback() {
Its working fine.
Upvotes: 0
Reputation: 17085
I'm not entirely sure I understood if this should be part of the path or part of the query parameters, so here's both ways.
Part of the path
The way to do this with retrofit is to make it a "variable" in the path and pass it as arguments to the function.
@POST("api/v1/app/user/resetpassword/{token}")
@Headers({
"Content-Type: application/json"
})
Call<JsonObject> getResult(
@Path("token") String token,
@Body JsonObject body);
Notice the curly braces in {token}
in the url. This tells retrofit that it should format an argument of your method into the url. To know which argument you use the annotation Path
with the same name as the one being formatted. This results in urls like api/v1/app/user/resetpassword/09df7seh98ghs
(09df7seh98ghs is my poor representation of a token).
(this assumes your token is a String. Retrofit supports more than that.)
Part of the query parameters
Similar to the way you do this with the @Path
annotation you can use the @Query
annotation:
@POST("api/v1/app/user/resetpassword/token")
@Headers({
"Content-Type: application/json"
})
Call<JsonObject> getResult(
@Query("token") String token,
@Body JsonObject body);
The difference here is that retrofit will add the given token as a query parameter resulting in urls like api/v1/app/user/resetpassword/token?token=...
Upvotes: 14