Good afternoon, I need to display the item by the user-entered id. Do I need to change something in the request or onResponse () method?

Request:

public interface RequestInterface { @GET("/posts/{n}") Call <List<Post>> getData(@Query("n") int n); } 

Answer:

 requestInterface.getData(3).enqueue(new Callback<List<Post>>() { @Override public void onResponse(Call<List<Post>> call, Response<List<Post>> response) { posts.addAll(response.body()); recyclerView.getAdapter().notifyDataSetChanged(); } 

Post.java:

 public class Post { @SerializedName("userId") @Expose private int userId; @SerializedName("id") @Expose private int id; @SerializedName("title") @Expose private String title; @SerializedName("body") @Expose private String body; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getBody() { return body; } public void setBody(String body) { this.body = body; } 

    1 answer 1

    In this form, @GET("/posts/{n}") - n is part of the path, so the parameters require the @Path annotation.

    The annotation @Query used for query parameters, which in url will look something like this: http://example.com/posts?n=3 .

    That is, you need to replace either one or the other, depending on what the server expects.

    • Eugeneek, thanks, but how to make the query output the first 5 elements? - Anna13
    • It all depends on how the server is written. Need a description of the API. Perhaps there is even no such request at all - eugeneek