Advertisement
Guest User

Untitled

a guest
Mar 8th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. {
  2. "code": "2000",
  3. "msg": "响应成功",
  4. "data": {
  5. "khmc": "上海复星长征医学科学有限公司(第三方)",
  6. "contact": "杨彪",
  7. "phone": "18888888888",
  8. "address": "上海摊"
  9. }
  10. }
  11.  
  12. {
  13. "code": "2000",
  14. "msg": "响应成功",
  15. "data": "添加成功"
  16. }
  17.  
  18. //ReactiveX
  19. compile 'io.reactivex:rxjava:1.2.3'
  20. compile 'io.reactivex:rxandroid:1.2.1'
  21.  
  22. //Retrofit
  23. compile 'com.squareup.retrofit2:retrofit:2.1.0'
  24. compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
  25. compile 'com.squareup.retrofit2:converter-gson:2.1.0'
  26.  
  27. public class AddFormFragment extends MvpFragment<AddFormPresenter> implements IAddFormView {
  28. @Override
  29. public void onNext(JudiciaryHttpResult judiciaryHttpResult) {
  30. switch (judiciaryHttpResult.code) {
  31. case Constants.JudiciaryResponseCode.JUDICIARY_SUCCESS_CODE:
  32. if (judiciaryHttpResult.data != null) {
  33. Object object = judiciaryHttpResult.data;
  34. if (object instanceof String) {
  35. ToastUtil.showShort(object.toString());
  36. } else {
  37. //LinkedTreeMap linkedTreeMap = (LinkedTreeMap) object;
  38. JudiciaryUser user = (JudiciaryUser) object;
  39. mKhmcEt.setText(user.getKhmc());
  40. mContactEt.setText(user.getContact());
  41. mPhoneEt.setText(user.getPhone());
  42. mAddressEt.setText(user.getAddress());
  43. }
  44. }
  45. break;
  46. }
  47. }
  48. }
  49. }
  50.  
  51. public class AddFormPresenter extends BasePresenter<IAddFormView> {
  52.  
  53. private JudiciaryFormModel mFormModel;
  54.  
  55. public AddFormPresenter(IAddFormView addFormView) {
  56. mFormModel = new JudiciaryFormModel();
  57. attachView(addFormView);
  58. }
  59.  
  60. public void queryUser() {
  61. addSubscription(mFormModel.queryUser(new BaseModelApiCallback<JudiciaryHttpResult>()));
  62. }
  63.  
  64. public void addForm(String khmc, String contact, String phone, String address, String time,
  65. String remark) {
  66. addSubscription(mFormModel.addForm(khmc, contact, phone, address, time, remark,
  67. new BaseModelApiCallback<JudiciaryHttpResult>()));
  68. }
  69. }
  70.  
  71. public class BasePresenter<V extends IBaseView> implements Presenter<V> {
  72.  
  73. protected V mvpView;
  74.  
  75. private CompositeSubscription mCompositeSubscription;
  76.  
  77. @Override
  78. public void attachView(V view) {
  79. mvpView = view;
  80. }
  81.  
  82. @Override
  83. public void detachView() {
  84. unsubscribe();
  85. }
  86.  
  87. /**
  88. * unsubscribe all, avoid memory leak
  89. */
  90. private void unsubscribe() {
  91. if (mCompositeSubscription != null && mCompositeSubscription.hasSubscriptions()) {
  92. mCompositeSubscription.clear();
  93. }
  94. }
  95.  
  96. /**
  97. * add the subscription to the CompositeSubscription,convenient for unsubscribe
  98. *
  99. * @param subscription {@link Subscription}
  100. */
  101. protected void addSubscription(Subscription subscription) {
  102. if (mCompositeSubscription == null) {
  103. mCompositeSubscription = new CompositeSubscription();
  104. }
  105. mCompositeSubscription.add(subscription);
  106. }
  107.  
  108. public class BaseModelApiCallback<T> implements ModelApiCallback<T> {
  109.  
  110. @Override
  111. public void onStart() {
  112. mvpView.onRxStart();
  113. }
  114.  
  115. @SuppressWarnings("unchecked")
  116. @Override
  117. public void onNext(T model) {
  118. mvpView.onNext(model);
  119. }
  120.  
  121. @Override
  122. public void onError(Throwable e) {
  123. mvpView.onError(e);
  124. }
  125.  
  126. @Override
  127. public void onCompleted() {
  128. mvpView.onComplete();
  129. }
  130. }
  131. }
  132.  
  133. interface Presenter<V> {
  134. /**
  135. * add view
  136. *
  137. * @param view view
  138. */
  139. void attachView(V view);
  140.  
  141. /**
  142. * remove view
  143. */
  144. void detachView();
  145. }
  146.  
  147. public class JudiciaryFormModel extends JudiciaryBaseModel {
  148.  
  149. public JudiciaryFormModel() {
  150. super();
  151. }
  152.  
  153. public Subscription queryUser(ModelApiCallback<JudiciaryHttpResult> apiCallback) {
  154. int khid = SharedPreferencesUtil.getInstance().get(Constants.UserSpKey.USER_SP_KHID, 0);
  155. String khmc = SharedPreferencesUtil.getInstance().get(Constants.UserSpKey.USER_SP_KHMC, "");
  156. return toSubscribe(mApiService.queryUser(khid, khmc), new SubscriberCallback<>(apiCallback));
  157. }
  158.  
  159. public Subscription addForm(String khmc, String contact, String phone, String address, String time,
  160. String remark, ModelApiCallback<JudiciaryHttpResult> apiCallback) {
  161. int khid = SharedPreferencesUtil.getInstance().get(Constants.UserSpKey.USER_SP_KHID, 0);
  162. String usercode = SharedPreferencesUtil.getInstance().get(Constants.UserSpKey.USER_SP_USERCODE, "");
  163. String password = SharedPreferencesUtil.getInstance().get(Constants.UserSpKey.USER_SP_PASSWORD, "");
  164. return toSubscribe(
  165. mApiService.addForm(usercode, password, khid, khmc, contact, phone, address, time, remark),
  166. new SubscriberCallback<>(apiCallback));
  167. }
  168. }
  169.  
  170. public class JudiciaryBaseModel {
  171.  
  172. //the Retrofit interface define that is for obtaining the Observable
  173. protected ApiService mApiService;
  174.  
  175. public JudiciaryBaseModel() {
  176. mApiService = JudiciaryAutherApi.getInstance().getApiService();
  177. }
  178.  
  179. /**
  180. * subscribe the observable to the subscriber
  181. *
  182. * @param observable the observable
  183. * @param subscriber the subscriber
  184. * @param <T> the type of data that the observable emits
  185. * @return return the subscription
  186. */
  187. protected <T> Subscription toSubscribe(Observable<T> observable, Subscriber<T> subscriber) {
  188. return observable.subscribeOn(Schedulers.io())
  189. .unsubscribeOn(Schedulers.io())
  190. .observeOn(AndroidSchedulers.mainThread())
  191. .subscribe(subscriber);
  192. }
  193. }
  194.  
  195. public interface ModelApiCallback<T> {
  196.  
  197. /**
  198. * callback before request start
  199. */
  200. void onStart();
  201.  
  202. /**
  203. * request success callback
  204. *
  205. * @param model request data model
  206. */
  207. void onNext(T model);
  208.  
  209. /**
  210. * request error callback
  211. *
  212. * @param e error exception
  213. */
  214. void onError(Throwable e);
  215.  
  216. /**
  217. * request callback
  218. */
  219. void onCompleted();
  220. }
  221.  
  222. public interface IAddFormView extends IBaseView<JudiciaryHttpResult> {
  223. }
  224.  
  225. public interface IBaseView<T> {
  226. void onRxStart();
  227.  
  228. void onComplete();
  229.  
  230. void onError(Throwable e);
  231.  
  232. void onNext(T t);
  233. }
  234.  
  235. public class JudiciaryHttpResult<T> {
  236. public int code;
  237. public String msg;
  238. public T data;
  239. }
  240.  
  241. public class JudiciaryUser implements Serializable {
  242.  
  243. private String khmc;
  244.  
  245. private String contact;
  246.  
  247. private String phone;
  248.  
  249. private String address;
  250.  
  251. public String getKhmc() {
  252. return khmc;
  253. }
  254.  
  255. public void setKhmc(String khmc) {
  256. this.khmc = khmc;
  257. }
  258.  
  259. public String getContact() {
  260. return contact;
  261. }
  262.  
  263. public void setContact(String contact) {
  264. this.contact = contact;
  265. }
  266.  
  267. public String getPhone() {
  268. return phone;
  269. }
  270.  
  271. public void setPhone(String phone) {
  272. this.phone = phone;
  273. }
  274.  
  275. public String getAddress() {
  276. return address;
  277. }
  278.  
  279. public void setAddress(String address) {
  280. this.address = address;
  281. }
  282. }
  283.  
  284. JudiciaryUser user = (JudiciaryUser) object;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement