Error when using Retrofit

2019-03-05 03:34发布

问题:

I'm trying to get data from web server.I'n new with Retrofit ,and I have error "No Retrofit annotation found (parameter#1)" when try launch my app,I dont understand what cause this error and what to do with that.

VideoApi class:

public interface VideoApi {

    @GET("/videos/featured")
    Call<Video>  getFeaturedVideo(Callback<List<Video>> response);
}

Video class:

public class Video {
    private  String url,title,description;
    private  int score;
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }


}

FragmentClass:

    public class FeaturedFragment extends Fragment {
        public static final String ROOT_URL = "https://api.vid.me";
        private List <Video> videos;
        RecyclerView recList;

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_featured, container, false);
            recList = (RecyclerView) rootView.findViewById(R.id.cardList);
            recList.setHasFixedSize(true);
            LinearLayoutManager llm = new LinearLayoutManager(getActivity());
            llm.setOrientation(LinearLayoutManager.VERTICAL);
            recList.setLayoutManager(llm);
            return rootView;
        }

        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            try {
                getVideos();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private void getVideos() throws IOException {

            Retrofit retrofitAdapter = new Retrofit.Builder()
    .addConverterFactory(GsonConverterFactory.create())
                    .baseUrl(ROOT_URL)
                    .build();
            VideoApi videoApi = retrofitAdapter.create(VideoApi.class);
   In this line// videoApi.getFeaturedVideo(new Callback<List<Video>>() {
        @Override
        public void onResponse(Call<List<Video>> call, Response<List<Video>> response) {
            videos.addAll(response.body());
            RecyclerViewAdapter reciclerViewAdapter = new RecyclerViewAdapter(videos);
            recList.setAdapter(reciclerViewAdapter);
        }

        @Override
        public void onFailure(Call<List<Video>> call, Throwable t) {

        }
    });

        }
    }

My logcat:

04-21 17:40:56.851 636-662/? E/EmbeddedLogger: App crashed! Process: com.example.vid_me_app
04-21 17:40:56.851 636-662/? E/EmbeddedLogger: App crashed! Package: com.example.vid_me_app v1 (1.0)
04-21 17:40:56.851 636-662/? E/EmbeddedLogger: Application Label: Vid_me_app
04-21 17:40:56.851 24066-24066/com.example.vid_me_app E/AndroidRuntime: 
FATAL EXCEPTION: main java.lang.IllegalArgumentException: 
No Retrofit annotation found. (parameter #1) for method VideoApi.getFeaturedVideo
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:695)
at retrofit2.ServiceMethod$Builder.methodError(ServiceMethod.java:686)
at retrofit2.ServiceMethod$Builder.parameterError(ServiceMethod.java:704)
at retrofit2.ServiceMethod$Builder.parseParameter(ServiceMethod.java:343)
at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:201)
at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:166)
at retrofit2.Retrofit$1.invoke(Retrofit.java:145)
at $Proxy1.getFeaturedVideo(Native Method)
at com.example.vid_me_app.FeaturedFragment.getVideos(FeaturedFragment.java:60)
at com.example.vid_me_app.FeaturedFragment.onActivityCreated(FeaturedFragment.java:47)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1983)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1092)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
at android.view.View.measure(View.java:15696)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735)
at android.view.View.measure(View.java:15696)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:15696)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15696)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15696)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15696)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4851)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2261)
at android.view.View.measure(View.java:15696)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2225)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1290)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1527)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1190)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4860)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:766)
at android.view.Choreographer.doCallbacks(Choreographer.java:575)
at android.view.Choreographer.doFrame(Choreographer.java:542)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:751)
at android.os.Handler.han

回答1:

You must use videoApi.getFeaturedVideo(new Callback<List<Video>>() method like that :

Call<List<Video>> call=videoApi.getFeaturedVideo();
call.enqueue(new Callback<List<Video>>() {
            @Override
            public void onResponse(Call<List<Video>> call, Response<List<Video>> response) {


            }

            @Override
            public void onFailure(Call<List<Video>> call, Throwable t) {

            }
        });

And your api :

public interface VideoApi {

    @GET("/videos/featured")
    Call<List<Video>>getFeaturedVideo();
}

Still if you have errors try this too :

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("org.jsonschema2pojo")
public class Video {

@SerializedName("url")
@Expose
private String url;
@SerializedName("title")
@Expose
private String title;
@SerializedName("description")
@Expose
private String description;
@SerializedName("score")
@Expose
private Integer score;

/**
* 
* @return
* The url
*/
public String getUrl() {
return url;
}

/**
* 
* @param url
* The url
*/
public void setUrl(String url) {
this.url = url;
}

/**
* 
* @return
* The title
*/
public String getTitle() {
return title;
}

/**
* 
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}

/**
* 
* @return
* The description
*/
public String getDescription() {
return description;
}

/**
* 
* @param description
* The description
*/
public void setDescription(String description) {
this.description = description;
}

/**
* 
* @return
* The score
*/
public Integer getScore() {
return score;
}

/**
* 
* @param score
* The score
*/
public void setScore(Integer score) {
this.score = score;
}

}

Make sure u have added this to your gradle

compile 'com.google.code.gson:gson:2.4'

And try this site to create your POJO class :Json Schema to POJO