PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/huangjunbin/YNS
Java | 117 lines | 90 code | 15 blank | 12 comment | 17 complexity | 0dd66e6f7f5ee656ac3135a76a3fbd57 MD5 | raw file
  1. package com.yns.net.http;
  2. import android.content.Context;
  3. import android.view.View;
  4. import com.yns.app.AppContext;
  5. import com.yns.model.RequestModel;
  6. import com.yns.model.ResponeModel;
  7. import com.yns.util.JsonUtil;
  8. import com.yns.util.LogUtil;
  9. import com.yns.util.UIHelper;
  10. import com.yns.widget.MyProgressDialog;
  11. public class CustomAsyncHttpClient {
  12. private String TAG = "CustomAsyncHttpClient";
  13. private boolean isTag = true;
  14. private AsyncHttpClient asyncHttpClient;
  15. private MyProgressDialog dialog;
  16. private Context mContext;
  17. private ResponeModel baseModel;
  18. public CustomAsyncHttpClient(Context context) {
  19. asyncHttpClient = new AsyncHttpClient();
  20. mContext = context;
  21. if (mContext != null) {
  22. dialog = new MyProgressDialog(mContext, "", true);
  23. dialog.tv_value.setVisibility(View.GONE);
  24. }
  25. baseModel = new ResponeModel();
  26. }
  27. public void post(final RequestModel requestModel,
  28. final CustomAsyncResponehandler responseHandler) {
  29. /*
  30. * RequestParams newParams = new RequestParams();
  31. * com.alibaba.fastjson.JSONObject jsonObject = new
  32. * com.alibaba.fastjson.JSONObject(); for (BasicNameValuePair param :
  33. * requestModel.getParams() .getParamsList()) {
  34. * jsonObject.put(param.getName(), param.getValue()); }
  35. *
  36. *
  37. *
  38. * newParams.put("p", jsonObject.toString());
  39. */
  40. if (AppContext.currentUser != null && AppContext.currentUser.getUserToken() != null) {
  41. requestModel.getParams().put("userToken", AppContext.currentUser.getUserToken());
  42. }
  43. LogUtil.d(TAG, requestModel.getUrl() + "?"
  44. + requestModel.getParams().toString(), isTag);
  45. asyncHttpClient.post(requestModel.getUrl(), requestModel.getParams(),
  46. new AsyncHttpResponseHandler() {
  47. @Override
  48. public void onStart() {
  49. super.onStart();
  50. LogUtil.d(TAG, "onStart___", isTag);
  51. if (requestModel.isShowDialog()) {// 显示网络对话框
  52. if (mContext != null) {
  53. dialog.show();
  54. }
  55. }
  56. responseHandler.onStart();
  57. }
  58. @Override
  59. public void onFinish() {
  60. super.onFinish();
  61. LogUtil.d(TAG, "onFinish___", isTag);
  62. if (requestModel.isShowDialog()) {// 隐藏网络对话框
  63. if (mContext != null) {
  64. dialog.dismiss();
  65. }
  66. }
  67. responseHandler.onFinish();
  68. }
  69. @Override
  70. public void onSuccess(String content) {
  71. super.onSuccess(content);
  72. LogUtil.d(TAG, "onSuccess___" + content, isTag);
  73. // TODO:解密返回的参数
  74. baseModel = JsonUtil.convertJsonToObject(content,
  75. ResponeModel.class);
  76. if (baseModel != null) {
  77. baseModel.setCls(requestModel.getCls());
  78. baseModel.setEasyName(requestModel.getEasyName());
  79. baseModel.setJson(content);
  80. baseModel.setList(requestModel.isList());
  81. baseModel.setNew(requestModel.isNew());
  82. baseModel.setListCountKey(requestModel
  83. .getListCountKey());
  84. baseModel.init();
  85. }
  86. if (requestModel.isShowErrorMessage()&&!"200".equals(baseModel.getCode())) {
  87. if (mContext != null) {
  88. UIHelper.ShowMessage(mContext,
  89. baseModel.getMsg());
  90. }
  91. }
  92. responseHandler.onSuccess(baseModel);
  93. }
  94. @Override
  95. public void onFailure(Throwable error, String content) {
  96. super.onFailure(error, content);
  97. LogUtil.d(TAG, "onFailure___" + content, isTag);
  98. responseHandler.onFailure(error, content);
  99. }
  100. });
  101. }
  102. }