@GET(“top250”)
Observable getTop250(@Query(“start”) int start, @Query(“count”)int count);
@FormUrlEncoded
@POST(“/x3/weather”)
Call getWeather(@Field(“cityId”) String cityId, @Field(“key”) String key);
}
好了,有了接口我们就可以获取到接口实例了mMovieService
三,创建一个业务Loader ,如XXXLoder,获取Observable并处理相关业务
解释一下为什么会出现Loader ,我看其他相关文章说,每一个Api 都写一个接口,我觉得这样很麻烦,因此就把请求逻辑封装在在一个业务Loader 里面,一个Loader里面可以处理多个Api 接口。代码如下:
/*
*
- Created by zhouwei on 16/11/10.
/
public class MovieLoader extends ObjectLoader {
private MovieService mMovieService;
public MovieLoader(){
mMovieService = RetrofitServiceManager.getInstance().create(MovieService.class);
}
/* - 获取电影列表
- @param start
- @param count
- @return
*/
public Observable<List> getMovie(int start, int count){
return observe(mMovieService.getTop250(start,count))
.map(new Func1<MovieSubject, List>() {
@Override
public List call(MovieSubject movieSubject) {
return movieSubject.subjects;
}
});
}
public Observable getWeatherList(String cityId,String key){
return observe(mMovieService.getWeather(cityId,key))
.map(new Func1<String, String>() {
@Override
public String call(String s) {
//可以处理对应的逻辑后在返回
return s;
}
});
}
public interface MovieService{
//获取豆瓣Top250 榜单
@GET(“top250”)
Observable getTop250(@Query(“start”) int start, @Query(“count”)int count);
@FormUrlEncoded
@POST(“/x3/weather”)
Call getWeather(@Field(“cityId”) String cityId, @Field(“key”) String key);
}
}
创建一个MovieLoader,构造方法中生成了mMovieService,而Service 中可以定义和业务相关的多个ap