在翻新中使用呼叫入队功能 [英] Using Call Enqueue function in Retrofit

查看:84
本文介绍了在翻新中使用呼叫入队功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的应用程序使用 Retrofit-2.0.0 .现在,我在网上找到的每本有关翻新的教程都是基于早期的 Retrofit ,并且那里没有 Call< T> 界面.这是我第一次使用Retrofit,并且反复得到 null object reference .这是我的网络模型界面

I am using Retrofit-2.0.0 for my app. Now every tutorial on Retrofit I found on web is based on earlier Retrofit and there was no Call<T> interface there. This is the first time I am using Retrofit and I am repeatedly getting null object reference. Here is my network model interface

public interface TMovieDBService {
    @GET("/movie")
    Call<MovieResponse> getMovieResponse(@Query("sort_by") String sortKey,
                             @Query("api_key") String apiKey);
}

这是我的 updateMovies()函数,用于更新电影列表.

And this is my updateMovies() function which updates the list of movies.

void updateMovies() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://api.themoviedb.org/3/discover")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    String sortKey = "popularity.desc";
    TMovieDBService service = retrofit.create(TMovieDBService.class);

    Call<MovieResponse> call = service.getMovieResponse(sortKey, ApiKey.API_KEY);

    call.enqueue(new Callback<MovieResponse>() {
        @Override
        public void onResponse(Response<MovieResponse> response) {
            Log.d(LOG_TAG, "Reached this place");
            if (!response.isSuccess()) {
                Log.d(LOG_TAG, "No Success");
            }
            mMovieList = response.body().getMovies();  // <- response is null here
            // Log.d(LOG_TAG, "Couldn't not reach this place");
            mMovieAdapter.notifyDataSetChanged();
            Log.d(LOG_TAG, "Response returned by website is : " + response.code());
        }

        @Override
        public void onFailure(Throwable t) {
            // Toast for the moment
            // Appropriate error handling code should be present here
            Toast.makeText(getActivity(), "Failed !", Toast.LENGTH_LONG).show();
        }
    });
}

我不知道在入队功能中要实现什么.改造网站上很少提供有关如何使用回调的信息.假设所有其他代码都工作正常.这里的 MovieResponse 对象由movieDB API返回,该对象包含 Movie 的数组和一些其他信息.我以这种方式实现它,以便一旦从那里从 MovieResponse 中获得响应,我就可以使用 getMovies()提取内容,这将返回电影列表.

I don't know what to implement in the enqueue function. Very little is given on the retrofit website on how to use the callback. Assume all the other code is working fine. Here MovieResponse object is returned by the movieDB API which contains an array of Movies and some extra information. I am implementing it in such a way so that once I get the response in MovieResponse from there I can extract using the getMovies() which will return a list of movies.

当我运行时,我只是得到 null对象引用,因为 response 为null.我尝试搜索有关使用新的 Retrofit-2.0.0 的教程,尤其是有关使用入队功能的教程,但我很走运.再加上一个问题,应该在哪里调用 updateMovies().我可以直接在mainactivity中调用它吗?改造是否在后台线程上自动运行网络调用?

When I run I simply get null object reference because response is null. I tried to search for tutorials on using new Retrofit-2.0.0 especially on using enqueue function but I am out of luck. Plus one more question where should the updateMovies() be called. Can I call it directly in the mainactivity. Does retrofit automatically run the network call on background thread ?

推荐答案

在翻新2中,即使发生故障,也会调用 onResponse .您已经使用!response.isSuccess()检查了响应是否不成功.但是您只是记录了它-如果它是真实的(即不成功).相反,您可以登录 response.errorBody().string()来查看api服务器是否指定了错误,并且应该调用类似 return 之类的方法来退出 onResponse回调,因为无法将 response.body()强制转换为 MovieResponse ,因此为null异常.

With Retrofit 2, onResponse is called even if there is a failure. You have checked whether response is not successful by using !response.isSuccess(). But you just logged it - if it's true (i.e. not successful). Instead, you could log response.errorBody().string() to see if api server specifies the error, and you should call something like return to exit onResponse callback, as response.body() couldn't be casted to MovieResponse, hence the null exception.

顺便说一句,您的代码是正确的,但是如果您只是从Retrofit开始,那么使用1.9版本会更简单,因为2.0仍然是beta版本(虽然非常稳定,但是缺少教程).

By the way, your code is correct, but if you just start with Retrofit, it would be simpler to use 1.9 version as 2.0 is still a beta version (very stable though, but lack of tutorials).

这篇关于在翻新中使用呼叫入队功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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