PageRenderTime 57ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/weiganyi/dr-helper
Java | 73 lines | 55 code | 14 blank | 4 comment | 12 complexity | 76d2bbe6044fb17a0e842985858a0e9f MD5 | raw file
  1. package com.drhelper.task;
  2. import com.alibaba.fastjson.JSON;
  3. import com.drhelper.activity.OrderActivity;
  4. import com.drhelper.bean.com.OrderInfo;
  5. import com.drhelper.entity.Order;
  6. import com.drhelper.util.HttpEngine;
  7. import android.app.Activity;
  8. import android.os.AsyncTask;
  9. import android.util.Log;
  10. public class DeleteOrderTask extends AsyncTask<Order, Integer, Integer> {
  11. private static final String DELETE_ORDER_TASK_TAG = "DeleteOrderTask";
  12. private Activity act;
  13. public static final int DELETE_ORDER_TASK_SUCCESS = 0;
  14. public static final int DELETE_ORDER_TASK_LOCAL_FALIURE = 1;
  15. public static final int DELETE_ORDER_TASK_REMOTE_FALIURE = 2;
  16. public DeleteOrderTask(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. ((OrderActivity)act).doDeleteOrderResult(result);
  26. }
  27. @Override
  28. protected Integer doInBackground(Order... param) {
  29. if (param.length != 1) {
  30. Log.e(DELETE_ORDER_TASK_TAG, "DeleteOrderTask.doInBackground(): there isn't one input param");
  31. return DELETE_ORDER_TASK_LOCAL_FALIURE;
  32. }
  33. OrderInfo orderReq = new OrderInfo();
  34. if (param[0] != null) {
  35. orderReq.setOrder(param[0]);
  36. }
  37. try {
  38. //serialize by fastjson
  39. String reqBody = JSON.toJSONString(orderReq);
  40. //send the http post and recv response
  41. String specUrl = "deleteOrder.do";
  42. String respBody = HttpEngine.doPost(specUrl, reqBody);
  43. if (respBody != null && respBody.length() != 0) {
  44. //unserialize from response string
  45. OrderInfo orderResp = JSON.parseObject(respBody, OrderInfo.class);
  46. if (orderResp != null && orderResp.isResult() == true) {
  47. return DELETE_ORDER_TASK_SUCCESS;
  48. }else {
  49. return DELETE_ORDER_TASK_REMOTE_FALIURE;
  50. }
  51. }else {
  52. Log.e(DELETE_ORDER_TASK_TAG, "DeleteOrderTask.doInBackground(): respBody is null");
  53. }
  54. }catch(Exception e) {
  55. Log.e(DELETE_ORDER_TASK_TAG, "DeleteOrderTask.doInBackground(): json serialize or http post is failure");
  56. }
  57. return DELETE_ORDER_TASK_LOCAL_FALIURE;
  58. }
  59. }