使用Retrofit时出错

时间:2016-04-21 13:59:32

标签: android retrofit retrofit2

我正在尝试从网络服务器获取数据。我是Retrofit的新用户,我在尝试启动我的应用时遇到错误"No Retrofit annotation found (parameter#1)",我不明白导致此错误的原因是什么这样做。

VideoApi类:

public interface VideoApi {

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

视频课程:

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

        }
    });

        }
    }

我的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 个答案:

答案 0 :(得分:1)

您必须使用videoApi.getFeaturedVideo(new Callback<List<Video>>()方法:

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

            }
        });

你的api:

public interface VideoApi {

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

如果您有错误,请尝试这样做:

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

}

确保您已将此添加到您的gradle

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

并尝试使用此网站创建您的POJO课程:Json Schema to POJO