NetImpl.java 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. package com.sheishuo.app.common.util.net;
  2. import android.support.annotation.Nullable;
  3. import android.util.Log;
  4. import com.google.gson.Gson;
  5. import com.google.gson.reflect.TypeToken;
  6. import com.sheishuo.app.SheishuoApplication;
  7. import com.sheishuo.app.cache.AccountCache;
  8. import com.sheishuo.app.cache.HobbyCache;
  9. import com.sheishuo.app.cache.SocialInfoCache;
  10. import com.sheishuo.app.common.beans.CircleBean;
  11. import com.sheishuo.app.common.beans.CircleUserLikedBean;
  12. import com.sheishuo.app.common.beans.ContactRelationshipBean;
  13. import com.sheishuo.app.common.beans.GiftBean;
  14. import com.sheishuo.app.common.beans.SocialInfoBean;
  15. import com.sheishuo.app.common.beans.SocialPhoto;
  16. import com.sheishuo.app.common.beans.TradeBean;
  17. import com.sheishuo.app.common.util.img.ImgUtil;
  18. import com.sheishuo.app.login.beans.LoginBean;
  19. import org.json.JSONArray;
  20. import org.json.JSONException;
  21. import org.json.JSONObject;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.util.ArrayList;
  25. import java.util.Iterator;
  26. import java.util.List;
  27. import okhttp3.Call;
  28. import okhttp3.Callback;
  29. import okhttp3.FormBody;
  30. import okhttp3.MultipartBody;
  31. import okhttp3.OkHttpClient;
  32. import okhttp3.Request;
  33. import okhttp3.RequestBody;
  34. import okhttp3.Response;
  35. import static com.sheishuo.app.common.util.net.NetInfo.*;
  36. /**
  37. * Created by KN on 2017/7/14.
  38. */
  39. public class NetImpl implements INet {
  40. private static OkHttpClient client = new OkHttpClient.Builder().build();
  41. private Gson gson = new Gson();
  42. @Override
  43. public void baseQuery() {
  44. }
  45. @Override
  46. public void get(String url, ResponseCallback callback) {
  47. Request request = new Request.Builder()
  48. .get()
  49. .url(url)
  50. .build();
  51. try {
  52. Response response = client.newCall(request).execute();
  53. if ( 200 == response.code()){
  54. callback.onSuccess(response.body().string());
  55. }else {
  56. callback.onFailed();
  57. }
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. callback.onFailed();
  61. }
  62. }
  63. @Override
  64. public void post(String url, FormBody body, ResponseCallback callback) {
  65. Request request = new Request.Builder()
  66. .url(url)
  67. .post(body)
  68. .build();
  69. try {
  70. Response response = client.newCall(request).execute();
  71. if (200 == response.code()) {
  72. callback.onSuccess(response.body().string());
  73. } else {
  74. callback.onFailed();
  75. }
  76. } catch (IOException e) {
  77. e.printStackTrace();
  78. callback.onFailed();
  79. }
  80. }
  81. @Override
  82. public void post(String url, RequestBody body, ResponseCallback callback) {
  83. Request request = new Request.Builder()
  84. .url(url)
  85. .post(body)
  86. .build();
  87. try {
  88. Response response = client.newCall(request).execute();
  89. if (200 == response.code()) {
  90. callback.onSuccess(response.body().string());
  91. } else {
  92. callback.onFailed();
  93. }
  94. } catch (IOException e) {
  95. e.printStackTrace();
  96. callback.onFailed();
  97. }
  98. }
  99. @Override
  100. public void updateLatitudeAndLongitude(final double longitude, final double latitude, final ResponseCallback callback) {
  101. new Thread(new Runnable() {
  102. @Override
  103. public void run() {
  104. try {
  105. Log.e("request", ""+longitude+", "+latitude);
  106. FormBody body = new FormBody.Builder()
  107. .add("id",AccountCache.getAccount().getId())
  108. .add("latitude", ""+latitude)
  109. .add("longitude", ""+longitude)
  110. .build();
  111. Request request = new Request.Builder()
  112. .post(body)
  113. .url(NetInfo.UPDATE_LATITUDE_AND_LONGITUDE)
  114. .build();
  115. Response response = client.newCall(request).execute();
  116. int responseCode = response.code();
  117. if (200 == responseCode){
  118. Log.e("code",""+responseCode);
  119. LoginBean.DBean account = AccountCache.getAccount();
  120. String resultStr = response.body().string();
  121. JSONObject data = new JSONObject(resultStr).getJSONObject("d");
  122. Log.e("json" ,data.toString());
  123. if (data.getBoolean("update")){
  124. account.setProvince(data.getString("province"));
  125. account.setCity(data.getString("city"));
  126. account.setDistrict(data.getString("district"));
  127. account.setProvince_room_id(data.getString("province_room_id"));
  128. account.setCity_room_id(data.getString("city_room_id"));
  129. account.setDistrict_room_id(data.getString("district_room_id"));
  130. callback.onSuccess(null);
  131. }
  132. }else {
  133. callback.onFailed();
  134. }
  135. }catch (Exception e){
  136. e.printStackTrace();
  137. callback.onFailed();
  138. }
  139. }
  140. }).start();
  141. }
  142. @Override
  143. public void updateLocation(final String province, final String city, final String district, final ResponseCallback callback) {
  144. new Thread(new Runnable() {
  145. @Override
  146. public void run() {
  147. try {
  148. FormBody body = new FormBody.Builder()
  149. .add("id",AccountCache.getAccount().getId())
  150. .add("province",province)
  151. .add("city",city)
  152. .add("district",district)
  153. .build();
  154. Request request = new Request.Builder()
  155. .post(body)
  156. .url(NetInfo.RESET_LOCATION_BY_DISTRICT)
  157. .build();
  158. Response response = client.newCall(request).execute();
  159. if (200 == response.code()){
  160. String resultStr = response.body().string();
  161. callback.onSuccess(resultStr);
  162. }else{
  163. callback.onFailed();
  164. }
  165. } catch (Exception e) {
  166. e.printStackTrace();
  167. callback.onFailed();
  168. }
  169. }
  170. }).start();
  171. }
  172. @Override
  173. public void loginQuery(String mobile, String pwd, final ResponseCallback callback) {
  174. final FormBody body = new FormBody.Builder()
  175. .add("mobile", mobile)
  176. .add("pwd", pwd)
  177. .build();
  178. new Thread(new Runnable() {
  179. @Override
  180. public void run() {
  181. //创建FormBody
  182. Request request = new Request.Builder()
  183. .post(body)
  184. .url(NetInfo.INDEX + NetInfo.LOGIN_URL)
  185. .build();
  186. //发起请求并完成回调
  187. try {
  188. Response response = client.newCall(request).execute();
  189. if (200 == response.code()) {
  190. LoginBean bean = gson.fromJson(response.body().string(), LoginBean.class);
  191. bean.getD().setAutoJoinRoom(true);
  192. switch (bean.getC()) {
  193. case 0:
  194. callback.onSuccess(bean);
  195. break;
  196. default:
  197. callback.onFailed();
  198. break;
  199. }
  200. } else {
  201. callback.onFailed();
  202. }
  203. } catch (Exception e) {
  204. e.printStackTrace();
  205. callback.onFailed();
  206. }
  207. }
  208. }
  209. ).start();
  210. }
  211. @Override
  212. public void getVerificationCode(final String mobile, final ResponseCallback callback) {
  213. new Thread(new Runnable() {
  214. @Override
  215. public void run() {
  216. try{
  217. FormBody body = new FormBody.Builder()
  218. .add("mobile", mobile)
  219. .build();
  220. Request request = new Request.Builder()
  221. .post(body)
  222. .url(GET_CODE)
  223. .build();
  224. Response response = client.newCall(request).execute();
  225. if (200 == response.code()){
  226. String resultStr = response.body().string();
  227. String code = new JSONObject(resultStr).getJSONObject("d").getString("obj");
  228. callback.onSuccess(code);
  229. }else {
  230. callback.onFailed();
  231. }
  232. }catch (Exception e){
  233. e.printStackTrace();
  234. callback.onFailed();
  235. }
  236. }
  237. }).start();
  238. }
  239. @Override
  240. public void changePwd(final String pwd, final String newPwd, final ResponseCallback callback) {
  241. new Thread(new Runnable() {
  242. @Override
  243. public void run() {
  244. try{
  245. FormBody body = new FormBody.Builder()
  246. .add("id",AccountCache.getAccount().getId())
  247. .add("pwd",pwd)
  248. .add("new_pwd",newPwd)
  249. .build();
  250. Request request = new Request.Builder()
  251. .post(body)
  252. .url(CHANGE_PWD)
  253. .build();
  254. Response response = client.newCall(request).execute();
  255. if (200 == response.code()){
  256. String resultStr = response.body().string();
  257. Boolean isUpdate = new JSONObject(resultStr).getJSONObject("d").getBoolean("update");
  258. if (isUpdate){
  259. callback.onSuccess(null);
  260. }else {
  261. callback.onFailed();
  262. }
  263. }else {
  264. callback.onFailed();
  265. }
  266. }catch (Exception e){
  267. e.printStackTrace();
  268. callback.onFailed();
  269. }
  270. }
  271. }).start();
  272. }
  273. @Override
  274. public void getGiftList(final ResponseCallback callback) {
  275. new Thread(new Runnable() {
  276. @Override
  277. public void run() {
  278. try {
  279. Request request = new Request.Builder()
  280. .url(NetInfo.GIFT_LIST)
  281. .build();
  282. Response response = client.newCall(request).execute();
  283. if (200 == response.code()) {
  284. String result = response.body().string();
  285. GiftBean.DBean gift = (new Gson().fromJson(result, GiftBean.class)).getD();
  286. callback.onSuccess(gift);
  287. } else {
  288. callback.onFailed();
  289. }
  290. } catch (Exception e) {
  291. e.printStackTrace();
  292. callback.onFailed();
  293. }
  294. }
  295. }).start();
  296. }
  297. @Override
  298. public void uploadImgs(String filePath, final ResponseCallback callback) {
  299. //File file = new File(filePath);
  300. MultipartBody.Builder builder = new MultipartBody.Builder();
  301. builder.setType(MultipartBody.FORM);
  302. builder.addFormDataPart("user_id", AccountCache.getAccount().getId());
  303. String targetPath = SheishuoApplication.getContext().getCacheDir()+"/compressPic.jpg";
  304. //调用压缩图片的方法,返回压缩后的图片path
  305. final File compressedPic = ImgUtil.compressImage(filePath, targetPath, 50);
  306. builder.addFormDataPart("upload", compressedPic.getName(), RequestBody.create(null, compressedPic));
  307. RequestBody body = builder.build();
  308. Request request = new Request.Builder()
  309. .url(NetInfo.UPLOAD_IMG)
  310. .post(body)
  311. .build();
  312. Call call = client.newCall(request);
  313. call.enqueue(new Callback() {
  314. @Override
  315. public void onFailure(Call call, IOException e) {
  316. callback.onFailed();
  317. e.printStackTrace();
  318. }
  319. @Override
  320. public void onResponse(Call call, Response response) throws IOException {
  321. try {
  322. JSONObject jsonObject = new JSONObject(response.body().string());
  323. if (jsonObject.getInt("c") == 0) {
  324. int imgCode = jsonObject.getJSONObject("d").getInt("aid");
  325. callback.onSuccess(imgCode);
  326. } else {
  327. callback.onFailed();
  328. }
  329. } catch (JSONException e) {
  330. e.printStackTrace();
  331. callback.onFailed();
  332. }
  333. }
  334. });
  335. }
  336. @Override
  337. public void uploadSocialPhoto(String filePath, final ResponseCallback callback) {
  338. File file = new File(filePath);
  339. MultipartBody.Builder builder = new MultipartBody.Builder();
  340. builder.setType(MultipartBody.FORM);
  341. builder.addFormDataPart("user_id", AccountCache.getAccount().getId());
  342. builder.addFormDataPart("upload", file.getName(), RequestBody.create(null, file));
  343. RequestBody body = builder.build();
  344. Request request = new Request.Builder()
  345. .url(UPLOAD_SOCIAL_PHOTO)
  346. .post(body)
  347. .build();
  348. Call call = client.newCall(request);
  349. call.enqueue(new Callback() {
  350. @Override
  351. public void onFailure(Call call, IOException e) {
  352. callback.onFailed();
  353. e.printStackTrace();
  354. }
  355. @Override
  356. public void onResponse(Call call, Response response) throws IOException {
  357. try {
  358. String resultStr = response.body().string();
  359. Log.e("resultStr",resultStr);
  360. JSONObject jsonObject = new JSONObject(resultStr);
  361. if (jsonObject.getInt("c") == 0)
  362. {
  363. JSONObject data = jsonObject.getJSONObject("d");
  364. SocialPhoto photo = new SocialPhoto();
  365. photo.setId(data.getString("id"));
  366. photo.setUser_id(AccountCache.getAccount().getId());
  367. photo.setThumb(data.getString("thumb"));
  368. photo.setPic(data.getString("pic"));
  369. callback.onSuccess(photo);
  370. } else {
  371. callback.onFailed();
  372. }
  373. } catch (JSONException e) {
  374. e.printStackTrace();
  375. callback.onFailed();
  376. }
  377. }
  378. });
  379. }
  380. @Override
  381. public void deleteSocialPhoto(final String id, final ResponseCallback callback)
  382. {
  383. new Thread(new Runnable() {
  384. @Override
  385. public void run() {
  386. FormBody body = new FormBody.Builder()
  387. .add("id", id)
  388. .build();
  389. Request request = new Request.Builder()
  390. .url(NetInfo.DELETE_SOCIAL_PHOTO)
  391. .post(body)
  392. .build();
  393. try {
  394. Response response = client.newCall(request).execute();
  395. if (200 == response.code()) {
  396. callback.onSuccess(null);
  397. } else {
  398. callback.onFailed();
  399. }
  400. } catch (IOException e) {
  401. e.printStackTrace();
  402. callback.onFailed();
  403. }
  404. }
  405. }).start();
  406. }
  407. @Override
  408. public void getSocialInfo(final String targetID, final ResponseCallback callback) {
  409. new Thread(new Runnable() {
  410. @Override
  411. public void run() {
  412. FormBody body = new FormBody.Builder()
  413. .add("user_id", AccountCache.getAccount().getId())
  414. .add("target_id", targetID)
  415. .build();
  416. Request request = new Request.Builder()
  417. .url(NetInfo.GET_SOCIAL_INFO)
  418. .post(body)
  419. .build();
  420. try {
  421. Response response = client.newCall(request).execute();
  422. if (200 == response.code()) {
  423. String result = response.body().string();
  424. try {
  425. String jsonStr = new JSONObject(result).getJSONObject("d").toString();
  426. Log.e("jsonStr",jsonStr);
  427. SocialInfoBean bean = new Gson().fromJson(jsonStr, SocialInfoBean.class);
  428. //更新缓存
  429. SocialInfoCache.put(bean.getId(),bean);
  430. callback.onSuccess(bean);
  431. } catch (JSONException e) {
  432. e.printStackTrace();
  433. callback.onFailed();
  434. }
  435. } else {
  436. callback.onFailed();
  437. }
  438. } catch (IOException e) {
  439. e.printStackTrace();
  440. callback.onFailed();
  441. }
  442. }
  443. }).start();
  444. }
  445. @Override
  446. public void socialLike(final String id, final ResponseCallback callback) {
  447. new Thread(new Runnable() {
  448. @Override
  449. public void run() {
  450. FormBody body = new FormBody.Builder()
  451. .add("user_id", AccountCache.getAccount().getId())
  452. .add("social_id", id)
  453. .build();
  454. Request request = new Request.Builder()
  455. .post(body)
  456. .url(CIRCLE_USER_LIKED)
  457. .build();
  458. try{
  459. Response response = client.newCall(request).execute();
  460. String resultStr = response.body().string();
  461. CircleUserLikedBean likedBean = new Gson().fromJson(resultStr, CircleUserLikedBean.class);
  462. callback.onSuccess(likedBean);
  463. }catch (Exception e){
  464. e.printStackTrace();
  465. callback.onFailed();
  466. }
  467. }
  468. }).start();
  469. }
  470. @Override
  471. public void getSocialComment(final String id, final int lastId, final ResponseCallback callback) {
  472. new Thread(new Runnable() {
  473. @Override
  474. public void run() {
  475. FormBody body = new FormBody.Builder()
  476. .add("social_id", id)
  477. .add("last_id", String.valueOf(lastId))
  478. .build();
  479. Request request = new Request.Builder()
  480. .post(body)
  481. .url(CIRCLE_COMMENT_LIST)
  482. .build();
  483. try{
  484. Response response = client.newCall(request).execute();
  485. String resultStr = response.body().string();
  486. JSONObject json = new JSONObject(resultStr);
  487. int c = json.getInt("c");
  488. if(c == 0)
  489. {
  490. JSONObject data = json.getJSONObject("d");
  491. JSONArray list = data.getJSONArray("list");
  492. List<CircleBean.DBean.ListBean.Comment> commentList = new ArrayList<CircleBean.DBean.ListBean.Comment>();
  493. for(int i=0; i<list.length(); i++)
  494. {
  495. CircleBean.DBean.ListBean.Comment comment = new Gson().fromJson(list.getJSONObject(i).toString(), new TypeToken<CircleBean.DBean.ListBean.Comment>(){}.getType());
  496. commentList.add(comment);
  497. }
  498. callback.onSuccess(commentList);
  499. }
  500. }catch (Exception e){
  501. e.printStackTrace();
  502. callback.onFailed();
  503. }
  504. }
  505. }).start();
  506. }
  507. @Override
  508. public void deleteSocial(final String socialId, final ResponseCallback callback) {
  509. new Thread(new Runnable() {
  510. @Override
  511. public void run() {
  512. FormBody body = new FormBody.Builder()
  513. .add("id", socialId)
  514. .add("user_id", AccountCache.getAccount().getId())
  515. .build();
  516. Request request = new Request.Builder()
  517. .post(body)
  518. .url(CIRCLE_DELETE)
  519. .build();
  520. try{
  521. Response response = client.newCall(request).execute();
  522. String resultStr = response.body().string();
  523. JSONObject json = new JSONObject(resultStr);
  524. int c = json.getInt("c");
  525. if(c == 0)
  526. {
  527. callback.onSuccess(null);
  528. }
  529. }catch (Exception e){
  530. e.printStackTrace();
  531. callback.onFailed();
  532. }
  533. }
  534. }).start();
  535. }
  536. @Override
  537. public void getCityDataList(final ResponseCallback callback) {
  538. new Thread(new Runnable() {
  539. @Override
  540. public void run() {
  541. try{
  542. Request request = new Request.Builder()
  543. .url(NetInfo.GET_CITY_DATA)
  544. .get()
  545. .build();
  546. Response response = client.newCall(request).execute();
  547. if (200 == response.code()){
  548. String resultStr = response.body().string();
  549. callback.onSuccess(resultStr);
  550. }else {
  551. callback.onFailed();
  552. }
  553. }catch (Exception e){
  554. e.printStackTrace();
  555. callback.onFailed();
  556. }
  557. }
  558. }).start();
  559. }
  560. @Override
  561. public void getNearbyPeople(final String userId, final String page, final ResponseCallback callback) {
  562. new Thread(new Runnable() {
  563. @Override
  564. public void run() {
  565. FormBody body = new FormBody.Builder()
  566. .add("user_id",userId)
  567. .add("page",page)
  568. .build();
  569. Request request = new Request.Builder()
  570. .url(NetInfo.NEARBY_PEOPLE)
  571. .post(body)
  572. .build();
  573. try{
  574. Response response = client.newCall(request).execute();
  575. if (200 == response.code()){
  576. String result = response.body().string();
  577. Log.e("result",result);
  578. callback.onSuccess(result);
  579. }else {
  580. callback.onFailed();
  581. }
  582. }catch (Exception e){
  583. e.printStackTrace();
  584. callback.onFailed();
  585. }
  586. }
  587. }).start();
  588. }
  589. @Override
  590. public void getCircleList(final String targetId, final String lastId, final ResponseCallback callback) {
  591. new Thread(new Runnable() {
  592. @Override
  593. public void run() {
  594. FormBody body = new FormBody.Builder()
  595. .add("user_id",AccountCache.getAccount().getId())
  596. .add("last_id",lastId)
  597. .add("target_id",targetId)
  598. .build();
  599. Request request = new Request.Builder()
  600. .url(NetInfo.CIRCLE_LIST)
  601. .post(body)
  602. .build();
  603. try{
  604. Response response = client.newCall(request).execute();
  605. if (200 == response.code()){
  606. CircleBean bean = new Gson().fromJson(response.body().string(),CircleBean.class);
  607. callback.onSuccess(bean);
  608. }else {
  609. callback.onFailed();
  610. }
  611. }catch (Exception e){
  612. e.printStackTrace();
  613. callback.onFailed();
  614. }
  615. }
  616. }).start();
  617. }
  618. @Override
  619. public void getTradeList(int type, int page, final ResponseCallback callback) {
  620. final FormBody body = new FormBody.Builder()
  621. .add("user_id", AccountCache.getAccount().getId())
  622. .add("province", AccountCache.getAccount().getProvince())
  623. .add("page", String.valueOf(page))
  624. .add("sort", String.valueOf(type))
  625. .build();
  626. final Request request = new Request.Builder()
  627. .url(NetInfo.TRADE_LIST)
  628. .post(body)
  629. .build();
  630. new Thread(new Runnable() {
  631. @Override
  632. public void run() {
  633. try {
  634. Response response = client.newCall(request).execute();
  635. if (200 == response.code()){
  636. String json = response.body().string();
  637. TradeBean bean = new Gson().fromJson(json,TradeBean.class);
  638. if (0 != bean.getD().getList().size()){
  639. callback.onSuccess(bean);
  640. }else {
  641. callback.onFailed();
  642. }
  643. }else{
  644. callback.onFailed();
  645. }
  646. }catch (Exception e){
  647. e.printStackTrace();
  648. callback.onFailed();
  649. }
  650. }
  651. }).start();
  652. }
  653. @Override
  654. public void searchTrade(final String key, final int type, final int page, final ResponseCallback callback) {
  655. new Thread(new Runnable() {
  656. @Override
  657. public void run() {
  658. FormBody body = new FormBody.Builder()
  659. .add("user_id",AccountCache.getAccount().getId())
  660. .add("province",AccountCache.getAccount().getProvince())
  661. .add("key",key)
  662. .add("sort", String.valueOf(type))
  663. .add("page", String.valueOf(page))
  664. .build();
  665. Request request = new Request.Builder()
  666. .post(body)
  667. .url(TRADE_SEARCH)
  668. .build();
  669. try{
  670. Response response = client.newCall(request).execute();
  671. String resultStr = response.body().string();
  672. TradeBean bean = new Gson().fromJson(resultStr,new TypeToken<TradeBean>(){}.getType());
  673. callback.onSuccess(bean);
  674. }catch (Exception e){
  675. e.printStackTrace();
  676. callback.onFailed();
  677. }
  678. }
  679. }).start();
  680. }
  681. @Override
  682. public void publishTrade(final int type, final String content, final String pics, final int priority, final int priorityType, final String roomId, final ResponseCallback callback) {
  683. new Thread(new Runnable() {
  684. @Override
  685. public void run() {
  686. FormBody body = new FormBody.Builder()
  687. .add("user_id",AccountCache.getAccount().getId())
  688. .add("type", String.valueOf(type))
  689. .add("msg",content)
  690. .add("pic",pics)
  691. .add("priority",String.valueOf(priority))
  692. .add("priority_type",String.valueOf(priorityType))
  693. .add("room_id",roomId)
  694. .build();
  695. Request request = new Request.Builder()
  696. .post(body)
  697. .url(NEW_TRADE)
  698. .build();
  699. Response response = null;
  700. try {
  701. response = client.newCall(request).execute();
  702. if (200 == response.code()){
  703. String resultJson = response.body().string();
  704. callback.onSuccess(resultJson);
  705. }else {
  706. callback.onFailed();
  707. }
  708. } catch (Exception e) {
  709. e.printStackTrace();
  710. callback.onFailed();
  711. }
  712. }
  713. }).start();
  714. }
  715. @Override
  716. public void deleteTrade(final String tradeId, final ResponseCallback callback)
  717. {
  718. new Thread(new Runnable() {
  719. @Override
  720. public void run() {
  721. FormBody body = new FormBody.Builder()
  722. .add("id", tradeId)
  723. .build();
  724. Request request = new Request.Builder()
  725. .post(body)
  726. .url(DELETE_TRADE)
  727. .build();
  728. Response response = null;
  729. try {
  730. response = client.newCall(request).execute();
  731. if (200 == response.code()){
  732. String resultJson = response.body().string();
  733. callback.onSuccess(resultJson);
  734. }else {
  735. callback.onFailed();
  736. }
  737. } catch (Exception e) {
  738. e.printStackTrace();
  739. callback.onFailed();
  740. }
  741. }
  742. }).start();
  743. }
  744. @Override
  745. public void newComment(final int socialId, @Nullable final int replyId, final String comment, final ResponseCallback callback) {
  746. new Thread(new Runnable() {
  747. @Override
  748. public void run() {
  749. try{
  750. FormBody body = new FormBody.Builder()
  751. .add("user_id",AccountCache.getAccount().getId())
  752. .add("social_id", String.valueOf(socialId))
  753. .add("reply_id", String.valueOf(replyId))
  754. .add("comment",comment)
  755. .build();
  756. Request request = new Request.Builder()
  757. .url(CIRCLE_COMMENT)
  758. .post(body)
  759. .build();
  760. Response response = client.newCall(request).execute();
  761. if (200 == response.code()){
  762. String resultStr = response.body().string();
  763. Log.e("resultStr",resultStr);
  764. int code = new JSONObject(resultStr).getInt("c");
  765. if (0 == code){
  766. callback.onSuccess(code);
  767. }else {
  768. callback.onFailed();
  769. }
  770. }else {
  771. callback.onFailed();
  772. }
  773. }catch (Exception e){
  774. e.printStackTrace();
  775. callback.onFailed();
  776. }
  777. }
  778. }).start();
  779. }
  780. @Override
  781. public void updateBanner(final String filePath, final ResponseCallback callback) {
  782. new Thread(new Runnable() {
  783. @Override
  784. public void run() {
  785. File file = new File(filePath);
  786. MultipartBody.Builder builder = new MultipartBody.Builder();
  787. builder.setType(MultipartBody.FORM);
  788. builder.addFormDataPart("user_id", AccountCache.getAccount().getId());
  789. builder.addFormDataPart("upload", file.getName(), RequestBody.create(null, file));
  790. RequestBody body = builder.build();
  791. Request request = new Request.Builder()
  792. .url(NetInfo.UPDATE_BANNER)
  793. .post(body)
  794. .build();
  795. Call call = client.newCall(request);
  796. call.enqueue(new Callback() {
  797. @Override
  798. public void onFailure(Call call, IOException e) {
  799. callback.onFailed();
  800. e.printStackTrace();
  801. }
  802. @Override
  803. public void onResponse(Call call, Response response) throws IOException {
  804. try {
  805. JSONObject jsonObject = new JSONObject(response.body().string());
  806. if (jsonObject.getInt("c") == 0) {
  807. callback.onSuccess("");
  808. } else {
  809. callback.onFailed();
  810. }
  811. } catch (JSONException e) {
  812. e.printStackTrace();
  813. callback.onFailed();
  814. }
  815. }
  816. });
  817. }
  818. }).start();
  819. }
  820. @Override
  821. public void getPhoneContactRelationship(final String phoneListStr, final ResponseCallback callback) {
  822. new Thread(new Runnable() {
  823. @Override
  824. public void run() {
  825. FormBody body = new FormBody.Builder()
  826. .add("user_id",AccountCache.getAccount().getId())
  827. .add("mobiles",phoneListStr)
  828. .build();
  829. Request request = new Request.Builder()
  830. .post(body)
  831. .url(CONTACTS_RELATIONSHIP)
  832. .build();
  833. try {
  834. Response response = client.newCall(request).execute();
  835. if (200 == response.code()){
  836. String resultStr = response.body().string();
  837. Log.e("result",resultStr);
  838. String resultJson = new JSONObject(resultStr).getString("d");
  839. List<ContactRelationshipBean> list = new Gson().fromJson(resultJson,new TypeToken<List<ContactRelationshipBean>>(){}.getType());
  840. callback.onSuccess(list);
  841. }else{
  842. callback.onFailed();
  843. }
  844. }catch (Exception e){
  845. e.printStackTrace();
  846. callback.onFailed();
  847. }
  848. }
  849. }).start();
  850. }
  851. @Override
  852. public void sendAddFriendRequest(final String targetId, final String msg) {
  853. new Thread(new Runnable() {
  854. @Override
  855. public void run() {
  856. FormBody body = new FormBody.Builder()
  857. .add("user_id",AccountCache.getAccount().getId())
  858. .add("target_id",targetId)
  859. .add("msg",msg)
  860. .build();
  861. Request request = new Request.Builder()
  862. .post(body)
  863. .url(REQUEST_FRIEND)
  864. .build();
  865. Response response = null;
  866. try {
  867. response = client.newCall(request).execute();
  868. Log.e("add result",response.body().string());
  869. } catch (IOException e) {
  870. e.printStackTrace();
  871. }
  872. }
  873. }).start();
  874. }
  875. @Override
  876. public void followOrUnfollow(final String targetId) {
  877. new Thread(new Runnable() {
  878. @Override
  879. public void run() {
  880. FormBody body = new FormBody.Builder()
  881. .add("user_id",AccountCache.getAccount().getId())
  882. .add("follow_id",targetId)
  883. .build();
  884. Request request = new Request.Builder()
  885. .post(body)
  886. .url(FOLLOW_OR_UNFOLLOW_USER)
  887. .build();
  888. Response response = null;
  889. try {
  890. response = client.newCall(request).execute();
  891. Log.e("follow",response.body().string());
  892. } catch (IOException e) {
  893. e.printStackTrace();
  894. }
  895. }
  896. }).start();
  897. }
  898. @Override
  899. public void updateSocialInfo(SocialInfoBean bean,final ResponseCallback callback) {
  900. //更新缓存
  901. SocialInfoCache.put(bean.getUser_id(),bean);
  902. String user_id = bean.getUser_id();
  903. String age = bean.getAge();
  904. String show_age = bean.getShow_age();
  905. String real_name = bean.getReal_name();
  906. String id_number = bean.getId_number();
  907. String add_verify = bean.getAdd_verify();
  908. String find_mobile = bean.getFind_mobile();
  909. String find_id = bean.getFind_id();
  910. String find_nick = bean.getFind_nick();
  911. String see_social = bean.getSee_social();
  912. String effect = bean.getEffect();
  913. String hobby = bean.getHobby();
  914. String address = bean.getAddress();
  915. FormBody body = new FormBody.Builder()
  916. .add("user_id",user_id)
  917. .add("age",age)
  918. .add("show_age",show_age)
  919. .add("real_name",real_name)
  920. .add("id_number",id_number)
  921. .add("add_verify",add_verify)
  922. .add("find_mobile",find_mobile)
  923. .add("find_id",find_id)
  924. .add("find_nick",find_nick)
  925. .add("see_social",see_social)
  926. .add("effect",effect)
  927. .add("hobby",hobby)
  928. .add("address",address)
  929. .build();
  930. final Request request = new Request.Builder()
  931. .post(body)
  932. .url(UPDATE_SOCIAL_INFO)
  933. .build();
  934. new Thread(new Runnable() {
  935. @Override
  936. public void run() {
  937. try {
  938. Response response = client.newCall(request).execute();
  939. if (200 == response.code()){
  940. String resultStr = response.body().string();
  941. int resultCode = new JSONObject(resultStr).getInt("c");
  942. if (0 == resultCode){
  943. callback.onSuccess(null);
  944. }else {
  945. callback.onFailed();
  946. }
  947. }else {
  948. callback.onFailed();
  949. }
  950. } catch (Exception e) {
  951. e.printStackTrace();
  952. callback.onFailed();
  953. }
  954. }
  955. }).start();
  956. }
  957. @Override
  958. public void getHobbyList(final ResponseCallback callback) {
  959. new Thread(new Runnable() {
  960. @Override
  961. public void run() {
  962. Request request = new Request.Builder()
  963. .get()
  964. .url(GET_HOBBY)
  965. .build();
  966. try {
  967. Response response = client.newCall(request).execute();
  968. if (200 == response.code()) {
  969. String resultStr = response.body().string();
  970. JSONObject jsonObject = new JSONObject(resultStr).getJSONObject("d");
  971. Iterator<String> iterator = jsonObject.keys();
  972. while (iterator.hasNext()){
  973. String categoryName = iterator.next();
  974. JSONArray itemJsonArray = jsonObject.getJSONArray(categoryName);
  975. List<String> items = new ArrayList<>();
  976. for (int j = 0;j < itemJsonArray.length();j++){
  977. String item = itemJsonArray.getString(j);
  978. items.add(item);
  979. }
  980. HobbyCache.put(categoryName,items);
  981. }
  982. callback.onSuccess(null);
  983. } else {
  984. callback.onFailed();
  985. }
  986. }catch (Exception e){
  987. e.printStackTrace();
  988. callback.onFailed();
  989. }
  990. }
  991. }).start();
  992. }
  993. @Override
  994. public void sendInviteMsg(final String phoneNum) {
  995. new Thread(new Runnable() {
  996. @Override
  997. public void run() {
  998. try{
  999. //后四位
  1000. String msg = phoneNum.substring(phoneNum.length() - 4 , phoneNum.length());
  1001. Log.e("msg",msg);
  1002. FormBody body = new FormBody.Builder()
  1003. .add("mobile",phoneNum)
  1004. .add("msg",msg)
  1005. .build();
  1006. Request request = new Request.Builder()
  1007. .url(SEND_INVITE_MSG)
  1008. .post(body)
  1009. .build();
  1010. client.newCall(request).execute();
  1011. }catch (Exception e){
  1012. e.printStackTrace();
  1013. }
  1014. }
  1015. }).start();
  1016. }
  1017. @Override
  1018. public void submitComplaint(final String targetId, final String type, final String reason) {
  1019. new Thread(new Runnable() {
  1020. @Override
  1021. public void run() {
  1022. FormBody body = new FormBody.Builder()
  1023. .add("user_id",AccountCache.getAccount().getId())
  1024. .add("target_id",targetId)
  1025. .add("type",type)
  1026. .add("msg",reason)
  1027. .build();
  1028. Request request = new Request.Builder()
  1029. .post(body)
  1030. .url(SUBMIT_COMPLAINT)
  1031. .build();
  1032. try{
  1033. Response response = client.newCall(request).execute();
  1034. Log.e("submitComplaint",response.body().string());
  1035. }catch (Exception e){
  1036. e.printStackTrace();
  1037. }
  1038. }
  1039. }).start();
  1040. }
  1041. @Override
  1042. public void alipay(final String money, final ResponseCallback callback) {
  1043. new Thread(new Runnable() {
  1044. @Override
  1045. public void run() {
  1046. try{
  1047. FormBody body = new FormBody.Builder()
  1048. .add("user_id",AccountCache.getAccount().getId())
  1049. .add("money",money)
  1050. .build();
  1051. Request request = new Request.Builder()
  1052. .post(body)
  1053. .url(ALIPAY_URL)
  1054. .build();
  1055. Response response = client.newCall(request).execute();
  1056. if (200 == response.code()){
  1057. String resultStr = response.body().string();
  1058. callback.onSuccess(resultStr);
  1059. }else {
  1060. callback.onFailed();
  1061. }
  1062. }catch (Exception e){
  1063. e.printStackTrace();
  1064. callback.onFailed();
  1065. }
  1066. }
  1067. }).start();
  1068. }
  1069. }