1

I am trying to query the https://jsonplaceholder.typicode.com/posts api using user input. Right now I use hardcoded text to query it, because I can't figure out how to dynamically query it. I have an activity with an EditText for input and a second activity where the api response is displayed. How do I give the user input to the next activity?

My Interface

public interface PostsApi {
    @GET("posts")       
    Call<List<Posts>> getPosts(@Query("id") int id, @Query("title") String title);

A part of my Retrofit Service Class

public Call<List<Posts>> getPosts(){
        return postsApi.getPosts(2, "qui est esse");  //this should use the user input
    }
Kowalski
  • 11
  • 2
  • Does [this](https://stackoverflow.com/questions/3913592/start-an-activity-with-a-parameter) answers your question? Pass the string to the activity, then send it to your API from there. I also recommend you eventually use fragment screens with a single activity instead of multiple activities. – Nicolas Mar 23 '20 at 16:09

1 Answers1

0

If I understand well your question. You would like to get a specfic post with an Id using the Query Parameter in retrofit. For example, if you would like to make a retrofit call like this:

https://jsonplaceholder.typicode.com/posts/1

So, you have to edit your endpoint from

@GET("posts")       
Call<List<Posts>> getPosts(@Query("id") int id, @Query("title") String title);

to become

@GET("posts/{id}")       
Call<List<Posts>> getPosts(@Query("id") int id, @Query("title") String title);

By this type of query type, you can pass your id to your function. Note: I don't have the api design, I just guess the query parameter of id. But anyway, It gonna be the same for the title.

If you found any problem, kindly reply.

Happy coding

MustafaKhaled
  • 1,343
  • 9
  • 25