PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/dateLoveInternational/src/main/java/com/datelove/online/International/utils/CommonRequestUtil.java

https://bitbucket.org/juzhiwiscom/datelove
Java | 1208 lines | 886 code | 84 blank | 238 comment | 260 complexity | b7ada9221d580683ea5354f59a597be7 MD5 | raw file
  1. package com.datelove.online.International.utils;
  2. import android.content.Context;
  3. import android.text.TextUtils;
  4. import com.alibaba.fastjson.JSON;
  5. import com.datelove.online.International.R;
  6. import com.datelove.online.International.bean.Area;
  7. import com.datelove.online.International.bean.AreaBean;
  8. import com.datelove.online.International.bean.BaseModel;
  9. import com.datelove.online.International.bean.HeadMsgNotice;
  10. import com.datelove.online.International.bean.Image;
  11. import com.datelove.online.International.bean.MyInfo;
  12. import com.datelove.online.International.bean.NewHeadMsgNotice;
  13. import com.datelove.online.International.bean.RecommendUser;
  14. import com.datelove.online.International.bean.SayHello;
  15. import com.datelove.online.International.bean.UpImage;
  16. import com.datelove.online.International.bean.User;
  17. import com.datelove.online.International.constant.IUrlConstant;
  18. import com.datelove.online.International.xml.PlatformInfoXml;
  19. import com.datelove.online.International.xml.UserInfoXml;
  20. import com.library.utils.ToastUtil;
  21. import com.zhy.http.okhttp.OkHttpUtils;
  22. import com.zhy.http.okhttp.callback.Callback;
  23. import com.zhy.http.okhttp.callback.StringCallback;
  24. import java.io.File;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import okhttp3.Call;
  28. import okhttp3.Response;
  29. /**
  30. * 公用接口请求工具类
  31. * 1、初始化数据字典
  32. * 2、打招呼
  33. * 3、拉黑
  34. * 4、取消拉黑
  35. * 5、删除聊天记录
  36. * 6、关注
  37. * 7、取消关注
  38. * 8、上传/加载用户资料项信息
  39. * // 图片相关
  40. * 9、上传图片
  41. * 10、删除图片
  42. * 11、设置为头像
  43. * 12、获取页眉
  44. * 13、获取新页眉
  45. * 14、获取推荐用户
  46. * Created by zhangdroid on 2016/6/30.
  47. */
  48. public class CommonRequestUtil {
  49. /**
  50. * 缘分打招呼
  51. */
  52. public static final String SAY_HELLO_TYPE_FATE = "1";
  53. /**
  54. * 搜索打招呼
  55. */
  56. public static final String SAY_HELLO_TYPE_SEARCH = "2";
  57. /**
  58. * 用户详情页打招呼
  59. */
  60. public static final String SAY_HELLO_TYPE_USER_INFO = "3";
  61. /**
  62. * 附近的人打招呼
  63. */
  64. public static final String SAY_HELLO_TYPE_NEARBY = "9";
  65. /**
  66. * 最近访客打招呼
  67. */
  68. public static final String SAY_HELLO_TYPE_GUEST = "10";
  69. /**
  70. * 初始化数据字典(带有监听回调)
  71. *
  72. * @param onCommonListener 请求结果回调
  73. */
  74. public static void initParamsDict(final OnCommonListener onCommonListener) {
  75. OkHttpUtils.post()
  76. .url(IUrlConstant.URL_INIT)
  77. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  78. .build()
  79. .execute(new StringCallback() {
  80. @Override
  81. public void onError(Call call, Exception e, int id) {
  82. if (onCommonListener != null) {
  83. onCommonListener.onFail();
  84. }
  85. }
  86. @Override
  87. public void onResponse(String response, int id) {
  88. UserInfoXml.setParamsInit(response);
  89. if (onCommonListener != null) {
  90. onCommonListener.onSuccess();
  91. }
  92. }
  93. });
  94. }
  95. /**
  96. * 打招呼
  97. *
  98. * @param context 上下文对象
  99. * @param userId 用户id
  100. * @param sayHelloType 打招呼类型
  101. * @param showToast 是否显示toast
  102. * @param listener 请求结果回调
  103. */
  104. public static void sayHello(final Context context, String userId, String sayHelloType, final boolean showToast, final OnSayHelloListener listener) {
  105. OkHttpUtils.post()
  106. .url(IUrlConstant.URL_SAY_HELLO_MATCH)
  107. .addHeader("token", PlatformInfoXml.getToken())
  108. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  109. .addParams("uid", userId)
  110. .addParams("sayHelloType", sayHelloType)
  111. .build()
  112. .execute(new Callback<SayHello>() {
  113. @Override
  114. public SayHello parseNetworkResponse(Response response, int id) throws Exception {
  115. String resultJson = response.body().string();
  116. if (!TextUtils.isEmpty(resultJson)) {
  117. return JSON.parseObject(resultJson, SayHello.class);
  118. }
  119. return null;
  120. }
  121. @Override
  122. public void onError(Call call, Exception e, int id) {
  123. if (listener != null) {
  124. listener.onFail();
  125. }
  126. if (showToast) {
  127. ToastUtil.showShortToast(context, context.getString(R.string.sayHello_fail));
  128. }
  129. }
  130. @Override
  131. public void onResponse(SayHello response, int id) {
  132. if (response != null) {
  133. String message = response.getMsg();
  134. if (showToast && !TextUtils.isEmpty(message)) {
  135. // ToastUtil.showShortToast(context, message);
  136. if (listener != null) {
  137. listener.onSuccess(response);
  138. }
  139. }
  140. // String isSucceed = response.getIsSucceed();
  141. // if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  142. // String isMatched = response.getIsMatched();
  143. // if (!TextUtils.isEmpty(isMatched) && "1".equals(isMatched)) {
  144. // if (listener != null) {
  145. // listener.onSuccess(response);
  146. // }
  147. // }
  148. //
  149. // } else {
  150. // if (listener != null) {
  151. // listener.onFail();
  152. // }
  153. // }
  154. }
  155. }
  156. });
  157. }
  158. /**
  159. * 取消关注
  160. *
  161. * @param context 上下文对象
  162. * @param userId 用户id
  163. * @param showToast 是否显示toast
  164. * @param listener 请求结果回调
  165. */
  166. public static void cancelFollow(final Context context, String userId, final boolean showToast, final OnCommonListener listener) {
  167. OkHttpUtils.post()
  168. .url(IUrlConstant.URL_CANCEL_FOLLOW)
  169. .addHeader("token", PlatformInfoXml.getToken())
  170. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  171. .addParams("uid", userId)
  172. .build()
  173. .execute(new Callback<BaseModel>() {
  174. @Override
  175. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  176. String resultJson = response.body().string();
  177. if (!TextUtils.isEmpty(resultJson)) {
  178. return JSON.parseObject(resultJson, BaseModel.class);
  179. }
  180. return null;
  181. }
  182. @Override
  183. public void onError(Call call, Exception e, int id) {
  184. if (listener != null) {
  185. listener.onFail();
  186. }
  187. if (showToast) {
  188. ToastUtil.showShortToast(context, context.getString(R.string.cancel_fail));
  189. }
  190. }
  191. @Override
  192. public void onResponse(BaseModel response, int id) {
  193. if (response != null) {
  194. String message = response.getMsg();
  195. if (showToast && !TextUtils.isEmpty(message)) {
  196. ToastUtil.showShortToast(context, message);
  197. }
  198. String isSucceed = response.getIsSucceed();
  199. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  200. if (listener != null) {
  201. listener.onSuccess();
  202. }
  203. } else {
  204. if (listener != null) {
  205. listener.onFail();
  206. }
  207. }
  208. }
  209. }
  210. });
  211. }
  212. /**
  213. * 拉黑
  214. *
  215. * @param userId 用户id
  216. * @param showToast 是否显示Toast
  217. * @param listener 请求结果回调
  218. */
  219. public static void pull2Black(String userId, final boolean showToast, final OnCommonListener listener) {
  220. OkHttpUtils.post()
  221. .url(IUrlConstant.URL_PULL_TO_BLACK)
  222. .addHeader("token", PlatformInfoXml.getToken())
  223. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  224. .addParams("uid", userId)
  225. .build()
  226. .execute(new Callback<BaseModel>() {
  227. @Override
  228. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  229. String resultJson = response.body().string();
  230. if (!TextUtils.isEmpty(resultJson)) {
  231. return JSON.parseObject(resultJson, BaseModel.class);
  232. }
  233. return null;
  234. }
  235. @Override
  236. public void onError(Call call, Exception e, int id) {
  237. if (listener != null) {
  238. listener.onFail();
  239. }
  240. if (showToast) {
  241. ToastUtil.showShortToast(Utils.getContext(), Utils.getContext().getString(R.string.pull_to_black_fail));
  242. }
  243. }
  244. @Override
  245. public void onResponse(BaseModel response, int id) {
  246. if (response != null) {
  247. String message = response.getMsg();
  248. if (showToast && !TextUtils.isEmpty(message)) {
  249. ToastUtil.showShortToast(Utils.getContext(), message);
  250. }
  251. String isSucceed = response.getIsSucceed();
  252. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  253. if (listener != null) {
  254. listener.onSuccess();
  255. }
  256. } else {
  257. if (listener != null) {
  258. listener.onFail();
  259. }
  260. }
  261. }
  262. }
  263. });
  264. }
  265. /**
  266. * 取消拉黑
  267. *
  268. * @param userId 用户id
  269. * @param showToast 是否显示Toast
  270. * @param listener 请求结果回调
  271. */
  272. public static void cancelPull2Black(String userId, final boolean showToast, final OnCommonListener listener) {
  273. OkHttpUtils.post()
  274. .url(IUrlConstant.URL_CANCEL_BLACK)
  275. .addHeader("token", PlatformInfoXml.getToken())
  276. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  277. .addParams("uid", userId)
  278. .build()
  279. .execute(new Callback<BaseModel>() {
  280. @Override
  281. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  282. String resultJson = response.body().string();
  283. if (!TextUtils.isEmpty(resultJson)) {
  284. return JSON.parseObject(resultJson, BaseModel.class);
  285. }
  286. return null;
  287. }
  288. @Override
  289. public void onError(Call call, Exception e, int id) {
  290. if (listener != null) {
  291. listener.onFail();
  292. }
  293. if (showToast) {
  294. ToastUtil.showShortToast(Utils.getContext(), Utils.getContext().getString(R.string.cancel_fail));
  295. }
  296. }
  297. @Override
  298. public void onResponse(BaseModel response, int id) {
  299. if (response != null) {
  300. String message = response.getMsg();
  301. if (showToast && !TextUtils.isEmpty(message)) {
  302. ToastUtil.showShortToast(Utils.getContext(), message);
  303. }
  304. String isSucceed = response.getIsSucceed();
  305. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  306. if (listener != null) {
  307. listener.onSuccess();
  308. }
  309. } else {
  310. if (listener != null) {
  311. listener.onFail();
  312. }
  313. }
  314. }
  315. }
  316. });
  317. }
  318. /**
  319. * 删除聊天记录
  320. *
  321. * @param userId 用户id
  322. * @param showToast 是否显示Toast
  323. * @param listener 请求结果回调
  324. */
  325. public static void deleteChatHistory(String userId, final boolean showToast, final OnCommonListener listener) {
  326. OkHttpUtils.post()
  327. .url(IUrlConstant.URL_DEL_CHAT_HISOTRY)
  328. .addHeader("token", PlatformInfoXml.getToken())
  329. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  330. .addParams("remoteUserIds", userId)
  331. .build()
  332. .execute(new Callback<BaseModel>() {
  333. @Override
  334. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  335. String resultJson = response.body().string();
  336. if (!TextUtils.isEmpty(resultJson)) {
  337. return JSON.parseObject(resultJson, BaseModel.class);
  338. }
  339. return null;
  340. }
  341. @Override
  342. public void onError(Call call, Exception e, int id) {
  343. if (listener != null) {
  344. listener.onFail();
  345. }
  346. if (showToast) {
  347. ToastUtil.showShortToast(Utils.getContext(), Utils.getContext().getString(R.string.del_chat_fail));
  348. }
  349. }
  350. @Override
  351. public void onResponse(BaseModel response, int id) {
  352. if (response != null) {
  353. String message = response.getMsg();
  354. if (showToast && !TextUtils.isEmpty(message)) {
  355. ToastUtil.showShortToast(Utils.getContext(), message);
  356. }
  357. String isSucceed = response.getIsSucceed();
  358. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  359. if (listener != null) {
  360. listener.onSuccess();
  361. }
  362. } else {
  363. if (listener != null) {
  364. listener.onFail();
  365. }
  366. }
  367. }
  368. }
  369. });
  370. }
  371. /**
  372. * 关注
  373. *
  374. * @param userId 用户id
  375. * @param showToast 是否显示Toast
  376. * @param listener 请求结果回调
  377. */
  378. public static void follow(String userId, final boolean showToast, final OnCommonListener listener) {
  379. OkHttpUtils.post()
  380. .url(IUrlConstant.URL_FOLLOW)
  381. .addHeader("token", PlatformInfoXml.getToken())
  382. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  383. .addParams("uid", userId)
  384. .build()
  385. .execute(new Callback<BaseModel>() {
  386. @Override
  387. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  388. String resultJson = response.body().string();
  389. if (!TextUtils.isEmpty(resultJson)) {
  390. return JSON.parseObject(resultJson, BaseModel.class);
  391. }
  392. return null;
  393. }
  394. @Override
  395. public void onError(Call call, Exception e, int id) {
  396. if (listener != null) {
  397. listener.onFail();
  398. }
  399. if (showToast) {
  400. ToastUtil.showShortToast(Utils.getContext(), Utils.getContext().getString(R.string.follow_fail));
  401. }
  402. }
  403. @Override
  404. public void onResponse(BaseModel response, int id) {
  405. if (response != null) {
  406. String message = response.getMsg();
  407. if (showToast && !TextUtils.isEmpty(message)) {
  408. ToastUtil.showShortToast(Utils.getContext(), message);
  409. }
  410. String isSucceed = response.getIsSucceed();
  411. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  412. if (listener != null) {
  413. listener.onSuccess();
  414. }
  415. } else {
  416. if (listener != null) {
  417. listener.onFail();
  418. }
  419. }
  420. }
  421. }
  422. });
  423. }
  424. /**
  425. * 取消关注
  426. *
  427. * @param userId 用户id
  428. * @param showToast 是否显示Toast
  429. * @param listener 请求结果回调
  430. */
  431. public static void cancelFollow(String userId, final boolean showToast, final OnCommonListener listener) {
  432. OkHttpUtils.post()
  433. .url(IUrlConstant.URL_CANCEL_FOLLOW)
  434. .addHeader("token", PlatformInfoXml.getToken())
  435. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  436. .addParams("uid", userId)
  437. .build()
  438. .execute(new Callback<BaseModel>() {
  439. @Override
  440. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  441. String resultJson = response.body().string();
  442. if (!TextUtils.isEmpty(resultJson)) {
  443. return JSON.parseObject(resultJson, BaseModel.class);
  444. }
  445. return null;
  446. }
  447. @Override
  448. public void onError(Call call, Exception e, int id) {
  449. if (listener != null) {
  450. listener.onFail();
  451. }
  452. if (showToast) {
  453. ToastUtil.showShortToast(Utils.getContext(), Utils.getContext().getString(R.string.cancel_fail));
  454. }
  455. }
  456. @Override
  457. public void onResponse(BaseModel response, int id) {
  458. if (response != null) {
  459. String message = response.getMsg();
  460. if (showToast && !TextUtils.isEmpty(message)) {
  461. ToastUtil.showShortToast(Utils.getContext(), message);
  462. }
  463. String isSucceed = response.getIsSucceed();
  464. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  465. if (listener != null) {
  466. listener.onSuccess();
  467. }
  468. } else {
  469. if (listener != null) {
  470. listener.onFail();
  471. }
  472. }
  473. }
  474. }
  475. });
  476. }
  477. /**
  478. * 不喜欢
  479. *
  480. * @param userId 用户id
  481. * @param listener 请求结果回调
  482. */
  483. public static void unLike(String userId,final OnCommonListener listener) {
  484. OkHttpUtils.post()
  485. .url(IUrlConstant.URL_UN_LIKE)
  486. .addHeader("token", PlatformInfoXml.getToken())
  487. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  488. .addParams("uid", userId)
  489. .build()
  490. .execute(new Callback<BaseModel>() {
  491. @Override
  492. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  493. String resultJson = response.body().string();
  494. if (!TextUtils.isEmpty(resultJson)) {
  495. return JSON.parseObject(resultJson, BaseModel.class);
  496. }
  497. return null;
  498. }
  499. @Override
  500. public void onError(Call call, Exception e, int id) {
  501. if (listener != null) {
  502. listener.onFail();
  503. }
  504. }
  505. @Override
  506. public void onResponse(BaseModel response, int id) {
  507. if (response != null) {
  508. String isSucceed = response.getIsSucceed();
  509. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  510. if (listener != null) {
  511. listener.onSuccess();
  512. }
  513. } else {
  514. if (listener != null) {
  515. listener.onFail();
  516. }
  517. }
  518. }
  519. }
  520. });
  521. }
  522. /**
  523. * 上传用户个人资料项
  524. *
  525. * @param showToast 是否显示Toast
  526. * @param listener 请求结果回调
  527. */
  528. public static void uploadUserInfo(final boolean showToast, final OnCommonListener listener) {
  529. Map<String, String> mapParams = new HashMap<String, String>();
  530. mapParams.put("platformInfo", PlatformInfoXml.getPlatformJsonString());
  531. String nickname = UserInfoXml.getNickName();
  532. if (!TextUtils.isEmpty(nickname)) {
  533. mapParams.put("nickName", nickname);
  534. }
  535. String monologue = UserInfoXml.getMonologue();
  536. if (!TextUtils.isEmpty(monologue)) {
  537. mapParams.put("monologue", monologue);
  538. }
  539. String birthday = UserInfoXml.getBirthday();
  540. if (!TextUtils.isEmpty(birthday)) {
  541. mapParams.put("birthday", birthday);
  542. }
  543. String provinceName = UserInfoXml.getProvinceName();
  544. if (!TextUtils.isEmpty(provinceName)) {
  545. Area area = new Area();
  546. area.setProvinceName(provinceName);
  547. AreaBean areaBean = new AreaBean();
  548. areaBean.setArea(area);
  549. mapParams.put("area", JSON.toJSONString(areaBean));
  550. }
  551. String income = UserInfoXml.getIncome();
  552. if (!TextUtils.isEmpty(income)) {
  553. mapParams.put("income", income);
  554. }
  555. String height = UserInfoXml.getHeight();
  556. if (!TextUtils.isEmpty(height)) {
  557. mapParams.put("height", height);
  558. }
  559. String occupation = UserInfoXml.getWork();
  560. if (!TextUtils.isEmpty(occupation)) {
  561. mapParams.put("occupation", occupation);
  562. }
  563. String education = UserInfoXml.getEducation();
  564. if (!TextUtils.isEmpty(education)) {
  565. mapParams.put("education", education);
  566. }
  567. String marriage = UserInfoXml.getMarriage();
  568. if (!TextUtils.isEmpty(marriage)) {
  569. mapParams.put("maritalStatus", marriage);
  570. }
  571. String wantBaby = UserInfoXml.getWantBaby();
  572. if (!TextUtils.isEmpty(wantBaby)) {
  573. mapParams.put("childStatus", wantBaby);
  574. }
  575. String sport = UserInfoXml.getSport();
  576. if (!TextUtils.isEmpty(sport)) {
  577. mapParams.put("sports", sport);
  578. }
  579. String pets = UserInfoXml.getPets();
  580. if (!TextUtils.isEmpty(pets)) {
  581. mapParams.put("pets", pets);
  582. }
  583. String ethnicity = UserInfoXml.getEthnicity();
  584. if (!TextUtils.isEmpty(ethnicity)) {
  585. mapParams.put("ethnicity", ethnicity);
  586. }
  587. String exerciseHabits = UserInfoXml.getExerciseHabits();
  588. if (!TextUtils.isEmpty(exerciseHabits)) {
  589. mapParams.put("exerciseHabits", exerciseHabits);
  590. }
  591. String interest = UserInfoXml.getInterest();
  592. if (!TextUtils.isEmpty(interest)) {
  593. mapParams.put("listHobby", interest);
  594. }
  595. OkHttpUtils.post()
  596. .url(IUrlConstant.URL_UPLOAD_USER_INFO)
  597. .params(mapParams)
  598. .build()
  599. .execute(new Callback<MyInfo>() {
  600. @Override
  601. public MyInfo parseNetworkResponse(Response response, int id) throws Exception {
  602. String resultJson = response.body().string();
  603. if (!TextUtils.isEmpty(resultJson)) {
  604. return JSON.parseObject(resultJson, MyInfo.class);
  605. }
  606. return null;
  607. }
  608. @Override
  609. public void onError(Call call, Exception e, int id) {
  610. if (listener != null) {
  611. listener.onFail();
  612. }
  613. }
  614. @Override
  615. public void onResponse(MyInfo response, int id) {
  616. if (response != null) {
  617. String isSucceed = response.getIsSucceed();
  618. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  619. // 上传成功后更新本地信息
  620. UserInfoXml.setUserInfo(response.getUserEnglish());
  621. if (showToast) {
  622. ToastUtil.showShortToast(Utils.getContext(), response.getMsg());
  623. }
  624. if (listener != null) {
  625. listener.onSuccess();
  626. }
  627. } else {
  628. if (listener != null) {
  629. listener.onFail();
  630. }
  631. }
  632. } else {
  633. if (listener != null) {
  634. listener.onFail();
  635. }
  636. }
  637. }
  638. });
  639. }
  640. /**
  641. * 加载用户个人信息
  642. *
  643. * @param listener 请求结果回调
  644. */
  645. public static void loadUserInfo(final OnLoadUserInfoListener listener) {
  646. OkHttpUtils.post()
  647. .url(IUrlConstant.URL_USER_INFO)
  648. .addHeader("token", PlatformInfoXml.getToken())
  649. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  650. .build()
  651. .execute(new Callback<MyInfo>() {
  652. @Override
  653. public MyInfo parseNetworkResponse(Response response, int id) throws Exception {
  654. String resultJson = response.body().string();
  655. if (!TextUtils.isEmpty(resultJson)) {
  656. return JSON.parseObject(resultJson, MyInfo.class);
  657. }
  658. return null;
  659. }
  660. @Override
  661. public void onError(Call call, Exception e, int id) {
  662. if (listener != null) {
  663. listener.onFail();
  664. }
  665. }
  666. @Override
  667. public void onResponse(MyInfo response, int id) {
  668. if (response != null) {
  669. String isSucceed = response.getIsSucceed();
  670. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  671. User user = response.getUserEnglish();
  672. if (user != null) {
  673. // if(user.getListImage().size()>1){
  674. // user.getListImage().remove(0);
  675. // }
  676. UserInfoXml.setUserInfo(user);
  677. }
  678. if (listener != null) {
  679. listener.onSuccess(user);
  680. }
  681. } else {
  682. if (listener != null) {
  683. listener.onFail();
  684. }
  685. }
  686. } else {
  687. if (listener != null) {
  688. listener.onFail();
  689. }
  690. }
  691. }
  692. });
  693. }
  694. /**
  695. * 上传图片
  696. *
  697. * @param isMain 是否上传头像(“1”表示上传头像,“2”表示普通图片)
  698. * @param file 图片File
  699. * @param showToast 是否显示Toast
  700. * @param listener 请求结果回调
  701. */
  702. public static void uploadImage(boolean isMain, File file, final boolean showToast, final OnUploadImageListener listener) {
  703. OkHttpUtils.post()
  704. .url(IUrlConstant.URL_UPLOAD_PHOTO)
  705. .addHeader("isMain", isMain ? "1" : "2")
  706. .addHeader("token", PlatformInfoXml.getToken())
  707. .addHeader("productId", PlatformInfoXml.getPlatformInfo().getProduct())
  708. .addHeader("fid", PlatformInfoXml.getPlatformInfo().getFid())
  709. .addHeader("pid", PlatformInfoXml.getPlatformInfo().getPid())
  710. .addHeader("country", PlatformInfoXml.getPlatformInfo().getCountry())
  711. .addHeader("language", PlatformInfoXml.getPlatformInfo().getLanguage())
  712. .addHeader("version", PlatformInfoXml.getPlatformInfo().getVersion())
  713. .addHeader("platform", PlatformInfoXml.getPlatformInfo().getPlatform())
  714. .addFile("file", "image", file)
  715. .build()
  716. .connTimeOut(60 * 1000)
  717. .readTimeOut(60 * 1000)
  718. .writeTimeOut(60 * 1000)
  719. .execute(new Callback<UpImage>() {
  720. @Override
  721. public UpImage parseNetworkResponse(Response response, int id) throws Exception {
  722. String resultJson = response.body().string();
  723. if (!TextUtils.isEmpty(resultJson)) {
  724. return JSON.parseObject(resultJson, UpImage.class);
  725. }
  726. return null;
  727. }
  728. @Override
  729. public void onError(Call call, Exception e, int id) {
  730. if (listener != null) {
  731. listener.onFail();
  732. }
  733. if (showToast) {
  734. ToastUtil.showShortToast(Utils.getContext(), Utils.getContext().getString(R.string.upload_img_fail));
  735. }
  736. }
  737. @Override
  738. public void onResponse(UpImage response, int id) {
  739. if (response != null) {
  740. String isSucceed = response.getIsSucceed();
  741. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  742. String message = response.getMsg();
  743. if (!TextUtils.isEmpty(message)) {
  744. ToastUtil.showShortToast(Utils.getContext(), message);
  745. }
  746. if (listener != null) {
  747. listener.onSuccess(response.getImage());
  748. }
  749. } else {
  750. if (listener != null) {
  751. listener.onFail();
  752. }
  753. }
  754. } else {
  755. if (listener != null) {
  756. listener.onFail();
  757. }
  758. }
  759. }
  760. });
  761. }
  762. /**
  763. * 删除图片
  764. *
  765. * @param imageId 图片id
  766. * @param showToast 是否显示Toast
  767. * @param listener 请求结果回调
  768. */
  769. public static void deleteImage(String imageId, final boolean showToast, final OnCommonListener listener) {
  770. OkHttpUtils.post()
  771. .url(IUrlConstant.URL_DEL_PHOTO)
  772. .addParams("imgId", imageId)
  773. .addHeader("token", PlatformInfoXml.getToken())
  774. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  775. .build()
  776. .execute(new Callback<BaseModel>() {
  777. @Override
  778. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  779. String resultJson = response.body().string();
  780. if (!TextUtils.isEmpty(resultJson)) {
  781. return JSON.parseObject(resultJson, BaseModel.class);
  782. }
  783. return null;
  784. }
  785. @Override
  786. public void onError(Call call, Exception e, int id) {
  787. if (listener != null) {
  788. listener.onFail();
  789. }
  790. if (showToast) {
  791. ToastUtil.showShortToast(Utils.getContext(), Utils.getContext().getString(R.string.del_image_fail));
  792. }
  793. }
  794. @Override
  795. public void onResponse(BaseModel response, int id) {
  796. if (response != null) {
  797. String isSucceed = response.getIsSucceed();
  798. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  799. String message = response.getMsg();
  800. if (!TextUtils.isEmpty(message)) {
  801. ToastUtil.showShortToast(Utils.getContext(), Utils.getContext().getString(R.string.del_image_success));
  802. }
  803. if (listener != null) {
  804. listener.onSuccess();
  805. }
  806. } else {
  807. if (listener != null) {
  808. listener.onFail();
  809. }
  810. }
  811. } else {
  812. if (listener != null) {
  813. listener.onFail();
  814. }
  815. }
  816. }
  817. });
  818. }
  819. /**
  820. * 设置图片作为头像
  821. *
  822. * @param imageId 图片id
  823. * @param showToast 是否显示Toast
  824. * @param listener 请求结果回调
  825. */
  826. public static void setAvatar(String imageId, final boolean showToast, final OnCommonListener listener) {
  827. OkHttpUtils.post()
  828. .url(IUrlConstant.URL_SET_HEAD_ICON)
  829. .addParams("imgId", imageId)
  830. .addHeader("token", PlatformInfoXml.getToken())
  831. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  832. .build()
  833. .execute(new Callback<BaseModel>() {
  834. @Override
  835. public BaseModel parseNetworkResponse(Response response, int id) throws Exception {
  836. String resultJson = response.body().string();
  837. if (!TextUtils.isEmpty(resultJson)) {
  838. return JSON.parseObject(resultJson, BaseModel.class);
  839. }
  840. return null;
  841. }
  842. @Override
  843. public void onError(Call call, Exception e, int id) {
  844. if (listener != null) {
  845. listener.onFail();
  846. }
  847. if (showToast) {
  848. ToastUtil.showShortToast(Utils.getContext(), Utils.getContext().getString(R.string.set_to_avatar_fail));
  849. }
  850. }
  851. @Override
  852. public void onResponse(BaseModel response, int id) {
  853. if (response != null) {
  854. String isSucceed = response.getIsSucceed();
  855. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  856. String message = response.getMsg();
  857. if (!TextUtils.isEmpty(message)) {
  858. ToastUtil.showShortToast(Utils.getContext(), message);
  859. }
  860. if (listener != null) {
  861. listener.onSuccess();
  862. }
  863. } else {
  864. if (listener != null) {
  865. listener.onFail();
  866. }
  867. }
  868. } else {
  869. if (listener != null) {
  870. listener.onFail();
  871. }
  872. }
  873. }
  874. });
  875. }
  876. /**
  877. * 获取页眉
  878. *
  879. * @param listener 请求结果回调
  880. */
  881. public static void getHeadMsg(final OnGetHeadMsgListener listener) {
  882. OkHttpUtils.post()
  883. .url(IUrlConstant.URL_HEAD_NOTIFICATION)
  884. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  885. .addParams("token", PlatformInfoXml.getToken())
  886. .build()
  887. .execute(new Callback<HeadMsgNotice>() {
  888. @Override
  889. public HeadMsgNotice parseNetworkResponse(Response response, int id) throws Exception {
  890. String resultJson = response.body().string();
  891. if (!TextUtils.isEmpty(resultJson)) {
  892. return JSON.parseObject(resultJson, HeadMsgNotice.class);
  893. }
  894. return null;
  895. }
  896. @Override
  897. public void onError(Call call, Exception e, int id) {
  898. if (listener != null) {
  899. listener.onFail();
  900. }
  901. }
  902. @Override
  903. public void onResponse(HeadMsgNotice response, int id) {
  904. if (response != null) {
  905. String isSucceed = response.getIsSucceed();
  906. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  907. if (listener != null) {
  908. listener.onSuccess(response);
  909. }
  910. } else {
  911. if (listener != null) {
  912. listener.onFail();
  913. }
  914. }
  915. } else {
  916. if (listener != null) {
  917. listener.onFail();
  918. }
  919. }
  920. }
  921. });
  922. }
  923. /**
  924. * 获取新页眉
  925. *
  926. * @param lastMsgTime 最后一条未读消息时间毫秒数
  927. * @param listener 请求结果回调
  928. */
  929. public static void getNewHeadMsg(long lastMsgTime, final OnGetNewHeadMsgListener listener) {
  930. OkHttpUtils.post()
  931. .url(IUrlConstant.URL_HEAD_NOTIFICATION2)
  932. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  933. .addParams("token", PlatformInfoXml.getToken())
  934. .addParams("lastMsgTime", String.valueOf(lastMsgTime))
  935. .build()
  936. .execute(new Callback<NewHeadMsgNotice>() {
  937. @Override
  938. public NewHeadMsgNotice parseNetworkResponse(Response response, int id) throws Exception {
  939. String resultJson = response.body().string();
  940. if (!TextUtils.isEmpty(resultJson)) {
  941. return JSON.parseObject(resultJson, NewHeadMsgNotice.class);
  942. }
  943. return null;
  944. }
  945. @Override
  946. public void onError(Call call, Exception e, int id) {
  947. if (listener != null) {
  948. listener.onFail();
  949. }
  950. }
  951. @Override
  952. public void onResponse(NewHeadMsgNotice response, int id) {
  953. if (response != null) {
  954. String isSucceed = response.getIsSucceed();
  955. String noticeType = response.getNoticeType();
  956. long lastMsgTime1 = response.getLastMsgTime();
  957. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  958. if (listener != null) {
  959. listener.onSuccess(response);
  960. }
  961. } else {
  962. if (listener != null) {
  963. listener.onFail();
  964. }
  965. }
  966. } else {
  967. if (listener != null) {
  968. listener.onFail();
  969. }
  970. }
  971. }
  972. });
  973. }
  974. /**
  975. * 获取推荐用户
  976. *
  977. * @param cycle 轮询周期(秒)
  978. * @param listener 请求结果回调
  979. */
  980. public static void getRecommendUser(int cycle, final OnGetRecommendUserListener listener) {
  981. OkHttpUtils.post()
  982. .url(IUrlConstant.URL_GET_RECOMMEND_USER)
  983. .addParams("platformInfo", PlatformInfoXml.getPlatformJsonString())
  984. .addParams("token", PlatformInfoXml.getToken())
  985. .addParams("type", "0")
  986. .addParams("cycle", cycle < 20 ? "20" : String.valueOf(cycle))
  987. .build()
  988. .execute(new Callback<RecommendUser>() {
  989. @Override
  990. public RecommendUser parseNetworkResponse(Response response, int id) throws Exception {
  991. String resultJson = response.body().string();
  992. if (!TextUtils.isEmpty(resultJson)) {
  993. return JSON.parseObject(resultJson, RecommendUser.class);
  994. }
  995. return null;
  996. }
  997. @Override
  998. public void onError(Call call, Exception e, int id) {
  999. if (listener != null) {
  1000. listener.onFail();
  1001. }
  1002. }
  1003. @Override
  1004. public void onResponse(RecommendUser response, int id) {
  1005. if (response != null) {
  1006. String isSucceed = response.getIsSucceed();
  1007. if (!TextUtils.isEmpty(isSucceed) && "1".equals(isSucceed)) {
  1008. if (listener != null) {
  1009. listener.onSuccess(response);
  1010. }
  1011. } else {
  1012. if (listener != null) {
  1013. listener.onFail();
  1014. }
  1015. }
  1016. } else {
  1017. if (listener != null) {
  1018. listener.onFail();
  1019. }
  1020. }
  1021. }
  1022. });
  1023. }
  1024. /**
  1025. * 通用监听器
  1026. */
  1027. public interface OnCommonListener {
  1028. /**
  1029. * 请求成功
  1030. */
  1031. void onSuccess();
  1032. /**
  1033. * 请求失败
  1034. */
  1035. void onFail();
  1036. }
  1037. /**
  1038. * 上传用户个人资料监听器
  1039. */
  1040. public interface OnLoadUserInfoListener {
  1041. /**
  1042. * 请求成功
  1043. *
  1044. * @param user 用户信息对象
  1045. */
  1046. void onSuccess(User user);
  1047. /**
  1048. * 请求失败
  1049. */
  1050. void onFail();
  1051. }
  1052. /**
  1053. * 上传图片监听器
  1054. */
  1055. public interface OnUploadImageListener {
  1056. /**
  1057. * 请求成功
  1058. *
  1059. * @param image 上传后图片对象
  1060. */
  1061. void onSuccess(Image image);
  1062. /**
  1063. * 请求失败
  1064. */
  1065. void onFail();
  1066. }
  1067. /**
  1068. * 获取页眉监听器
  1069. */
  1070. public interface OnGetHeadMsgListener {
  1071. /**
  1072. * 请求成功
  1073. *
  1074. * @param headMsgNotice 页眉对象
  1075. */
  1076. void onSuccess(HeadMsgNotice headMsgNotice);
  1077. /**
  1078. * 请求失败
  1079. */
  1080. void onFail();
  1081. }
  1082. /**
  1083. * 获取新页眉监听器
  1084. */
  1085. public interface OnGetNewHeadMsgListener {
  1086. /**
  1087. * 请求成功
  1088. *
  1089. * @param newHeadMsgNotice 新页眉对象
  1090. */
  1091. void onSuccess(NewHeadMsgNotice newHeadMsgNotice);
  1092. /**
  1093. * 请求失败
  1094. */
  1095. void onFail();
  1096. }
  1097. /**
  1098. * 获取推荐用户监听器
  1099. */
  1100. public interface OnGetRecommendUserListener {
  1101. /**
  1102. * 请求成功
  1103. *
  1104. * @param recommendUser 推荐用户对象
  1105. */
  1106. void onSuccess(RecommendUser recommendUser);
  1107. /**
  1108. * 请求失败
  1109. */
  1110. void onFail();
  1111. }
  1112. public interface OnSayHelloListener {
  1113. /**
  1114. * 打招呼成功
  1115. *
  1116. * @param matchUser 打招呼配对成功的用户
  1117. */
  1118. void onSuccess(SayHello matchUser);
  1119. /**
  1120. * 打招呼失败
  1121. */
  1122. void onFail();
  1123. }
  1124. }