No Retrofit annotation found. (parameter #1)

ぃ、小莉子 提交于 2019-12-18 01:22:29

问题


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.** { *; }

回答1:


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.




回答2:


My problem was that I had the @POST annotation, but forgot the @Body annotation for the parameter.




回答3:


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);



回答4:


I had the @Post annotation but forgot the @Field and @FormUrlEncoded annotations




回答5:


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);

http://square.github.io/retrofit/




回答6:


My problem was that I am using Kotlin coroutines and used 'suspend' in the Retrofit GET method. Just remove it and everything works fine.




回答7:


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.




回答8:


Remove from retrofit interface all parameters that not belong to this.




回答9:


In my case, I just forgot to remove Callback parameter for the service call when I start using retrofit 2. Make sure you didn't do the silly mistake I did.




回答10:


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.



来源:https://stackoverflow.com/questions/28371305/no-retrofit-annotation-found-parameter-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!