Result.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package com.xzjmyk.pm.activity.volley;
  2. import android.content.Context;
  3. import android.text.TextUtils;
  4. import android.widget.Toast;
  5. import com.alibaba.fastjson.JSON;
  6. import com.android.volley.Response;
  7. import com.android.volley.VolleyError;
  8. import com.xzjmyk.pm.activity.AppConfig;
  9. import com.xzjmyk.pm.activity.MyApplication;
  10. import com.xzjmyk.pm.activity.R;
  11. import com.xzjmyk.pm.activity.bean.LoginRegisterResult;
  12. import com.xzjmyk.pm.activity.bean.User;
  13. import com.xzjmyk.pm.activity.db.dao.UserDao;
  14. import com.xzjmyk.pm.activity.helper.LoginHelper;
  15. import com.xzjmyk.pm.activity.sp.UserSp;
  16. import com.xzjmyk.pm.activity.util.DeviceInfoUtil;
  17. import com.xzjmyk.pm.activity.util.Md5Util;
  18. import com.xzjmyk.pm.activity.view.crouton.Crouton;
  19. import java.util.HashMap;
  20. public class Result {
  21. /**
  22. * 通用的Http Result Code http 请求返回的结果码 <br/>
  23. * 0表示一般性错误</br> 1-100表示成功</br> 大于100000表示一些详细的错误</br>
  24. */
  25. public final static int CODE_ERROE = 0;// 未知的错误 或者系统内部错误
  26. public final static int CODE_SUCCESS = 1;// 正确的Http请求返回状态码
  27. public final static int CODE_ARGUMENT_ERROR1 = 1010101;// 请求参数验证失败,缺少必填参数或参数错误
  28. public final static int CODE_ARGUMENT_ERROR2 = 1010102;// 缺少请求参数:%1$s
  29. public final static int CODE_INTERNAL_ERROR = 1020101;// 接口内部异常
  30. public final static int CODE_NO_TOKEN = 1030101;// 缺少访问令牌
  31. public final static int CODE_TOKEN_ERROR = 1030102;// 访问令牌过期或无效
  32. /* 登陆接口的Http Result Code */
  33. public final static int CODE_ACCOUNT_INEXISTENCE = 1040101;// 帐号不存在
  34. public final static int CODE_ACCOUNT_ERROE = 1040102;// 帐号或密码错误
  35. public static final String RESULT_CODE = "resultCode";
  36. public static final String RESULT_MSG = "resultMsg";
  37. public static final String DATA = "data";
  38. private int resultCode;
  39. private String resultMsg;
  40. //private JSONObject jsonObject;//返回响应正文数据
  41. private String resultData;//响应正文
  42. public static boolean defaultParser(Context context, Result result, boolean showToast) {
  43. if (result == null) {
  44. if (showToast) {
  45. Toast.makeText(context, context.getString(R.string.data_exception), Toast.LENGTH_SHORT).show();
  46. }
  47. return false;
  48. }
  49. if (result.resultCode == CODE_SUCCESS) {// 成功
  50. return true;
  51. } else if (result.resultCode == CODE_NO_TOKEN) {// 缺少参数Token
  52. //TODO 发出异常登录的广播
  53. // LoginHelper.broadcastToken(context);
  54. // LoginHelper.broadcastConflict(context);
  55. if (showToast)
  56. showResultToast(context, result);
  57. return false;
  58. } else if (result.resultCode == CODE_TOKEN_ERROR) {// Token过期或错误
  59. //TODO 发出异常登录的广播
  60. loginIM(context);
  61. // LoginHelper.broadcastToken(context);
  62. // LoginHelper.broadcastConflict(context);
  63. if (showToast)
  64. showResultToast(context, result);
  65. return false;
  66. } else if (result.resultCode == CODE_INTERNAL_ERROR) {//接口内部异常
  67. Crouton.makeText(context, R.string.service_start_failed, 2000);
  68. return false;
  69. } else {
  70. if (showToast) {
  71. showResultToast(context, result);
  72. }
  73. return false;
  74. }
  75. }
  76. //当发现taken过期时候重新登陆
  77. public static void loginIM(final Context context) {
  78. String userId = UserSp.getInstance(context).getUserId("");
  79. User user = UserDao.getInstance().getUserByUserId(userId);
  80. final String phoneNumber = user.getTelephone();
  81. final String password = user.getPassword();
  82. AppConfig mConfig = MyApplication.getInstance().getConfig();
  83. // 加密之后的密码
  84. final String requestTag = "login";
  85. HashMap<String, String> params = new HashMap<String, String>();
  86. params.put("telephone", Md5Util.toMD5(phoneNumber));// 账号登陆的时候需要MD5加密,服务器需求
  87. params.put("password", password);
  88. // 附加信息
  89. params.put("model", DeviceInfoUtil.getModel());
  90. params.put("osVersion", DeviceInfoUtil.getOsVersion());
  91. params.put("serial", DeviceInfoUtil.getDeviceId(context));
  92. // 地址信息
  93. double latitude = MyApplication.getInstance().getBdLocationHelper().getLatitude();
  94. double longitude = MyApplication.getInstance().getBdLocationHelper().getLongitude();
  95. if (latitude != 0)
  96. params.put("latitude", String.valueOf(latitude));
  97. if (longitude != 0)
  98. params.put("longitude", String.valueOf(longitude));
  99. final StringJsonObjectRequest<LoginRegisterResult> request = new StringJsonObjectRequest<LoginRegisterResult>(mConfig.USER_LOGIN,
  100. new Response.ErrorListener() {
  101. @Override
  102. public void onErrorResponse(VolleyError arg0) {
  103. }
  104. }, new StringJsonObjectRequest.Listener<LoginRegisterResult>() {
  105. @Override
  106. public void onResponse(ObjectResult<LoginRegisterResult> result) {
  107. if (result == null) {
  108. return;
  109. }
  110. boolean success = false;
  111. if (result.getResultCode() == Result.CODE_SUCCESS) {
  112. success = LoginHelper.setLoginUser(context, phoneNumber, password, result);// 设置登陆用户信息
  113. }
  114. if (success) {// 登陆IM成功
  115. } else {// 登录失败
  116. }
  117. }
  118. }, LoginRegisterResult.class, params);
  119. request.setTag(requestTag);
  120. MyApplication.getInstance().getFastVolley().addDefaultRequest("Result", request);
  121. }
  122. private static void showResultToast(Context context, Result result) {
  123. if (TextUtils.isEmpty(result.resultMsg)) {
  124. Toast.makeText(context, context.getString(R.string.data_exception), Toast.LENGTH_SHORT).show();
  125. } else {
  126. Toast.makeText(context, result.resultMsg, Toast.LENGTH_SHORT).show();
  127. }
  128. }
  129. public int getResultCode() {
  130. return resultCode;
  131. }
  132. public void setResultCode(int resultCode) {
  133. this.resultCode = resultCode;
  134. }
  135. public String getResultMsg() {
  136. return resultMsg;
  137. }
  138. public void setResultMsg(String resultMsg) {
  139. this.resultMsg = resultMsg;
  140. }
  141. // public JSONObject getJsonObject() {
  142. // return jsonObject;
  143. // }
  144. //
  145. // public void setJsonObject(JSONObject jsonObject) {
  146. // this.jsonObject = jsonObject;
  147. // }
  148. public String getResultData() {
  149. return resultData;
  150. }
  151. public void setResultData(String resultData) {
  152. this.resultData = resultData;
  153. }
  154. @Override
  155. public String toString() {
  156. return JSON.toJSON(this).toString();
  157. }
  158. }