PageRenderTime 441ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Android/src/com/drhelper/task/UpdateTask.java

https://github.com/weiganyi/dr-helper
Java | 96 lines | 74 code | 17 blank | 5 comment | 20 complexity | 81c42e0bf0d0986ae549b3dab1340fac MD5 | raw file
  1. package com.drhelper.task;
  2. import java.util.List;
  3. import com.alibaba.fastjson.JSON;
  4. import com.drhelper.activity.UpdateActivity;
  5. import com.drhelper.bean.com.MenuList;
  6. import com.drhelper.bean.com.MenuTypeList;
  7. import com.drhelper.entity.Menu;
  8. import com.drhelper.entity.MenuType;
  9. import com.drhelper.util.HttpEngine;
  10. import android.app.Activity;
  11. import android.os.AsyncTask;
  12. import android.util.Log;
  13. public class UpdateTask extends AsyncTask<String, Integer, Integer> {
  14. private static final String UPDATE_TASK_TAG = "UpdateTask";
  15. private Activity act;
  16. public static final int UPDATE_TASK_SUCCESS = 0;
  17. public static final int UPDATE_TASK_LOCAL_FALIURE = 1;
  18. public static final int UPDATE_TASK_REMOTE_FALIURE = 2;
  19. private MenuTypeList menuTypeListResp = null;
  20. private MenuList menuListResp = null;
  21. public UpdateTask(Activity act) {
  22. //save the Activity that call this AsyncTask
  23. this.act = act;
  24. }
  25. protected void onPreExecute() {
  26. }
  27. protected void onProgressUpdate(Integer... progress) {
  28. }
  29. protected void onPostExecute(Integer result) {
  30. List<MenuType> menuTypeList = null;
  31. List<Menu> menuList = null;
  32. if (menuTypeListResp != null) {
  33. menuTypeList = menuTypeListResp.getList();
  34. }
  35. if (menuListResp != null) {
  36. menuList = menuListResp.getList();
  37. }
  38. ((UpdateActivity)act).doUpdateResult(result, menuTypeList, menuList);
  39. }
  40. @Override
  41. protected Integer doInBackground(String... param) {
  42. String specUrl;
  43. String respBody;
  44. try {
  45. //send the http post and recv response
  46. specUrl = "updateMenuType.do";
  47. respBody = HttpEngine.doPost(specUrl, null);
  48. if (respBody != null && respBody.length() != 0) {
  49. //unserialize from response string
  50. menuTypeListResp = JSON.parseObject(respBody, MenuTypeList.class);
  51. if (menuTypeListResp != null &&
  52. menuTypeListResp.isResult() == true) {
  53. //send the http post and recv response
  54. specUrl = "updateMenu.do";
  55. respBody = HttpEngine.doPost(specUrl, null);
  56. if (respBody != null && respBody.length() != 0) {
  57. //unserialize from response string
  58. menuListResp = JSON.parseObject(respBody, MenuList.class);
  59. if (menuListResp != null &&
  60. menuListResp.isResult() == true) {
  61. return UPDATE_TASK_SUCCESS;
  62. }else {
  63. return UPDATE_TASK_REMOTE_FALIURE;
  64. }
  65. }else {
  66. Log.e(UPDATE_TASK_TAG, "UpdateTask.doInBackground(): menu respBody is null");
  67. }
  68. }else {
  69. return UPDATE_TASK_REMOTE_FALIURE;
  70. }
  71. }else {
  72. Log.e(UPDATE_TASK_TAG, "UpdateTask.doInBackground(): menu type respBody is null");
  73. }
  74. }catch(Exception e) {
  75. Log.e(UPDATE_TASK_TAG, "UpdateTask.doInBackground(): json serialize or http post is failure");
  76. }
  77. return UPDATE_TASK_LOCAL_FALIURE;
  78. }
  79. }