自己也是最近几天看了相关的文章,就尝试写了一下,有点小激动,纯粹是为了记录一下
一.依赖如下
二。直接上正题
1.url用的是豆瓣的:
BASE_URL = "https://api.douban.com/v2/movie/"
配置retrofit
public class HttpMethods {
public static final String BASE_URL = "https://api.douban.com/v2/movie/";
private static final int DEFAULT_TIMEOUT = 5;
private Retrofit retrofit; private MovieService movieService;
private HttpMethods() {
//手动创建一个OkHttpClient并设置超时时间
OkHttpClient mOkHttpClient = new OkHttpClient.Builder() .connectTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)//设置连接超时时间 .readTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)//设置读取超时时间 .writeTimeout(HttpConfig.HTTP_TIME, TimeUnit.SECONDS)//设置写入超时时间 // .addInterceptor(InterceptorUtil.HeaderInterceptor())//添加其他拦截器 .addInterceptor(InterceptorUtil.LogInterceptor())//添加日志拦截器 .build();
retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create())
//这里就是retrofit和rxjava扯上关系了
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(BASE_URL)
.client(mOkHttpClient)
.build();
movieService = retrofit.create(MovieService.class);
}
//在访问HttpMethods时创建单例
private static class SingletonHolder {
private static final HttpMethods INSTANCE = new HttpMethods();
}
//获取单例
public static HttpMethods getInstance() {
return SingletonHolder.INSTANCE;
}
public MovieService API() {
return movieService;
}
/**
* 用于获取豆瓣电影Top250的数据
* @param
* @param start 起始位置
* @param count 获取长度
*/
public void getTopMovie(Subscriber subscriber, int start, int count){
movieService.getTopMovie(start, count)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
2.定义api
public interface MovieService { @GET("top250") Flowable getTopMovie(@Query("start") int start, @Query("count") int count);
MovieEntity为具体的实体类,我就不贴出来了
3.高潮来了
/** * 第一种retrofit2 和Rxjava2 结合成功获取数据的方案 */ HttpMethods.getInstance().API() .getTopMovie( 0, 10) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer() { @Override public void accept(MovieEntity movieEntity) throws Exception { MovieEntity ob = movieEntity; List subjects = ob.getSubjects(); tv_title.setText(subjects.get(2).getTitle()); } }); /** * 第二种retrofit2 和Rxjava2 结合成功获取数据的方案 */ HttpMethods.getInstance().API() .getTopMovie( 0, 10) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber() { @Override public void onSubscribe(Subscription s) {/** * 第一种retrofit2 和Rxjava2 结合成功获取数据的方案 */ HttpMethods.getInstance().API() .getTopMovie( 0, 10) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Consumer() { @Override public void accept(MovieEntity movieEntity) throws Exception { MovieEntity ob = movieEntity; List subjects = ob.getSubjects(); tv_title.setText(subjects.get(2).getTitle()); } }); /** * 第二种retrofit2 和Rxjava2 结合成功获取数据的方案 */ HttpMethods.getInstance().API() .getTopMovie( 0, 10) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber() { @Override public void onSubscribe(Subscription s) {BASE_URL = "https://api.douban.com/v2/movie/"
//这里注意了,一定要设置,只有设置了,接下来的方法才会被调用
s.request(Long.MAX_VALUE);
tv_title.setText("I got it ");
}
@Override
public void onNext(MovieEntity movieEntity) {
MovieEntity ob = movieEntity;
List subjects = ob.getSubjects();
tv_title.setText(subjects.get(2).getTitle());
}
@Override
public void onError(Throwable t) {
tv_title.setText(" a error");
}
@Override
public void onComplete() {
tv_title.setText("it is finished");
}
});
}