Android开发RxJava结合ProgressDialog实现请求数据

释放双眼,带上耳机,听听看~!

先看效果图
这里写图片描述

接下来看代码
1.请求接口

public interface MovieService {
//        @GET("top250")//https://api.douban.com/v2/movie/top250?start=0&count=10
//        Call<MovieInfo> getMovieInfo(@Query("start") int start, @Query("count") int count);
//         @GET("top250")
//         rx.Observable<MovieInfo> getMovieInfo2(@Query("start") int start, @Query("count") int count);
@GET("top250")
Observable<HttpResult<List<MovieInfo.SubjectsBean>>> getMovieInfo3(@Query("start") int start, @Query("count") int count);
}

2.ProgressHandler

public class ProgressHandler extends Handler {
public static final int SHOW_PROGRESS = 0;
public static final int DISMISS_PROGRESS = 1;
private ProgressDialog pd;
private Context mContext;
private ProgressCancelListener mProgressCancelListener;
private boolean cancelable;
public ProgressHandler(Context context, ProgressCancelListener listener,boolean cancelable){
this.mContext = context;
mProgressCancelListener = listener;
this.cancelable = cancelable;
}
public void initProgressDialog(){
if(pd == null){
pd = new ProgressDialog(mContext);
pd.setCancelable(cancelable);
if(cancelable){
pd.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
mProgressCancelListener.onProgressCanceled();
}
});
}
if(!pd.isShowing()){
pd.show();//显示进度条
}
}
}
public void dismissProgressDialog(){
if(pd!=null){
pd.dismiss();//取消进度条
pd = null;
}
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case SHOW_PROGRESS:
initProgressDialog();
break;
case DISMISS_PROGRESS:
dismissProgressDialog();
break;
}
}
}

3.ProgressSubscribe

public class ProgressSubscribe<T> extends Subscriber<T> implements ProgressCancelListener {
private SubscribeOnNextListener mOnNextListener;
private Context mContext;
private ProgressHandler mProgressHandler;
public ProgressSubscribe(SubscribeOnNextListener listener, Context context){
this.mOnNextListener = listener;
mContext = context;
mProgressHandler = new ProgressHandler(mContext,this,true);
}
public void showProgressDialog(){
if(mProgressHandler!=null){
mProgressHandler.obtainMessage(ProgressHandler.SHOW_PROGRESS).sendToTarget();
}
}
public void disMissProgressDialog(){
if(mProgressHandler!=null){
mProgressHandler.obtainMessage(ProgressHandler.DISMISS_PROGRESS).sendToTarget();
mProgressHandler = null;
}
}
@Override
public void onStart() {
super.onStart();
showProgressDialog();
}
@Override
public void onCompleted() {
disMissProgressDialog();
}
@Override
public void onError(Throwable e) {
disMissProgressDialog();
}
@Override
public void onNext(Object o) {
mOnNextListener.onNext(o);//回调MainActivity中的onNext方法
}
@Override
public void onProgressCanceled() {//取消请求
if(!this.isUnsubscribed()){
unsubscribe();
}
}
}

4.RetrofitClient

public class RetrofitClient {
private Retrofit mRetrofit;
private final MovieService mMovieService;
public RetrofitClient(){
mRetrofit = new Retrofit.Builder().baseUrl("https://api.douban.com/v2/movie/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
mMovieService = mRetrofit.create(MovieService.class);
}
public static RetrofitClient getInstance(){
return RetrofitHolder.sClient;
}
public static class RetrofitHolder{
private static RetrofitClient sClient = new RetrofitClient();
}
public<T> T create(Class<T> clazz){
return mRetrofit.create(clazz);
}
//    public void getMovieInfo(Subscriber<MovieInfo> subscriber,int start,int count){
//        mMovieService.getMovieInfo2(start,count)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(subscriber);
//    }
/**
* 用来统一处理Htpp的resultCode,并将data部分的数据剥离出来给Subscriber
* @param <T> Subscribe真正需要的数据类型
*/
class HttpResultFunc<T> implements Func1<HttpResult<T>,T>{
@Override
public T call(HttpResult<T> tHttpResult) {
if(tHttpResult.resultCode!=0){
//                throw new ApiException
}
return tHttpResult.subjects;
}
}
public void getMovieInfo(Subscriber<List<MovieInfo.SubjectsBean>> subscriber, int start, int count){
mMovieService.getMovieInfo3(start,count)
.map(new HttpResultFunc<List<MovieInfo.SubjectsBean>>())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
}

5.SubscribeOnNextListener

//接口,用来回调
public interface SubscribeOnNextListener<T> {
void onNext(T t);
}

6.ProgressCancelListener

//接口,用来取消进度条
public interface ProgressCancelListener {
void onProgressCanceled();
}

7.MainActivity

public class MainActivity extends AppCompatActivity {
@Bind(R.id.textview)
TextView mTextview;
@Bind(R.id.button)
Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.button)
public void click() {
getMovie();
}
public void getMovie() {
RetrofitClient.getInstance().getMovieInfo
(new ProgressSubscribe<List<MovieInfo.SubjectsBean>>(mOnNextListener,this),0,10);
}
SubscribeOnNextListener<List<MovieInfo.SubjectsBean>> mOnNextListener = new SubscribeOnNextListener<List<MovieInfo.SubjectsBean>>() {
@Override
public void onNext(List<MovieInfo.SubjectsBean> o) {
mTextview.setText(o.toString());
}
};
}

人已赞赏
Android文章

android listview设置选中时的item的背景色

2020-3-14 17:38:02

Android文章

android 修改包名

2020-3-14 18:37:18

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索