PageRenderTime 268ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/weiganyi/dr-helper
Java | 78 lines | 59 code | 14 blank | 5 comment | 14 complexity | 7f954af4d0f1fd292e2cf78b6884aebb MD5 | raw file
  1. package com.drhelper.task;
  2. import com.alibaba.fastjson.JSON;
  3. import com.drhelper.activity.ChangeTableActivity;
  4. import com.drhelper.bean.com.TwoTableOneOrder;
  5. import com.drhelper.util.HttpEngine;
  6. import android.app.Activity;
  7. import android.os.AsyncTask;
  8. import android.util.Log;
  9. public class ChangeTableTask extends AsyncTask<String, Integer, Integer> {
  10. private static final String CHANGE_TABLE_TASK_TAG = "ChangeTableTask";
  11. private Activity act;
  12. private int orderNum;
  13. public static final int CHANGE_TABLE_TASK_SUCCESS = 0;
  14. public static final int CHANGE_TABLE_TASK_LOCAL_FALIURE = 1;
  15. public static final int CHANGE_TABLE_TASK_REMOTE_FALIURE = 2;
  16. public ChangeTableTask(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. ((ChangeTableActivity)act).doChangeTableResult(result, orderNum);
  26. }
  27. @Override
  28. protected Integer doInBackground(String... param) {
  29. if (param.length != 2) {
  30. Log.e(CHANGE_TABLE_TASK_TAG, "ChangeTableTask.doInBackground(): there isn't two input param");
  31. return CHANGE_TABLE_TASK_LOCAL_FALIURE;
  32. }
  33. TwoTableOneOrder tableOrderReq = new TwoTableOneOrder();
  34. if (param[0] != null) {
  35. tableOrderReq.setTable1(Integer.valueOf(param[0]));
  36. }
  37. if (param[1] != null) {
  38. tableOrderReq.setTable2(Integer.valueOf(param[1]));
  39. }
  40. try {
  41. //serialize by fastjson
  42. String reqBody = JSON.toJSONString(tableOrderReq);
  43. //send the http post and recv response
  44. String specUrl = "changeTable.do";
  45. String respBody = HttpEngine.doPost(specUrl, reqBody);
  46. if (respBody != null && respBody.length() != 0) {
  47. //unserialize from response string
  48. TwoTableOneOrder tableOrderResp = JSON.parseObject(respBody, TwoTableOneOrder.class);
  49. if (tableOrderResp != null && tableOrderResp.isResult() == true) {
  50. //get the order num from tableResp
  51. orderNum = tableOrderResp.getOrder();
  52. return CHANGE_TABLE_TASK_SUCCESS;
  53. }else {
  54. return CHANGE_TABLE_TASK_REMOTE_FALIURE;
  55. }
  56. }else {
  57. Log.e(CHANGE_TABLE_TASK_TAG, "ChangeTableTask.doInBackground(): respBody is null");
  58. }
  59. }catch(Exception e) {
  60. Log.e(CHANGE_TABLE_TASK_TAG, "ChangeTableTask.doInBackground(): json serialize or http post is failure");
  61. }
  62. return CHANGE_TABLE_TASK_LOCAL_FALIURE;
  63. }
  64. }