2

I'm following a tutorial online to get information from a local API using retrofit2, but when I run my code I get:

java.lang.IllegalArgumentException: No Retrofit annotation found. (parameter #1)

I notice most answers online have this problem when POSTing but all I'm trying to do is GET all the items in the database. The API part o this works and when I go to the local IP (10.0.2.2) on the emulator it gives me what I want. I don't understand why I'm getting this problem, actually still trying to fully understand the base code. I also saw some solutions using .enqueue but I guess i don't fully understand how to use it.

@GET("/institute/Students")
    void getStudent(Callback<List<Student>> callback);
 InstituteService serv = restService.getService();
 serv.getStudent(new Callback<List<Student>>() {...}

I'm trying to get a list of students from my database but I'm stuck right here. After initiating the Callback it goes straight to the onFailure activity. Any point in the right direction appreciated.

Kristin Vernon
  • 155
  • 1
  • 2
  • 20
  • Possible duplicate of [No Retrofit annotation found. (parameter #1)](https://stackoverflow.com/questions/28371305/no-retrofit-annotation-found-parameter-1) – Ghasem Sadeghi Sep 06 '19 at 18:53
  • For that one it seems that they wrote a custome Callback but I haven't written any custom Callbacks of my own. So that answer didn't really solve my question. – Kristin Vernon Sep 06 '19 at 21:02

2 Answers2

2

These interface descriptions should return Call instead of void:

import retrofit2.Call;
import retrofit2.http.GET;
...

@GET("institute/students")
Call<ArrayList<Student>> getStudents();
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Why should it return ```Call```? I changed to: ``` @GET("/institute/Students") Call> getStudent(Callback> callback); ``` But no luck :( – Kristin Vernon Sep 06 '19 at 20:57
  • @KristinVernon it is not what I've wrote; there is no parameter `Callback` (whatever this may be), because it returns a `Call` with `onResponse` and `onFailure` callback. – Martin Zeitler Sep 06 '19 at 21:01
  • Oh I see. But how can I save the response in a ListView? Currently my onResponse takes the response.body() and puts it in a custome adapter for the listview – Kristin Vernon Sep 06 '19 at 21:05
  • just set `ArrayList` to an array-adapter. when class `Student` is properly annotated, `response.body()` can be auto-mapped to `ArrayList`. – Martin Zeitler Sep 06 '19 at 21:08
  • Yes that fixed it! Thank you so much! – Kristin Vernon Sep 06 '19 at 21:19
1

Noticed that you are using callback not call and that too I guess goes as return type not as a parameter. This interface will return list of students, you can pass query parameters to function if required (eg. StudentId)

@GET("institutes/students/") 
Call<List<Student>> getStudentsData();

Please see this article for more information

akshay bhange
  • 2,320
  • 2
  • 28
  • 46