Retrofit與Rxjava結合使用例項
阿新 • • 發佈:2018-12-12
環境配置
在Module:app的build.gradle下新增如下依賴,然後sync now。下面這些依賴有些沒有用到,暫時都新增進去不會有錯。
dependencies { implementation 'com.squareup.retrofit2:retrofit:2.4.0' implementation 'com.squareup.retrofit2:adapter-rxjava:2.4.0' implementation 'com.squareup.retrofit2:converter-gson:2.4.0' implementation 'com.squareup.retrofit2:converter-scalars:2.4.0' implementation 'com.squareup.okhttp3:okhttp:3.11.0' implementation 'io.reactivex.rxjava2:rxandroid:2.1.0' implementation 'io.reactivex.rxjava2:rxjava:2.2.2' implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0' implementation 'com.trello.rxlifecycle2:rxlifecycle:2.2.2' implementation 'com.trello.rxlifecycle2:rxlifecycle-components:2.2.2' implementation 'io.reactivex:rxandroid:1.2.1' }
具體框架搭建
以下是我的專案目錄的網路連線部分,使用了mvp模式。
首先根據retrofit的註釋方法建立一個介面,引數是url中要傳入的資料。
public interface RetrofitService {
@GET("bubbleWeb/user?method=regist")
Observable<User> regist(
@Query("nickname")String nickname,
@Query("password")String password
);
}
接下來將寫一個單例模式將retrofit封裝起來。
.baseUrl("http://")這裡填自己的ip地址。
public class WebService { private static final String TAG = "WebService"; private Context mContext; OkHttpClient client = new OkHttpClient(); GsonConverterFactory factory = GsonConverterFactory.create(new GsonBuilder().create()); private static WebService instance = null; private Retrofit mRetrofit = null; public static WebService getInstance(){ if (instance == null){ instance = new WebService(); } return instance; } private WebService(){ this.mContext = Utils.getContext(); init(); } private void init() { resetApp(); } private void resetApp() { mRetrofit = new Retrofit.Builder() .baseUrl("http://***.***.***.***/") .client(client) .addConverterFactory(factory) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .build(); } public RetrofitService getServer(){ return mRetrofit.create(RetrofitService.class); } }
model層:
public class UserManager {
private RetrofitService service;
public UserManager() {
this.service=WebService.getInstance().getServer();
}
public Observable<User> regist(String nickname, String password){
return service.regist(nickname, password);
}
}
View層:
public interface View {
}
public interface UserView extends View {
void onSuccess(User user);
void onError(String result);
}
Present層:
介面
public interface Presenter {
void onCreate();
void onStart();
void onStop();
void pause();
void attachView(View view);
void attachIncomingIntent(Intent intent);
}
public class UserPresenter implements Presenter {
private UserManager manager;
private CompositeSubscription subscription;
private UserView view;
private User mUser;
private Observer<User> userObserver = new Observer<User>() {
@Override
public void onCompleted() {
if(mUser!=null){
view.onSuccess(mUser);
}
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
view.onError("請求失敗!");
}
@Override
public void onNext(User user) {
mUser = user;
}
};
@Override
public void onCreate() {
manager = new UserManager();
subscription = new CompositeSubscription();
}
@Override
public void onStart() {
}
@Override
public void onStop() {
if(subscription.hasSubscriptions()){
//停止訂閱,防止記憶體洩漏
subscription.unsubscribe();
}
}
@Override
public void pause() {
}
@Override
public void attachView(View view) {
this.view =(UserView) view;
}
@Override
public void attachIncomingIntent(Intent intent) {
}
public void regist(String nickname, String password){
subscription.add(manager.regist(nickname, password)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(userObserver)
);
}
}
例項化呼叫
1.實現View介面的onSuccess和onError方法,可以在方法中操作ui
2.Present要在Activity的生命週期完結時一起回收。
private UserPresenter presenter = new UserPresenter();
...
presenter.regist(nickname,password);
...
private UserView userView= new UserView() {
@Override
public void onSuccess(User user) {
finish();
Intent intent = new Intent(getApplicationContext(), FriendsListActivity.class);
startActivity(intent);
}
@Override
public void onError(String result) {
Toast.makeText(getApplicationContext(),result, Toast.LENGTH_SHORT).show();
}
};
...
@Override
protected void onDestroy(){
super.onDestroy();
presenter.onStop();
}