我为我的应用使用Retrofit-2.0.0
。现在,我在网上找到的关于Retrofit的每个教程都基于早期的Retrofit
,并且在那里没有Call<T>
接口。这是我第一次使用Retrofit,我一再获得null object reference
。这是我的网络模型接口在改造中使用呼叫入队功能
public interface TMovieDBService {
@GET("/movie")
Call<MovieResponse> getMovieResponse(@Query("sort_by") String sortKey,
@Query("api_key") String apiKey);
}
这是我updateMovies()
功能,更新的电影名单。
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();
}
});
}
我不知道在enqueue函数中要实现什么。关于如何使用回调,改装网站上的提示很少。假设所有其他代码工作正常。这里的MovieResponse
对象由包含Movie
和一些额外信息的数组的movieDB API返回。我正在以这种方式实施它,以便一旦我从那里得到MovieResponse
的回复,我可以使用getMovies()
进行提取,这将返回一个电影列表。
当我运行时,我只是得到null object reference
,因为response
为空。我试图搜索关于使用新的Retrofit-2.0.0
的教程,特别是使用入队函数,但我运气不佳。再加上一个问题,updateMovies()
应该在哪里调用。我可以直接在主要活动中打电话吗?后期改造是否会自动在后台线程上运行网络调用?
https://github.com/square/retrofit/releases/tag/parent-2.0.0-beta1 ↑这是测试版,所以我觉得你应该改为使用改进版本1.9.0(最新的稳定版本)。 –
我也面临同样的问题。你有任何解决方案? – Govan
类似的问题和答案:http://stackoverflow.com/questions/33202871/retrofit-error-2-0-2-beta-error-in-callback-does-not-override-abstract-method-on –