改造,onResponse方法不起作用 [英] Retrofit,onResponse method doesnt work

查看:302
本文介绍了改造,onResponse方法不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Retrofit的新手,尝试从一台Web服务器获取数据,创建Model,Interface,但这仍然无法正常工作.方法onResponse()中的问题(也许)我添加到该方法Log.d和Toast中,但是我看不到启动我的应用程序时记录并祝酒,为什么不起作用?我可以理解我何时收到错误的响应或其他错误,但是onResponse()通常无法正常工作,我是怎么想的.也许Toast无法使用超级数据,但是Log.d必须在没有它的情况下工作,而Log.d没有数据,只是代码反应.我添加了所有缺陷,并且tryind像所有教程中一样执行此操作,我做了什么错误以及如何解决该问题?并且我也尝试将这些数据放入适配器,但是启动应用程序时,我在日志"RecyclerView:未连接适配器;跳过布局"中遇到错误,可能是相同的问题.onResponse无法正常工作且适配器未创建,因为适配器inilialze在onResponse方法中,并且如果onResponse不起作用,则setadapter到recyclerview也不起作用. VideoApi类:

Im new in Retrofit,try to get data from one web server,create Model,Interface but this still not working.Problem(maybe) in method onResponse() I add to that method Log.d and Toast but I dont see Log and Toast when launch my app.Why that dont work? I can understand when I get wrong response or something else,but onResponse() dont work in general,how I think.Maybe Toast cant work withoud data,but Log.d must work without it,and Log.d havent data,just code of response. I added all depencies and tryind do this like in all tutorial,what wrong I did and what I can do to fix that? And also I try tu put this data to adapter,but when launch app,I have error in Log "RecyclerView: No adapter attached; skipping layout" maybe it's the same problem.onResponse dont work and adapter doesn't create,because adapter inilialze in onResponse method and if onResponse doesn't work,setadapter to recyclerview doesn't work to.And VideoApi class:

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:

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

            }
        });

    }
}

推荐答案

您的json响应返回Video对象的数组. 将Call对象中所有位置的List<Video>更改为Videos 其中Videos class定义为-

Your json response returns array of Video objects. Change List<Video> everywhere in your Call object to Videos where Videosclass is be defined as -

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

这篇关于改造,onResponse方法不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆