PageRenderTime 105ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/yueai/src/main/java/com/yueai/utils/CommonRequestUtil.java

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