PageRenderTime 109ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/financialnet/net/http/CustomAsyncHttpClient.java

https://gitlab.com/huangjunbin/pupu
Java | 397 lines | 346 code | 41 blank | 10 comment | 87 complexity | cd428440659efa3d5e44e7e038097850 MD5 | raw file
  1. package com.financialnet.net.http;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.widget.Toast;
  7. import com.financialnet.app.AppContext;
  8. import com.financialnet.app.ui.LoginActivity;
  9. import com.financialnet.model.RequestModel;
  10. import com.financialnet.model.ResponeModel;
  11. import com.financialnet.service.MemberService;
  12. import com.financialnet.util.JsonUtil;
  13. import com.financialnet.util.UIHelper;
  14. import com.financialnet.widget.MyProgressDialog;
  15. import org.apache.http.message.BasicNameValuePair;
  16. import org.json.JSONException;
  17. import org.json.JSONObject;
  18. import cn.jpush.android.api.JPushInterface;
  19. public class CustomAsyncHttpClient {
  20. String TAG = "CustomAsyncHttpClient";
  21. private AsyncHttpClient asyncHttpClient;
  22. private MyProgressDialog dialog;
  23. private Context mContext;
  24. private ResponeModel baseModel;
  25. public CustomAsyncHttpClient(Context context) {
  26. asyncHttpClient = new AsyncHttpClient();
  27. mContext = context;
  28. if (mContext != null) {
  29. dialog = new MyProgressDialog(mContext, "", true);
  30. dialog.tv_value.setVisibility(View.GONE);
  31. }
  32. baseModel = new ResponeModel();
  33. }
  34. public void post(final RequestModel requestModel,
  35. final CustomAsyncResponehandler responseHandler) {
  36. RequestParams newParams = new RequestParams();
  37. com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
  38. for (BasicNameValuePair param : requestModel.getParams()
  39. .getParamsList()) {
  40. jsonObject.put(param.getName(), param.getValue());
  41. }
  42. newParams.fileParams = requestModel.getParams().fileParams;
  43. newParams.put("p", jsonObject.toString());
  44. Log.d(TAG, requestModel.getUrl() + "?" + newParams.toString());
  45. asyncHttpClient.addHeader("App-Authorization", AppContext.getCurrentMember() == null ? "" : AppContext.getCurrentMember().getAuthorization());
  46. asyncHttpClient.post(requestModel.getUrl(), newParams,
  47. new AsyncHttpResponseHandler() {
  48. @Override
  49. public void onStart() {
  50. Log.d(TAG, "onStart________________________");
  51. if (requestModel.isShowDialog()) {// 显示网络对话框
  52. if (mContext != null) {
  53. dialog.show();
  54. }
  55. }
  56. responseHandler.onStart();
  57. }
  58. @Override
  59. public void onFinish() {
  60. Log.d(TAG, "onFinish________________________");
  61. if (requestModel.isShowDialog()) {// 隐藏网络对话框
  62. if (mContext != null) {
  63. dialog.dismiss();
  64. }
  65. }
  66. responseHandler.onFinish();
  67. }
  68. @Override
  69. public void onSuccess(String content) {
  70. Log.d(TAG, "onSuccess________________________"
  71. + content);
  72. // TODO:解密返回的参数
  73. baseModel = JsonUtil.convertJsonToObject(content,
  74. ResponeModel.class);
  75. if (baseModel != null) {
  76. if ("200".equals(baseModel.getCode())) {
  77. baseModel.setStatus(true);
  78. }
  79. if ("-1".equals(baseModel.getCode())) {
  80. if (mContext != null) {
  81. MemberService.getInstance(
  82. mContext).loginOut();
  83. AppContext.getApplication()
  84. .logout();
  85. JPushInterface
  86. .stopPush(mContext);
  87. JPushInterface
  88. .clearAllNotifications(mContext);
  89. mContext.startActivity(new Intent(mContext, LoginActivity.class));
  90. Toast.makeText(mContext, "请重新登陆", Toast.LENGTH_SHORT).show();
  91. }
  92. return;
  93. }
  94. if (baseModel.isStatus()) {
  95. baseModel.setJson(content);
  96. if (baseModel.getData() != null
  97. && baseModel.getData().length() > 0) {
  98. String Head = baseModel.getData()
  99. .substring(0, 1);
  100. // Data是对象
  101. if ("{".equals(Head)) {
  102. JSONObject object;
  103. try {
  104. object = new JSONObject(baseModel
  105. .getData());
  106. baseModel.setDataResult(object);
  107. if (!object.isNull("totalCount")) {// list
  108. // 带总条数
  109. baseModel.setTotalCount(object
  110. .getString("totalCount"));
  111. // 设置其他分页信息
  112. }
  113. if (!object.isNull("result")) {// list
  114. // 带分页
  115. baseModel.setResult(JsonUtil
  116. .convertJsonToList(
  117. object.get(
  118. "result")
  119. .toString(),
  120. requestModel
  121. .getCls()));
  122. } else {
  123. baseModel.setResult(JsonUtil.convertJsonToObject(
  124. baseModel.getData(),
  125. requestModel.getCls()));
  126. }
  127. } catch (Exception e) {
  128. e.printStackTrace();
  129. }
  130. } else if ("[".equals(Head)) {// data直接是数组
  131. baseModel.setResult(JsonUtil
  132. .convertJsonToList(
  133. baseModel.getData(),
  134. requestModel.getCls()));
  135. }
  136. }
  137. } else {
  138. if (requestModel.isShowErrorMessage()
  139. && !requestModel.isLetsChildShow()) {
  140. if (mContext != null) {
  141. UIHelper.ShowMessage(mContext,
  142. baseModel.getMsg());
  143. }
  144. }
  145. }
  146. responseHandler.onSuccess(baseModel);
  147. } else {
  148. if (requestModel.isShowErrorMessage()) {
  149. if (mContext != null) {
  150. UIHelper.ShowMessage(mContext, "网络异常");
  151. }
  152. }
  153. }
  154. }
  155. @Override
  156. public void onFailure(Throwable error, String content) {
  157. responseHandler.onFailure(error, content);
  158. Log.d(TAG, "onFailure________________________"
  159. + content);
  160. if (content == null || content.length() < 1) {
  161. if (mContext != null) {
  162. UIHelper.ShowMessage(mContext, "服务器异常");
  163. }
  164. return;
  165. }
  166. JSONObject jsonobj;
  167. try {
  168. jsonobj = new JSONObject(content);
  169. baseModel = new ResponeModel();
  170. baseModel.setMsg(jsonobj.getString("msg"));
  171. baseModel.setCode(Integer.parseInt(jsonobj
  172. .getString("code")));
  173. baseModel.setData(jsonobj.getString("data"));
  174. baseModel.setStatus(Boolean.parseBoolean(jsonobj
  175. .getString("status")));
  176. } catch (JSONException e1) {
  177. e1.printStackTrace();
  178. }
  179. System.out.println(baseModel);
  180. if (mContext != null) {
  181. if (baseModel.getCode() == -1) {
  182. if (mContext != null) {
  183. MemberService.getInstance(
  184. mContext).loginOut();
  185. AppContext.getApplication()
  186. .logout();
  187. JPushInterface
  188. .stopPush(mContext);
  189. JPushInterface
  190. .clearAllNotifications(mContext);
  191. mContext.startActivity(new Intent(mContext, LoginActivity.class));
  192. Toast.makeText(mContext, "账号异地登陆,请重新登陆", Toast.LENGTH_SHORT).show();
  193. }
  194. }
  195. if (requestModel.isShowErrorMessage()) {
  196. if (mContext != null && baseModel.getCode() != -1) {
  197. UIHelper.ShowMessage(mContext, "网络异常");
  198. }
  199. }
  200. }
  201. }
  202. });
  203. }
  204. public void postOrign(final RequestModel requestModel,
  205. final CustomAsyncResponehandler responseHandler) {
  206. RequestParams newParams = new RequestParams();
  207. com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
  208. for (BasicNameValuePair param : requestModel.getParams()
  209. .getParamsList()) {
  210. jsonObject.put(param.getName(), param.getValue());
  211. }
  212. newParams.fileParams = requestModel.getParams().fileParams;
  213. newParams.put("p", jsonObject.toString());
  214. Log.d(TAG, requestModel.getUrl() + "?" + newParams.toString());
  215. asyncHttpClient.post(requestModel.getUrl(), newParams,
  216. new AsyncHttpResponseHandler() {
  217. @Override
  218. public void onStart() {
  219. Log.d(TAG, "onStart________________________");
  220. if (requestModel.isShowDialog()) {// 显示网络对话框
  221. if (mContext != null) {
  222. dialog.show();
  223. }
  224. }
  225. responseHandler.onStart();
  226. }
  227. @Override
  228. public void onFinish() {
  229. Log.d(TAG, "onFinish________________________");
  230. if (requestModel.isShowDialog()) {// 隐藏网络对话框
  231. if (mContext != null) {
  232. dialog.dismiss();
  233. }
  234. }
  235. responseHandler.onFinish();
  236. }
  237. @Override
  238. public void onSuccess(String content) {
  239. Log.d(TAG, "onSuccess________________________"
  240. + content);
  241. JSONObject jsonobj;
  242. try {
  243. jsonobj = new JSONObject(content);
  244. baseModel = new ResponeModel();
  245. baseModel.setMsg(jsonobj.getString("msg"));
  246. baseModel.setCode(Integer.parseInt(jsonobj
  247. .getString("code")));
  248. baseModel.setData(jsonobj.getString("data"));
  249. baseModel.setStatus(Boolean.parseBoolean(jsonobj
  250. .getString("status")));
  251. } catch (JSONException e1) {
  252. e1.printStackTrace();
  253. }
  254. // TODO:解密返回的参数
  255. if (baseModel != null) {
  256. if (baseModel.isStatus()) {
  257. baseModel.setJson(content);
  258. if (baseModel.getData() != null
  259. && baseModel.getData().length() > 0) {
  260. String Head = baseModel.getData()
  261. .substring(0, 1);
  262. // Data是对象
  263. if ("{".equals(Head)) {
  264. JSONObject object;
  265. try {
  266. object = new JSONObject(baseModel
  267. .getData());
  268. if (!object.isNull("totalCount")) {// list
  269. // 带总条数
  270. baseModel.setTotalCount(object
  271. .getString("totalCount"));
  272. // 设置其他分页信息
  273. }
  274. if (!object.isNull("result")) {// list
  275. // 带分页
  276. baseModel.setResult(JsonUtil
  277. .convertJsonToList(
  278. object.get(
  279. "result")
  280. .toString(),
  281. requestModel
  282. .getCls()));
  283. } else {
  284. baseModel.setResult(JsonUtil.convertJsonToObject(
  285. baseModel.getData(),
  286. requestModel.getCls()));
  287. }
  288. } catch (Exception e) {
  289. e.printStackTrace();
  290. }
  291. } else if ("[".equals(Head)) {// data直接是数组
  292. baseModel.setResult(JsonUtil
  293. .convertJsonToList(
  294. baseModel.getData(),
  295. requestModel.getCls()));
  296. }
  297. }
  298. } else {
  299. if (requestModel.isShowErrorMessage()) {
  300. if (mContext != null) {
  301. UIHelper.ShowMessage(mContext,
  302. baseModel.getMsg());
  303. }
  304. }
  305. }
  306. responseHandler.onSuccess(baseModel);
  307. }
  308. }
  309. @Override
  310. public void onFailure(Throwable error, String content) {
  311. responseHandler.onFailure(error, content);
  312. Log.d(TAG, "onFailure________________________"
  313. + content);
  314. if (content == null || content.length() < 1) {
  315. UIHelper.ShowMessage(mContext, "服务器异常");
  316. return;
  317. }
  318. JSONObject jsonobj;
  319. try {
  320. jsonobj = new JSONObject(content);
  321. baseModel = new ResponeModel();
  322. baseModel.setMsg(jsonobj.getString("msg"));
  323. baseModel.setCode(Integer.parseInt(jsonobj
  324. .getString("code")));
  325. baseModel.setData(jsonobj.getString("data"));
  326. baseModel.setStatus(Boolean.parseBoolean(jsonobj
  327. .getString("status")));
  328. } catch (JSONException e1) {
  329. e1.printStackTrace();
  330. }
  331. System.out.println(baseModel);
  332. if (baseModel.getCode() == -1) {
  333. if (mContext != null) {
  334. MemberService.getInstance(
  335. mContext).loginOut();
  336. AppContext.getApplication()
  337. .logout();
  338. JPushInterface
  339. .stopPush(mContext);
  340. JPushInterface
  341. .clearAllNotifications(mContext);
  342. mContext.startActivity(new Intent(mContext, LoginActivity.class));
  343. Toast.makeText(mContext, "账号异地登陆,请重新登陆", Toast.LENGTH_SHORT).show();
  344. }
  345. }
  346. if (requestModel.isShowErrorMessage()) {
  347. if (requestModel.isShowErrorMessage()) {
  348. if (mContext != null && baseModel.getCode() != -1) {
  349. UIHelper.ShowMessage(mContext, "网络异常");
  350. }
  351. }
  352. }
  353. }
  354. });
  355. }
  356. }