Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "code": "2000",
- "msg": "响应成功",
- "data": {
- "khmc": "上海复星长征医学科学有限公司(第三方)",
- "contact": "杨彪",
- "phone": "18888888888",
- "address": "上海摊"
- }
- }
- {
- "code": "2000",
- "msg": "响应成功",
- "data": "添加成功"
- }
- //ReactiveX
- compile 'io.reactivex:rxjava:1.2.3'
- compile 'io.reactivex:rxandroid:1.2.1'
- //Retrofit
- compile 'com.squareup.retrofit2:retrofit:2.1.0'
- compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
- compile 'com.squareup.retrofit2:converter-gson:2.1.0'
- public class AddFormFragment extends MvpFragment<AddFormPresenter> implements IAddFormView {
- @Override
- public void onNext(JudiciaryHttpResult judiciaryHttpResult) {
- switch (judiciaryHttpResult.code) {
- case Constants.JudiciaryResponseCode.JUDICIARY_SUCCESS_CODE:
- if (judiciaryHttpResult.data != null) {
- Object object = judiciaryHttpResult.data;
- if (object instanceof String) {
- ToastUtil.showShort(object.toString());
- } else {
- //LinkedTreeMap linkedTreeMap = (LinkedTreeMap) object;
- JudiciaryUser user = (JudiciaryUser) object;
- mKhmcEt.setText(user.getKhmc());
- mContactEt.setText(user.getContact());
- mPhoneEt.setText(user.getPhone());
- mAddressEt.setText(user.getAddress());
- }
- }
- break;
- }
- }
- }
- }
- public class AddFormPresenter extends BasePresenter<IAddFormView> {
- private JudiciaryFormModel mFormModel;
- public AddFormPresenter(IAddFormView addFormView) {
- mFormModel = new JudiciaryFormModel();
- attachView(addFormView);
- }
- public void queryUser() {
- addSubscription(mFormModel.queryUser(new BaseModelApiCallback<JudiciaryHttpResult>()));
- }
- public void addForm(String khmc, String contact, String phone, String address, String time,
- String remark) {
- addSubscription(mFormModel.addForm(khmc, contact, phone, address, time, remark,
- new BaseModelApiCallback<JudiciaryHttpResult>()));
- }
- }
- public class BasePresenter<V extends IBaseView> implements Presenter<V> {
- protected V mvpView;
- private CompositeSubscription mCompositeSubscription;
- @Override
- public void attachView(V view) {
- mvpView = view;
- }
- @Override
- public void detachView() {
- unsubscribe();
- }
- /**
- * unsubscribe all, avoid memory leak
- */
- private void unsubscribe() {
- if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
- mCompositeSubscription.clear();
- }
- }
- /**
- * add the subscription to the CompositeSubscription,convenient for unsubscribe
- *
- * @param subscription {@link Subscription}
- */
- protected void addSubscription(Subscription subscription) {
- if (mCompositeSubscription == null) {
- mCompositeSubscription = new CompositeSubscription();
- }
- mCompositeSubscription.add(subscription);
- }
- public class BaseModelApiCallback<T> implements ModelApiCallback<T> {
- @Override
- public void onStart() {
- mvpView.onRxStart();
- }
- @SuppressWarnings("unchecked")
- @Override
- public void onNext(T model) {
- mvpView.onNext(model);
- }
- @Override
- public void onError(Throwable e) {
- mvpView.onError(e);
- }
- @Override
- public void onCompleted() {
- mvpView.onComplete();
- }
- }
- }
- interface Presenter<V> {
- /**
- * add view
- *
- * @param view view
- */
- void attachView(V view);
- /**
- * remove view
- */
- void detachView();
- }
- public class JudiciaryFormModel extends JudiciaryBaseModel {
- public JudiciaryFormModel() {
- super();
- }
- public Subscription queryUser(ModelApiCallback<JudiciaryHttpResult> apiCallback) {
- int khid = SharedPreferencesUtil.getInstance().get(Constants.UserSpKey.USER_SP_KHID, 0);
- String khmc = SharedPreferencesUtil.getInstance().get(Constants.UserSpKey.USER_SP_KHMC, "");
- return toSubscribe(mApiService.queryUser(khid, khmc), new SubscriberCallback<>(apiCallback));
- }
- public Subscription addForm(String khmc, String contact, String phone, String address, String time,
- String remark, ModelApiCallback<JudiciaryHttpResult> apiCallback) {
- int khid = SharedPreferencesUtil.getInstance().get(Constants.UserSpKey.USER_SP_KHID, 0);
- String usercode = SharedPreferencesUtil.getInstance().get(Constants.UserSpKey.USER_SP_USERCODE, "");
- String password = SharedPreferencesUtil.getInstance().get(Constants.UserSpKey.USER_SP_PASSWORD, "");
- return toSubscribe(
- mApiService.addForm(usercode, password, khid, khmc, contact, phone, address, time, remark),
- new SubscriberCallback<>(apiCallback));
- }
- }
- public class JudiciaryBaseModel {
- //the Retrofit interface define that is for obtaining the Observable
- protected ApiService mApiService;
- public JudiciaryBaseModel() {
- mApiService = JudiciaryAutherApi.getInstance().getApiService();
- }
- /**
- * subscribe the observable to the subscriber
- *
- * @param observable the observable
- * @param subscriber the subscriber
- * @param <T> the type of data that the observable emits
- * @return return the subscription
- */
- protected <T> Subscription toSubscribe(Observable<T> observable, Subscriber<T> subscriber) {
- return observable.subscribeOn(Schedulers.io())
- .unsubscribeOn(Schedulers.io())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(subscriber);
- }
- }
- public interface ModelApiCallback<T> {
- /**
- * callback before request start
- */
- void onStart();
- /**
- * request success callback
- *
- * @param model request data model
- */
- void onNext(T model);
- /**
- * request error callback
- *
- * @param e error exception
- */
- void onError(Throwable e);
- /**
- * request callback
- */
- void onCompleted();
- }
- public interface IAddFormView extends IBaseView<JudiciaryHttpResult> {
- }
- public interface IBaseView<T> {
- void onRxStart();
- void onComplete();
- void onError(Throwable e);
- void onNext(T t);
- }
- public class JudiciaryHttpResult<T> {
- public int code;
- public String msg;
- public T data;
- }
- public class JudiciaryUser implements Serializable {
- private String khmc;
- private String contact;
- private String phone;
- private String address;
- public String getKhmc() {
- return khmc;
- }
- public void setKhmc(String khmc) {
- this.khmc = khmc;
- }
- public String getContact() {
- return contact;
- }
- public void setContact(String contact) {
- this.contact = contact;
- }
- public String getPhone() {
- return phone;
- }
- public void setPhone(String phone) {
- this.phone = phone;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- }
- JudiciaryUser user = (JudiciaryUser) object;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement