No Retrofit annotation found. (parameter #1)
AndroidAndroid StudioRetrofitAndroid Problem Overview
I want get RSS code from a URL with Retrofit
and if I enter url staticly in the get annotation everything is OK but with dynamic url I get an error.
My interface service :
public interface AllNewsService {
@GET("/fa/rss/{url}")
void getRss( @Path("url") String nGroup , Callback<AllNewsRss> callback);}
And calling getRss method :
DataGetter dg = new DataGetter();
dg.get().getRss("allnews" ,new Callback<AllNewsRss>() {
@Override
public void success(AllNewsRss allNewsRss, Response response) {
Log.d(TAG,"success");
}
@Override
public void failure(RetrofitError error) {
Log.d("*********",error.toString());
}
I get the following error:
retrofit.RetrofitError: AllNewsService.getRss: No Retrofit annotation found. (parameter #1)
Note: I added below line to proguard.cfg
but it didn't work
-keep class retrofit.** { *; }
Android Solutions
Solution 1 - Android
My problem was that I had the @POST
annotation, but forgot the @Body
annotation for the parameter.
Solution 2 - Android
Addition to Destil's answer
Make sure the parameters
you pass to the retrofit interface methods(i.e callbacks, headers, body )
are only belongs to retrofit package
. e.g. Not custom callbacks that you want on your own.
Solution 3 - Android
Also make sure you add @Query
or @Field
for all the parameters , depending on whether you issuing GET/POST.
eg:
@GET("/api/Books/GetAll")
void GetRecentBooks(@Query int Offset, Callback<List<Book>> cb);
Solution 4 - Android
My problem was that I am using Kotlin coroutines and used 'suspend' in the Retrofit GET method. Just remove it and everything works fine.
Solution 5 - Android
It's way too late, but i just faced the same issue. I was using retrofit 2.5.0 with 'deferred' and 'await' on my retrofit interface and viewModel. And I read from retrofit github that from 2.6.0 deferred is deprecated and we can switch from deferred to 'suspend'. before i moved my retrofit version up, i just simply changed from deferred to suspend and run it, and got the same error. I solved it by moving my retrofit version from 2.5.0 to 2.6.0.
My point is that maybe there may be a chance that current retrofit version in your app doesn't support the way you use retrofit in your retrofit related classes or interfaces.
Solution 6 - Android
I had the @Post
annotation but forgot the @Field
and @FormUrlEncoded
annotations
Solution 7 - Android
You probably forgot to initialise adapter first, as mentioned in retrofit api:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.github.com")
.build();
GitHubService service = restAdapter.create(GitHubService.class);
Solution 8 - Android
(Kotlin) I solved here:
//@FormUrlEncoded // Don't used
@POST("orders")
fun saveOrders(@Body orders: Orders): Call<OrdersResponse>
Solution 9 - Android
Please check the Path
class you are using and make sure that the package is retrofit2.http.Path
instead of org.simpleframework.xml.Path
.
It is a common Retrofit annotation mistake.
Solution 10 - Android
Remove from retrofit interface all parameters that not belong to this.
Solution 11 - Android
In my case I used Retrofit 2.5.0 instead of the newest version. Update to 2.9.0 and thing went fine again.
Solution 12 - Android
In my case, I just forgot to remove Callback
Solution 13 - Android
I forgot to add @Field("")
Previously
@POST("/api/verify-phone")
@FormUrlEncoded
fun sendOTP(phone_number: String): Observable<SignInResponse?>
Fixed
@POST("/api/verify-phone")
@FormUrlEncoded
fun sendOTP(@Field("phone")phone_number: String): Observable<SignInResponse?>
Solution 14 - Android
This happened to me when I was using suspend functions in the network service interface like so.
interface Service {
@GET("chapters")
suspend fun fetchChapters() : List<NetworkChapter>
I solved it by doing 2 things.
- Using Gson instead of Moshi library. Remove moshi converter from retrofit and use Gson converter.
- Removing coroutine converter factory from retrofit. I think this one was the main problem.
Solution 15 - Android
Actual problem for me was old okhttp. Getting same error even updating retrofit to 2.6.0.
okhttp_version = "3.12.1"//remove
okhttp_version = "4.4.0"//works
Solution 16 - Android
make sure you add all dependencies required:
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.5.0'
Solution 17 - Android
In my case I use @Post and @body but use multiple options instead a class when I use give object instead of parameters it solves my problems