改造,onResponse方法不起作用

时间:2016-04-21 19:05:35

标签: android retrofit android-recyclerview retrofit2

我是Retrofit的新手,尝试从一个Web服务器获取数据,创建模型,接口,但这仍然无法正常工作。问题(可能)在方法onResponse()我添加到该方法Log.d和Toast但我没看到启动我的应用程序时记录和Toast。为什么不工作?我可以理解当我得到错误的响应或其他什么,但onResponse()一般不工作,我怎么想。也许Toast不能使用数据,但Log.d必须在没有它的情况下工作,而Log.d没有数据,只是代码回应。我添加了所有依赖项并尝试在所有教程中执行此操作,我做了什么错误以及我可以做些什么来解决这个问题?而且我尝试把这个数据放到适配器,但是当启动应用程序时,我在Log“RecyclerView:没有连接适配器;跳过布局”中有错误可能是同样的问题.onResponse不工作且适配器无法创建,因为适配器inilialze在onResponse方法中,如果onResponse不起作用,setadapter到recyclerview不起作用。并且 VideoApi类:

public interface VideoApi {

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

视频课程:

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

}

FeaturedFragment:

public class FeaturedFragment extends Fragment {
    RecyclerViewAdapter recyclerViewAdapter;
    public static final String ROOT_URL = "https://api.vid.me/";
    public 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);
        try {
            getVideos();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rootView;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    private void getVideos() throws IOException {
        Retrofit retrofitAdapter = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
                .baseUrl(ROOT_URL)
                .build();
        final VideoApi videoApi = retrofitAdapter.create(VideoApi.class);
Call<List<Video>> call = videoApi.getFeaturedVideo();
        call.enqueue(new Callback<List<Video>>() {
            @Override
            public void onResponse(Call<List<Video>> call, Response<List<Video>> response) {
                Log.d("MainActivity", "Status Code = " + response.code());
                videos.addAll(response.body());
                recyclerViewAdapter = new RecyclerViewAdapter(videos);
                String result = response.body().get(0).getTitle();
                Toast.makeText(getActivity(), result, Toast.LENGTH_SHORT).show();
                recList.setAdapter(recyclerViewAdapter);
            }

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

            }
        });

    }
}

1 个答案:

答案 0 :(得分:3)

您的json响应返回Video个对象的数组。 将List<Video>对象中的Call更改为Videos 其中Videos类被定义为 -

public class Videos {
    List<Video> videos;
}

像这样改变 -

Call<Videos> call = videoApi.getFeaturedVideo();
        call.enqueue(new Callback<Videos>() {
            @Override
            public void onResponse(Call<Videos> call, Response<Videos> response) {
                Log.d("MainActivity", "Status Code = " + response.code());
                videos = response.body().videos;
                recyclerViewAdapter = new RecyclerViewAdapter(videos);                   
                recList.setAdapter(recyclerViewAdapter);
            }

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

            }
        });

    }

另外,改变 -

@GET("/videos/featured")
Call<Videos>getFeaturedVideo();