您的位置:首页 > 编程语言 > Java开发

Retrofit 和 RxJava的简单使用以及简单封装

2016-11-19 12:07 549 查看
这里还有APiService呢

public interface APIService {

/**
* 尽量不要在前面加  /   ,尽量在BASE_URL后面加  /  , 然后组成的就是   base_url + start-image/1080*1776
* */
@GET("start-image/1080*1776")
Call<StratImage> loadStartImage();

@GET("news/{id}")
Call<NewsContent> loadNewsContent(@Path("id") int id);

//下面是rx的写法

@GET("news/{id}")
Observable<NewsContent> loadNewContentRX(@Path("id") int id);

@GET("news/{latest}")
Observable<NewsLatest>  loadNewsLatsetRX(@Path("latest") String latset);
}


然后在写个调用他的管理者,使用 单例模式

public class ServiceManager {

public APIService mAPIService;

private static ServiceManager mServiceManager = null ;

private ServiceManager(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(AppUrl.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
mAPIService = retrofit.create(APIService.class);
}

public static ServiceManager getManager(){
if (mServiceManager == null){
mServiceManager = new ServiceManager();
}
return mServiceManager ;
}
}


这里是简单的使用

private void loadContentRX() {
//因为用的是RxJava所有需要自己手动调用网路请求在哪个线程执行,最后结果在哪个线程执行
ServiceManager.getManager().mAPIService
.loadNewContentRX(3892357)
.subscribeOn(Schedulers.newThread())//网路执行
.observeOn(AndroidSchedulers.mainThread())//结果在主线程执行
.subscribe(new Subscriber<NewsContent>() {
@Override
public void onCompleted() {
Logger.d("success");
}

@Override
public void onError(Throwable e) {
Logger.d(e.toString());
}

@Override
public void onNext(NewsContent newsContent) {
Logger.d(newsContent.toString());
}
});
}


下面代码比上面多了一步操作,就是讲得到的内容进行保存操作,也可以不需要这一步!

private void loadContentRX() {
final NewsContent news = (NewsContent) ACache.get(this).getAsObject("news");
//这里可以将保存的内容进行显示,
if (news !=null){
Logger.d(news.toString());
}else {
ServiceManager.getManager().mAPIService
.loadNewContentRX(3892357)
.subscribeOn(Schedulers.newThread())
.observeOn(Schedulers.io())
.doOnNext(new Action1<NewsContent>() {
@Override//这里面不是主线程,不要进行UI操作,进行保存操作就行
public void call(NewsContent newsContent) {
saveNewsContent(newsContent);
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<NewsContent>() {
@Override
public void onCompleted() {
Logger.d("success");
}

@Override
public void onError(Throwable e) {
Logger.d(e.toString());
}

@Override
public void onNext(NewsContent newsContent) {
Logger.d(newsContent.toString());
}
});
}
}
//讲对象保存进去
private void saveNewsContent(NewsContent newsContent) {
ACache aCache = ACache.get(MainActivity.this);
aCache.put("news" , newsContent);
}


然后就是关于dialog 和点击事件的处理了

其实网上可以使用RxBing的库进行操作防抖事件,但是我没有搞明白,如果有搞明白的大哥 可以指点一下

public class MultipleClick {
/**
* 防止多次点击 在点击事件里面调用
* */
public static void blockView(final View view) {
view.setEnabled(false);
new Handler().postDelayed(new Runnable() {

@Override
public void run() {
view.setEnabled(true);
}
}, 500);
}
}

public class ProgressUtil {

private static ProgressDialog dialog;
/*
* 开始显示dialog
* */
public static void showDialog(Context context){
dialog = new ProgressDialog(context);
dialog.setTitle("标题");
dialog.setMessage("wait...");
dialog.show();
}

/*
* 关闭dialog
* */
public static void dismissDialog(){
if (dialog != null){
dialog.dismiss();
dialog = null;
}
}
}


下面就是在合适的地方调用

mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MultipleClick.blockView(mButton);//调用防止多次点击
loadContentRX();
}
});

//开始和显示dialog的地方
private void loadContentRX() {
ProgressUtil.showDialog(MainActivity.this);//显示dialog

ServiceManager.getManager().mAPIService
.loadNewsLatsetRX("latest")
.throttleFirst(5000, TimeUnit.MINUTES)//添加防抖操作,多次点击
//这个方法
.flatMap(new Func1<NewsLatest, Observable<NewsContent>>() {
@Override
public Observable<NewsContent> call(NewsLatest newsLatest) {
int id = newsLatest.getStories().get(0).getId();
//因为没有api就用知乎日报的,return 里面写的内容就是再次进行了网路请求
return ServiceManager.getManager().mAPIService.loadNewContentRX(id);
}
})
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<NewsContent>() {
@Override
public void onCompleted() {
Logger.d("成功");
ProgressUtil.dismissDialog();//取消
}

@Override
public void onError(Throwable e) {
Logger.d(e.toString());
ProgressUtil.dismissDialog();//取消
}

@Override
public void onNext(NewsContent newsContent) {
Logger.d(newsContent.toString());
ProgressUtil.dismissDialog();//取消
}
});

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rxjava retrofit