/src/com/core/base/net/http/CustomAsyncHttpClient.java

https://gitlab.com/huangjunbin/Base · Java · 160 lines · 130 code · 20 blank · 10 comment · 25 complexity · 469f3e6090532385accc77c0c386dbc5 MD5 · raw file

  1. package com.core.base.net.http;
  2. import org.apache.http.message.BasicNameValuePair;
  3. import org.json.JSONObject;
  4. import android.content.Context;
  5. import android.view.View;
  6. import com.core.base.model.RequestModel;
  7. import com.core.base.model.ResponeModel;
  8. import com.core.base.util.JsonUtil;
  9. import com.core.base.util.LogUtil;
  10. import com.core.base.util.UIHelper;
  11. import com.core.base.widget.MyProgressDialog;
  12. public class CustomAsyncHttpClient {
  13. private String TAG = "CustomAsyncHttpClient";
  14. private boolean isTag = true;
  15. private AsyncHttpClient asyncHttpClient;
  16. private MyProgressDialog dialog;
  17. private Context mContext;
  18. private ResponeModel baseModel;
  19. public CustomAsyncHttpClient(Context context) {
  20. asyncHttpClient = new AsyncHttpClient();
  21. mContext = context;
  22. if (mContext != null) {
  23. dialog = new MyProgressDialog(mContext, "", true);
  24. dialog.tv_value.setVisibility(View.GONE);
  25. }
  26. baseModel = new ResponeModel();
  27. }
  28. public void post(final RequestModel requestModel,
  29. final CustomAsyncResponehandler responseHandler) {
  30. RequestParams newParams = new RequestParams();
  31. com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
  32. for (BasicNameValuePair param : requestModel.getParams()
  33. .getParamsList()) {
  34. jsonObject.put(param.getName(), param.getValue());
  35. }
  36. newParams.fileParams = requestModel.getParams().fileParams;
  37. newParams.put("p", jsonObject.toString());
  38. LogUtil.d(TAG, requestModel.getUrl() + "&" + newParams.toString(),
  39. isTag);
  40. asyncHttpClient.post(requestModel.getUrl(), newParams,
  41. new AsyncHttpResponseHandler() {
  42. @Override
  43. public void onStart() {
  44. LogUtil.d(TAG, "onStart___", isTag);
  45. if (requestModel.isShowDialog()) {// 显示网络对话框
  46. if (mContext != null) {
  47. dialog.show();
  48. }
  49. }
  50. responseHandler.onStart();
  51. }
  52. @Override
  53. public void onFinish() {
  54. LogUtil.d(TAG, "onFinish___",
  55. isTag);
  56. if (requestModel.isShowDialog()) {// 隐藏网络对话框
  57. if (mContext != null) {
  58. dialog.dismiss();
  59. }
  60. }
  61. responseHandler.onFinish();
  62. }
  63. @Override
  64. public void onSuccess(String content) {
  65. LogUtil.d(TAG, "onSuccess___"
  66. + content, isTag);
  67. // TODO:解密返回的参数
  68. baseModel = JsonUtil.convertJsonToObject(content,
  69. ResponeModel.class);
  70. if (baseModel != null) {
  71. if (baseModel.isStatus()) {
  72. baseModel.setJson(content);
  73. if (baseModel.getData() != null
  74. && baseModel.getData().length() > 0) {
  75. String Head = baseModel.getData()
  76. .substring(0, 1);
  77. // 注:自动快捷解析的情况
  78. // 1.data直接是对象
  79. // 2.data直接是数组(不含条数)
  80. // 3.data直接是对象,对象里面包含“result”数组(条数可选)
  81. // 其它情况均需返回对象,在你的service中进行处理
  82. // Data是对象
  83. if ("{".equals(Head)) {
  84. JSONObject object;
  85. try {
  86. object = new JSONObject(baseModel
  87. .getData());
  88. baseModel.setDataResult(object);
  89. if (!object.isNull("result")) {// list
  90. // 带分页
  91. baseModel.setResult(JsonUtil
  92. .convertJsonToList(
  93. object.get(
  94. "result")
  95. .toString(),
  96. requestModel
  97. .getCls()));
  98. if (!object
  99. .isNull("totalCount")) {// list
  100. // 带总条数
  101. baseModel
  102. .setTotalCount(object
  103. .getString("totalCount"));
  104. // 设置其他分页信息
  105. }
  106. } else {
  107. baseModel.setResult(JsonUtil.convertJsonToObject(
  108. baseModel.getData(),
  109. requestModel.getCls()));
  110. }
  111. } catch (Exception e) {
  112. e.printStackTrace();
  113. }
  114. } else if ("[".equals(Head)) {// data是数组
  115. baseModel.setResult(JsonUtil
  116. .convertJsonToList(
  117. baseModel.getData(),
  118. requestModel.getCls()));
  119. }
  120. }
  121. } else {
  122. if (requestModel.isShowErrorMessage()) {
  123. if (mContext != null) {
  124. UIHelper.ShowMessage(mContext,
  125. baseModel.getMsg());
  126. }
  127. }
  128. }
  129. responseHandler.onSuccess(baseModel);
  130. }
  131. }
  132. @Override
  133. public void onFailure(Throwable error, String content) {
  134. responseHandler.onFailure(error, content);
  135. LogUtil.d(TAG, "onFailure___"
  136. + content, isTag);
  137. }
  138. });
  139. }
  140. }