androidpathretrofit

Issue with Retrofit @Path Annotation: API Call Fails to Fetch Data


I'm working on an Android app where I'm using Retrofit to fetch trivia questions from an API. The API endpoint works fine when I hardcode the category in the URL like this:

@GET("questions?categories=general_knowledge&limit=16")
Call<List<Questions>> general();

However this is not work :

@GET("questions?categories={type}&limit=16")
Call<List<Questions>> getData(@Path("type") String type);

Also when I delete the app from emulator, and install again with path include method, it works just once. Not second time.

Questions:

  1. Why does Retrofit's @Path annotation not work as expected in my API call?

  2. How can I properly use @Path to dynamically specify the category type for fetching trivia questions?

  3. What could be causing the API call to work only once after reinstalling the app?

Any insights or solutions would be greatly appreciated!


Solution

  • categories is not a path parameter, it's a query, do this instead

    @GET("questions?limit=16")
    Call<List<Questions>> getData(@Query("categories") String categories);
    

    and about the second problem you mentioned, there's noting in the provided code to find out from