1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255 |
- package com.sheishuo.app.common.util.net;
- import android.support.annotation.Nullable;
- import android.util.Log;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- import com.sheishuo.app.SheishuoApplication;
- import com.sheishuo.app.cache.AccountCache;
- import com.sheishuo.app.cache.HobbyCache;
- import com.sheishuo.app.cache.SocialInfoCache;
- import com.sheishuo.app.common.beans.CircleBean;
- import com.sheishuo.app.common.beans.CircleUserLikedBean;
- import com.sheishuo.app.common.beans.ContactRelationshipBean;
- import com.sheishuo.app.common.beans.GiftBean;
- import com.sheishuo.app.common.beans.SocialInfoBean;
- import com.sheishuo.app.common.beans.SocialPhoto;
- import com.sheishuo.app.common.beans.TradeBean;
- import com.sheishuo.app.common.util.img.ImgUtil;
- import com.sheishuo.app.login.beans.LoginBean;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- import okhttp3.Call;
- import okhttp3.Callback;
- import okhttp3.FormBody;
- import okhttp3.MultipartBody;
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.RequestBody;
- import okhttp3.Response;
- import static com.sheishuo.app.common.util.net.NetInfo.*;
- /**
- * Created by KN on 2017/7/14.
- */
- public class NetImpl implements INet {
- private static OkHttpClient client = new OkHttpClient.Builder().build();
- private Gson gson = new Gson();
- @Override
- public void baseQuery() {
- }
- @Override
- public void get(String url, ResponseCallback callback) {
- Request request = new Request.Builder()
- .get()
- .url(url)
- .build();
- try {
- Response response = client.newCall(request).execute();
- if ( 200 == response.code()){
- callback.onSuccess(response.body().string());
- }else {
- callback.onFailed();
- }
- } catch (IOException e) {
- e.printStackTrace();
- callback.onFailed();
- }
- }
- @Override
- public void post(String url, FormBody body, ResponseCallback callback) {
- Request request = new Request.Builder()
- .url(url)
- .post(body)
- .build();
- try {
- Response response = client.newCall(request).execute();
- if (200 == response.code()) {
- callback.onSuccess(response.body().string());
- } else {
- callback.onFailed();
- }
- } catch (IOException e) {
- e.printStackTrace();
- callback.onFailed();
- }
- }
- @Override
- public void post(String url, RequestBody body, ResponseCallback callback) {
- Request request = new Request.Builder()
- .url(url)
- .post(body)
- .build();
- try {
- Response response = client.newCall(request).execute();
- if (200 == response.code()) {
- callback.onSuccess(response.body().string());
- } else {
- callback.onFailed();
- }
- } catch (IOException e) {
- e.printStackTrace();
- callback.onFailed();
- }
- }
- @Override
- public void updateLatitudeAndLongitude(final double longitude, final double latitude, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- Log.e("request", ""+longitude+", "+latitude);
- FormBody body = new FormBody.Builder()
- .add("id",AccountCache.getAccount().getId())
- .add("latitude", ""+latitude)
- .add("longitude", ""+longitude)
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(NetInfo.UPDATE_LATITUDE_AND_LONGITUDE)
- .build();
- Response response = client.newCall(request).execute();
- int responseCode = response.code();
- if (200 == responseCode){
- Log.e("code",""+responseCode);
- LoginBean.DBean account = AccountCache.getAccount();
- String resultStr = response.body().string();
- JSONObject data = new JSONObject(resultStr).getJSONObject("d");
- Log.e("json" ,data.toString());
- if (data.getBoolean("update")){
- account.setProvince(data.getString("province"));
- account.setCity(data.getString("city"));
- account.setDistrict(data.getString("district"));
- account.setProvince_room_id(data.getString("province_room_id"));
- account.setCity_room_id(data.getString("city_room_id"));
- account.setDistrict_room_id(data.getString("district_room_id"));
- callback.onSuccess(null);
- }
- }else {
- callback.onFailed();
- }
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void updateLocation(final String province, final String city, final String district, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- FormBody body = new FormBody.Builder()
- .add("id",AccountCache.getAccount().getId())
- .add("province",province)
- .add("city",city)
- .add("district",district)
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(NetInfo.RESET_LOCATION_BY_DISTRICT)
- .build();
- Response response = client.newCall(request).execute();
- if (200 == response.code()){
- String resultStr = response.body().string();
- callback.onSuccess(resultStr);
- }else{
- callback.onFailed();
- }
- } catch (Exception e) {
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void loginQuery(String mobile, String pwd, final ResponseCallback callback) {
- final FormBody body = new FormBody.Builder()
- .add("mobile", mobile)
- .add("pwd", pwd)
- .build();
- new Thread(new Runnable() {
- @Override
- public void run() {
- //创建FormBody
- Request request = new Request.Builder()
- .post(body)
- .url(NetInfo.INDEX + NetInfo.LOGIN_URL)
- .build();
- //发起请求并完成回调
- try {
- Response response = client.newCall(request).execute();
- if (200 == response.code()) {
- LoginBean bean = gson.fromJson(response.body().string(), LoginBean.class);
- bean.getD().setAutoJoinRoom(true);
- switch (bean.getC()) {
- case 0:
- callback.onSuccess(bean);
- break;
- default:
- callback.onFailed();
- break;
- }
- } else {
- callback.onFailed();
- }
- } catch (Exception e) {
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }
- ).start();
- }
- @Override
- public void getVerificationCode(final String mobile, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try{
- FormBody body = new FormBody.Builder()
- .add("mobile", mobile)
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(GET_CODE)
- .build();
- Response response = client.newCall(request).execute();
- if (200 == response.code()){
- String resultStr = response.body().string();
- String code = new JSONObject(resultStr).getJSONObject("d").getString("obj");
- callback.onSuccess(code);
- }else {
- callback.onFailed();
- }
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void changePwd(final String pwd, final String newPwd, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try{
- FormBody body = new FormBody.Builder()
- .add("id",AccountCache.getAccount().getId())
- .add("pwd",pwd)
- .add("new_pwd",newPwd)
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(CHANGE_PWD)
- .build();
- Response response = client.newCall(request).execute();
- if (200 == response.code()){
- String resultStr = response.body().string();
- Boolean isUpdate = new JSONObject(resultStr).getJSONObject("d").getBoolean("update");
- if (isUpdate){
- callback.onSuccess(null);
- }else {
- callback.onFailed();
- }
- }else {
- callback.onFailed();
- }
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void getGiftList(final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- Request request = new Request.Builder()
- .url(NetInfo.GIFT_LIST)
- .build();
- Response response = client.newCall(request).execute();
- if (200 == response.code()) {
- String result = response.body().string();
- GiftBean.DBean gift = (new Gson().fromJson(result, GiftBean.class)).getD();
- callback.onSuccess(gift);
- } else {
- callback.onFailed();
- }
- } catch (Exception e) {
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void uploadImgs(String filePath, final ResponseCallback callback) {
- //File file = new File(filePath);
- MultipartBody.Builder builder = new MultipartBody.Builder();
- builder.setType(MultipartBody.FORM);
- builder.addFormDataPart("user_id", AccountCache.getAccount().getId());
- String targetPath = SheishuoApplication.getContext().getCacheDir()+"/compressPic.jpg";
- //调用压缩图片的方法,返回压缩后的图片path
- final File compressedPic = ImgUtil.compressImage(filePath, targetPath, 50);
- builder.addFormDataPart("upload", compressedPic.getName(), RequestBody.create(null, compressedPic));
- RequestBody body = builder.build();
- Request request = new Request.Builder()
- .url(NetInfo.UPLOAD_IMG)
- .post(body)
- .build();
- Call call = client.newCall(request);
- call.enqueue(new Callback() {
- @Override
- public void onFailure(Call call, IOException e) {
- callback.onFailed();
- e.printStackTrace();
- }
- @Override
- public void onResponse(Call call, Response response) throws IOException {
- try {
- JSONObject jsonObject = new JSONObject(response.body().string());
- if (jsonObject.getInt("c") == 0) {
- int imgCode = jsonObject.getJSONObject("d").getInt("aid");
- callback.onSuccess(imgCode);
- } else {
- callback.onFailed();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- callback.onFailed();
- }
- }
- });
- }
- @Override
- public void uploadSocialPhoto(String filePath, final ResponseCallback callback) {
- File file = new File(filePath);
- MultipartBody.Builder builder = new MultipartBody.Builder();
- builder.setType(MultipartBody.FORM);
- builder.addFormDataPart("user_id", AccountCache.getAccount().getId());
- builder.addFormDataPart("upload", file.getName(), RequestBody.create(null, file));
- RequestBody body = builder.build();
- Request request = new Request.Builder()
- .url(UPLOAD_SOCIAL_PHOTO)
- .post(body)
- .build();
- Call call = client.newCall(request);
- call.enqueue(new Callback() {
- @Override
- public void onFailure(Call call, IOException e) {
- callback.onFailed();
- e.printStackTrace();
- }
- @Override
- public void onResponse(Call call, Response response) throws IOException {
- try {
- String resultStr = response.body().string();
- Log.e("resultStr",resultStr);
- JSONObject jsonObject = new JSONObject(resultStr);
- if (jsonObject.getInt("c") == 0)
- {
- JSONObject data = jsonObject.getJSONObject("d");
- SocialPhoto photo = new SocialPhoto();
- photo.setId(data.getString("id"));
- photo.setUser_id(AccountCache.getAccount().getId());
- photo.setThumb(data.getString("thumb"));
- photo.setPic(data.getString("pic"));
- callback.onSuccess(photo);
- } else {
- callback.onFailed();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- callback.onFailed();
- }
- }
- });
- }
- @Override
- public void deleteSocialPhoto(final String id, final ResponseCallback callback)
- {
- new Thread(new Runnable() {
- @Override
- public void run() {
- FormBody body = new FormBody.Builder()
- .add("id", id)
- .build();
- Request request = new Request.Builder()
- .url(NetInfo.DELETE_SOCIAL_PHOTO)
- .post(body)
- .build();
- try {
- Response response = client.newCall(request).execute();
- if (200 == response.code()) {
- callback.onSuccess(null);
- } else {
- callback.onFailed();
- }
- } catch (IOException e) {
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void getSocialInfo(final String targetID, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- FormBody body = new FormBody.Builder()
- .add("user_id", AccountCache.getAccount().getId())
- .add("target_id", targetID)
- .build();
- Request request = new Request.Builder()
- .url(NetInfo.GET_SOCIAL_INFO)
- .post(body)
- .build();
- try {
- Response response = client.newCall(request).execute();
- if (200 == response.code()) {
- String result = response.body().string();
- try {
- String jsonStr = new JSONObject(result).getJSONObject("d").toString();
- Log.e("jsonStr",jsonStr);
- SocialInfoBean bean = new Gson().fromJson(jsonStr, SocialInfoBean.class);
- //更新缓存
- SocialInfoCache.put(bean.getId(),bean);
- callback.onSuccess(bean);
- } catch (JSONException e) {
- e.printStackTrace();
- callback.onFailed();
- }
- } else {
- callback.onFailed();
- }
- } catch (IOException e) {
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void socialLike(final String id, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- FormBody body = new FormBody.Builder()
- .add("user_id", AccountCache.getAccount().getId())
- .add("social_id", id)
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(CIRCLE_USER_LIKED)
- .build();
- try{
- Response response = client.newCall(request).execute();
- String resultStr = response.body().string();
- CircleUserLikedBean likedBean = new Gson().fromJson(resultStr, CircleUserLikedBean.class);
- callback.onSuccess(likedBean);
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void getSocialComment(final String id, final int lastId, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- FormBody body = new FormBody.Builder()
- .add("social_id", id)
- .add("last_id", String.valueOf(lastId))
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(CIRCLE_COMMENT_LIST)
- .build();
- try{
- Response response = client.newCall(request).execute();
- String resultStr = response.body().string();
- JSONObject json = new JSONObject(resultStr);
- int c = json.getInt("c");
- if(c == 0)
- {
- JSONObject data = json.getJSONObject("d");
- JSONArray list = data.getJSONArray("list");
- List<CircleBean.DBean.ListBean.Comment> commentList = new ArrayList<CircleBean.DBean.ListBean.Comment>();
- for(int i=0; i<list.length(); i++)
- {
- CircleBean.DBean.ListBean.Comment comment = new Gson().fromJson(list.getJSONObject(i).toString(), new TypeToken<CircleBean.DBean.ListBean.Comment>(){}.getType());
- commentList.add(comment);
- }
- callback.onSuccess(commentList);
- }
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void deleteSocial(final String socialId, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- FormBody body = new FormBody.Builder()
- .add("id", socialId)
- .add("user_id", AccountCache.getAccount().getId())
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(CIRCLE_DELETE)
- .build();
- try{
- Response response = client.newCall(request).execute();
- String resultStr = response.body().string();
- JSONObject json = new JSONObject(resultStr);
- int c = json.getInt("c");
- if(c == 0)
- {
- callback.onSuccess(null);
- }
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void getCityDataList(final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try{
- Request request = new Request.Builder()
- .url(NetInfo.GET_CITY_DATA)
- .get()
- .build();
- Response response = client.newCall(request).execute();
- if (200 == response.code()){
- String resultStr = response.body().string();
- callback.onSuccess(resultStr);
- }else {
- callback.onFailed();
- }
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void getNearbyPeople(final String userId, final String page, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- FormBody body = new FormBody.Builder()
- .add("user_id",userId)
- .add("page",page)
- .build();
- Request request = new Request.Builder()
- .url(NetInfo.NEARBY_PEOPLE)
- .post(body)
- .build();
- try{
- Response response = client.newCall(request).execute();
- if (200 == response.code()){
- String result = response.body().string();
- Log.e("result",result);
- callback.onSuccess(result);
- }else {
- callback.onFailed();
- }
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void getCircleList(final String targetId, final String lastId, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- FormBody body = new FormBody.Builder()
- .add("user_id",AccountCache.getAccount().getId())
- .add("last_id",lastId)
- .add("target_id",targetId)
- .build();
- Request request = new Request.Builder()
- .url(NetInfo.CIRCLE_LIST)
- .post(body)
- .build();
- try{
- Response response = client.newCall(request).execute();
- if (200 == response.code()){
- CircleBean bean = new Gson().fromJson(response.body().string(),CircleBean.class);
- callback.onSuccess(bean);
- }else {
- callback.onFailed();
- }
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void getTradeList(int type, int page, final ResponseCallback callback) {
- final FormBody body = new FormBody.Builder()
- .add("user_id", AccountCache.getAccount().getId())
- .add("province", AccountCache.getAccount().getProvince())
- .add("page", String.valueOf(page))
- .add("sort", String.valueOf(type))
- .build();
- final Request request = new Request.Builder()
- .url(NetInfo.TRADE_LIST)
- .post(body)
- .build();
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- Response response = client.newCall(request).execute();
- if (200 == response.code()){
- String json = response.body().string();
- TradeBean bean = new Gson().fromJson(json,TradeBean.class);
- if (0 != bean.getD().getList().size()){
- callback.onSuccess(bean);
- }else {
- callback.onFailed();
- }
- }else{
- callback.onFailed();
- }
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void searchTrade(final String key, final int type, final int page, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- FormBody body = new FormBody.Builder()
- .add("user_id",AccountCache.getAccount().getId())
- .add("province",AccountCache.getAccount().getProvince())
- .add("key",key)
- .add("sort", String.valueOf(type))
- .add("page", String.valueOf(page))
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(TRADE_SEARCH)
- .build();
- try{
- Response response = client.newCall(request).execute();
- String resultStr = response.body().string();
- TradeBean bean = new Gson().fromJson(resultStr,new TypeToken<TradeBean>(){}.getType());
- callback.onSuccess(bean);
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void publishTrade(final int type, final String content, final String pics, final int priority, final int priorityType, final String roomId, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- FormBody body = new FormBody.Builder()
- .add("user_id",AccountCache.getAccount().getId())
- .add("type", String.valueOf(type))
- .add("msg",content)
- .add("pic",pics)
- .add("priority",String.valueOf(priority))
- .add("priority_type",String.valueOf(priorityType))
- .add("room_id",roomId)
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(NEW_TRADE)
- .build();
- Response response = null;
- try {
- response = client.newCall(request).execute();
- if (200 == response.code()){
- String resultJson = response.body().string();
- callback.onSuccess(resultJson);
- }else {
- callback.onFailed();
- }
- } catch (Exception e) {
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void deleteTrade(final String tradeId, final ResponseCallback callback)
- {
- new Thread(new Runnable() {
- @Override
- public void run() {
- FormBody body = new FormBody.Builder()
- .add("id", tradeId)
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(DELETE_TRADE)
- .build();
- Response response = null;
- try {
- response = client.newCall(request).execute();
- if (200 == response.code()){
- String resultJson = response.body().string();
- callback.onSuccess(resultJson);
- }else {
- callback.onFailed();
- }
- } catch (Exception e) {
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void newComment(final int socialId, @Nullable final int replyId, final String comment, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try{
- FormBody body = new FormBody.Builder()
- .add("user_id",AccountCache.getAccount().getId())
- .add("social_id", String.valueOf(socialId))
- .add("reply_id", String.valueOf(replyId))
- .add("comment",comment)
- .build();
- Request request = new Request.Builder()
- .url(CIRCLE_COMMENT)
- .post(body)
- .build();
- Response response = client.newCall(request).execute();
- if (200 == response.code()){
- String resultStr = response.body().string();
- Log.e("resultStr",resultStr);
- int code = new JSONObject(resultStr).getInt("c");
- if (0 == code){
- callback.onSuccess(code);
- }else {
- callback.onFailed();
- }
- }else {
- callback.onFailed();
- }
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void updateBanner(final String filePath, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- File file = new File(filePath);
- MultipartBody.Builder builder = new MultipartBody.Builder();
- builder.setType(MultipartBody.FORM);
- builder.addFormDataPart("user_id", AccountCache.getAccount().getId());
- builder.addFormDataPart("upload", file.getName(), RequestBody.create(null, file));
- RequestBody body = builder.build();
- Request request = new Request.Builder()
- .url(NetInfo.UPDATE_BANNER)
- .post(body)
- .build();
- Call call = client.newCall(request);
- call.enqueue(new Callback() {
- @Override
- public void onFailure(Call call, IOException e) {
- callback.onFailed();
- e.printStackTrace();
- }
- @Override
- public void onResponse(Call call, Response response) throws IOException {
- try {
- JSONObject jsonObject = new JSONObject(response.body().string());
- if (jsonObject.getInt("c") == 0) {
- callback.onSuccess("");
- } else {
- callback.onFailed();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- callback.onFailed();
- }
- }
- });
- }
- }).start();
- }
- @Override
- public void getPhoneContactRelationship(final String phoneListStr, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- FormBody body = new FormBody.Builder()
- .add("user_id",AccountCache.getAccount().getId())
- .add("mobiles",phoneListStr)
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(CONTACTS_RELATIONSHIP)
- .build();
- try {
- Response response = client.newCall(request).execute();
- if (200 == response.code()){
- String resultStr = response.body().string();
- Log.e("result",resultStr);
- String resultJson = new JSONObject(resultStr).getString("d");
- List<ContactRelationshipBean> list = new Gson().fromJson(resultJson,new TypeToken<List<ContactRelationshipBean>>(){}.getType());
- callback.onSuccess(list);
- }else{
- callback.onFailed();
- }
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void sendAddFriendRequest(final String targetId, final String msg) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- FormBody body = new FormBody.Builder()
- .add("user_id",AccountCache.getAccount().getId())
- .add("target_id",targetId)
- .add("msg",msg)
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(REQUEST_FRIEND)
- .build();
- Response response = null;
- try {
- response = client.newCall(request).execute();
- Log.e("add result",response.body().string());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }).start();
- }
- @Override
- public void followOrUnfollow(final String targetId) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- FormBody body = new FormBody.Builder()
- .add("user_id",AccountCache.getAccount().getId())
- .add("follow_id",targetId)
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(FOLLOW_OR_UNFOLLOW_USER)
- .build();
- Response response = null;
- try {
- response = client.newCall(request).execute();
- Log.e("follow",response.body().string());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }).start();
- }
- @Override
- public void updateSocialInfo(SocialInfoBean bean,final ResponseCallback callback) {
- //更新缓存
- SocialInfoCache.put(bean.getUser_id(),bean);
- String user_id = bean.getUser_id();
- String age = bean.getAge();
- String show_age = bean.getShow_age();
- String real_name = bean.getReal_name();
- String id_number = bean.getId_number();
- String add_verify = bean.getAdd_verify();
- String find_mobile = bean.getFind_mobile();
- String find_id = bean.getFind_id();
- String find_nick = bean.getFind_nick();
- String see_social = bean.getSee_social();
- String effect = bean.getEffect();
- String hobby = bean.getHobby();
- String address = bean.getAddress();
- FormBody body = new FormBody.Builder()
- .add("user_id",user_id)
- .add("age",age)
- .add("show_age",show_age)
- .add("real_name",real_name)
- .add("id_number",id_number)
- .add("add_verify",add_verify)
- .add("find_mobile",find_mobile)
- .add("find_id",find_id)
- .add("find_nick",find_nick)
- .add("see_social",see_social)
- .add("effect",effect)
- .add("hobby",hobby)
- .add("address",address)
- .build();
- final Request request = new Request.Builder()
- .post(body)
- .url(UPDATE_SOCIAL_INFO)
- .build();
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- Response response = client.newCall(request).execute();
- if (200 == response.code()){
- String resultStr = response.body().string();
- int resultCode = new JSONObject(resultStr).getInt("c");
- if (0 == resultCode){
- callback.onSuccess(null);
- }else {
- callback.onFailed();
- }
- }else {
- callback.onFailed();
- }
- } catch (Exception e) {
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void getHobbyList(final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- Request request = new Request.Builder()
- .get()
- .url(GET_HOBBY)
- .build();
- try {
- Response response = client.newCall(request).execute();
- if (200 == response.code()) {
- String resultStr = response.body().string();
- JSONObject jsonObject = new JSONObject(resultStr).getJSONObject("d");
- Iterator<String> iterator = jsonObject.keys();
- while (iterator.hasNext()){
- String categoryName = iterator.next();
- JSONArray itemJsonArray = jsonObject.getJSONArray(categoryName);
- List<String> items = new ArrayList<>();
- for (int j = 0;j < itemJsonArray.length();j++){
- String item = itemJsonArray.getString(j);
- items.add(item);
- }
- HobbyCache.put(categoryName,items);
- }
- callback.onSuccess(null);
- } else {
- callback.onFailed();
- }
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- @Override
- public void sendInviteMsg(final String phoneNum) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try{
- //后四位
- String msg = phoneNum.substring(phoneNum.length() - 4 , phoneNum.length());
- Log.e("msg",msg);
- FormBody body = new FormBody.Builder()
- .add("mobile",phoneNum)
- .add("msg",msg)
- .build();
- Request request = new Request.Builder()
- .url(SEND_INVITE_MSG)
- .post(body)
- .build();
- client.newCall(request).execute();
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }).start();
- }
- @Override
- public void submitComplaint(final String targetId, final String type, final String reason) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- FormBody body = new FormBody.Builder()
- .add("user_id",AccountCache.getAccount().getId())
- .add("target_id",targetId)
- .add("type",type)
- .add("msg",reason)
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(SUBMIT_COMPLAINT)
- .build();
- try{
- Response response = client.newCall(request).execute();
- Log.e("submitComplaint",response.body().string());
- }catch (Exception e){
- e.printStackTrace();
- }
- }
- }).start();
- }
- @Override
- public void alipay(final String money, final ResponseCallback callback) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- try{
- FormBody body = new FormBody.Builder()
- .add("user_id",AccountCache.getAccount().getId())
- .add("money",money)
- .build();
- Request request = new Request.Builder()
- .post(body)
- .url(ALIPAY_URL)
- .build();
- Response response = client.newCall(request).execute();
- if (200 == response.code()){
- String resultStr = response.body().string();
- callback.onSuccess(resultStr);
- }else {
- callback.onFailed();
- }
- }catch (Exception e){
- e.printStackTrace();
- callback.onFailed();
- }
- }
- }).start();
- }
- }
|