PageRenderTime 52ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/weiganyi/dr-helper
Java | 80 lines | 61 code | 14 blank | 5 comment | 18 complexity | 119cec7e9c24efe41d72547f3d308e52 MD5 | raw file
  1. package com.drhelper.task;
  2. import com.alibaba.fastjson.JSON;
  3. import com.drhelper.activity.CheckOrderActivity;
  4. import com.drhelper.bean.com.OneTableOneOrder;
  5. import com.drhelper.util.HttpEngine;
  6. import android.app.Activity;
  7. import android.os.AsyncTask;
  8. import android.util.Log;
  9. public class CheckOrderTask extends AsyncTask<String, Integer, Integer> {
  10. private static final String CHECK_ORDER_TASK_TAG = "CheckOrderTask";
  11. private Activity act;
  12. private int orderNum;
  13. public static final int CHECK_ORDER_TASK_SUCCESS = 0;
  14. public static final int CHECK_ORDER_TASK_LOCAL_FALIURE = 1;
  15. public static final int CHECK_ORDER_TASK_REMOTE_FALIURE = 2;
  16. public CheckOrderTask(Activity act) {
  17. //save the Activity that call this AsyncTask
  18. this.act = act;
  19. }
  20. protected void onPreExecute() {
  21. }
  22. protected void onProgressUpdate(Integer... progress) {
  23. }
  24. protected void onPostExecute(Integer result) {
  25. ((CheckOrderActivity)act).doCheckOrderResult(result, orderNum);
  26. }
  27. @Override
  28. protected Integer doInBackground(String... param) {
  29. if (param.length != 2) {
  30. Log.e(CHECK_ORDER_TASK_TAG, "CheckOrderTask.doInBackground(): there isn't two input param");
  31. return CHECK_ORDER_TASK_LOCAL_FALIURE;
  32. }
  33. OneTableOneOrder tableOrderReq = new OneTableOneOrder();
  34. if (param[0] != null) {
  35. tableOrderReq.setOrderNum(Integer.valueOf(param[0]));
  36. }else if (param[1] != null) {
  37. tableOrderReq.setTableNum(Integer.valueOf(param[1]));
  38. }
  39. try {
  40. //serialize by fastjson
  41. String reqBody = JSON.toJSONString(tableOrderReq);
  42. //send the http post and recv response
  43. String specUrl = "checkOrder.do";
  44. String respBody = HttpEngine.doPost(specUrl, reqBody);
  45. if (respBody != null && respBody.length() != 0) {
  46. //unserialize from response string
  47. OneTableOneOrder tableOrderResp = JSON.parseObject(respBody, OneTableOneOrder.class);
  48. if (tableOrderResp != null &&
  49. tableOrderResp.isResult() != false &&
  50. (tableOrderResp.getOrderNum() == tableOrderReq.getOrderNum() ||
  51. tableOrderResp.getTableNum() == tableOrderReq.getTableNum())) {
  52. //get the order num from tableResp
  53. orderNum = tableOrderResp.getOrderNum();
  54. return CHECK_ORDER_TASK_SUCCESS;
  55. }else {
  56. return CHECK_ORDER_TASK_REMOTE_FALIURE;
  57. }
  58. }else {
  59. Log.e(CHECK_ORDER_TASK_TAG, "CheckOrderTask.doInBackground(): respBody is null");
  60. }
  61. }catch(Exception e) {
  62. Log.e(CHECK_ORDER_TASK_TAG, "CheckOrderTask.doInBackground(): json serialize or http post is failure");
  63. }
  64. return CHECK_ORDER_TASK_LOCAL_FALIURE;
  65. }
  66. }