PageRenderTime 787ms CodeModel.GetById 10ms RepoModel.GetById 8ms app.codeStats 0ms

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

https://github.com/weiganyi/dr-helper
Java | 76 lines | 56 code | 15 blank | 5 comment | 12 complexity | 7e0a493c700dd7d85383c57519f012fd MD5 | raw file
  1. package com.drhelper.task;
  2. import com.alibaba.fastjson.JSON;
  3. import com.drhelper.activity.CreateTableActivity;
  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 CreateTableTask extends AsyncTask<String, Integer, Integer> {
  10. private static final String CREATE_TABLE_TASK_TAG = "CreateTableTask";
  11. private Activity act;
  12. private int orderNum;
  13. public static final int CREATE_TABLE_TASK_SUCCESS = 0;
  14. public static final int CREATE_TABLE_TASK_LOCAL_FALIURE = 1;
  15. public static final int CREATE_TABLE_TASK_REMOTE_FALIURE = 2;
  16. public CreateTableTask(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. ((CreateTableActivity)act).doCreateTableResult(result, orderNum);
  26. }
  27. @Override
  28. protected Integer doInBackground(String... param) {
  29. if (param.length != 1) {
  30. Log.e(CREATE_TABLE_TASK_TAG, "CreateTableTask.doInBackground(): there isn't one input param");
  31. return CREATE_TABLE_TASK_LOCAL_FALIURE;
  32. }
  33. OneTableOneOrder tableOrderReq = new OneTableOneOrder();
  34. tableOrderReq.setTableNum(Integer.valueOf(param[0]));
  35. try {
  36. //serialize by fastjson
  37. String reqBody = JSON.toJSONString(tableOrderReq);
  38. //send the http post and recv response
  39. String specUrl = "createTable.do";
  40. String respBody = HttpEngine.doPost(specUrl, reqBody);
  41. if (respBody != null && respBody.length() != 0) {
  42. //unserialize from response string
  43. OneTableOneOrder tableOrderResp = JSON.parseObject(respBody, OneTableOneOrder.class);
  44. if (tableOrderResp != null &&
  45. tableOrderResp.isResult() != false &&
  46. tableOrderResp.getTableNum() == tableOrderReq.getTableNum()) {
  47. //get the order num from tableResp
  48. orderNum = tableOrderResp.getOrderNum();
  49. return CREATE_TABLE_TASK_SUCCESS;
  50. }else {
  51. return CREATE_TABLE_TASK_REMOTE_FALIURE;
  52. }
  53. }else {
  54. Log.e(CREATE_TABLE_TASK_TAG, "CreateTableTask.doInBackground(): respBody is null");
  55. }
  56. }catch(Exception e) {
  57. Log.e(CREATE_TABLE_TASK_TAG, "CheckTableTask.doInBackground(): json serialize or http post is failure");
  58. }
  59. return CREATE_TABLE_TASK_LOCAL_FALIURE;
  60. }
  61. }