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

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

https://github.com/weiganyi/dr-helper
Java | 80 lines | 61 code | 14 blank | 5 comment | 14 complexity | 1b4022ab4b288285edf59e51e1ba5e78 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 LoadOrderTask extends AsyncTask<String, Integer, Integer> {
  11. private static final String LOAD_ORDER_TASK_TAG = "LoadOrderTask";
  12. private Activity act;
  13. private Order order;
  14. public static final int LOAD_ORDER_TASK_SUCCESS = 0;
  15. public static final int LOAD_ORDER_TASK_LOCAL_FALIURE = 1;
  16. public static final int LOAD_ORDER_TASK_REMOTE_FALIURE = 2;
  17. public LoadOrderTask(Activity act) {
  18. //save the Activity that call this AsyncTask
  19. this.act = act;
  20. }
  21. protected void onPreExecute() {
  22. }
  23. protected void onProgressUpdate(Integer... progress) {
  24. }
  25. protected void onPostExecute(Integer result) {
  26. ((OrderActivity)act).doLoadOrderResult(result, order);
  27. }
  28. @Override
  29. protected Integer doInBackground(String... param) {
  30. if (param.length != 1) {
  31. Log.e(LOAD_ORDER_TASK_TAG, "LoadOrderTask.doInBackground(): there isn't one input param");
  32. return LOAD_ORDER_TASK_LOCAL_FALIURE;
  33. }
  34. OrderInfo orderReq = new OrderInfo();
  35. if (param[0] != null) {
  36. orderReq.setOrder(new Order());
  37. Order order = orderReq.getOrder();
  38. order.setOrder(Integer.parseInt(param[0]));
  39. }
  40. try {
  41. //serialize by fastjson
  42. String reqBody = JSON.toJSONString(orderReq);
  43. //send the http post and recv response
  44. String specUrl = "loadOrder.do";
  45. String respBody = HttpEngine.doPost(specUrl, reqBody);
  46. if (respBody != null && respBody.length() != 0) {
  47. //unserialize from response string
  48. OrderInfo orderResp = JSON.parseObject(respBody, OrderInfo.class);
  49. if (orderResp != null &&
  50. orderResp.isResult() == true &&
  51. orderResp.getOrder().getOrder() == orderReq.getOrder().getOrder()) {
  52. //store the order object
  53. order = orderResp.getOrder();
  54. return LOAD_ORDER_TASK_SUCCESS;
  55. }else {
  56. return LOAD_ORDER_TASK_REMOTE_FALIURE;
  57. }
  58. }else {
  59. Log.e(LOAD_ORDER_TASK_TAG, "LoadOrderTask.doInBackground(): respBody is null");
  60. }
  61. }catch(Exception e) {
  62. Log.e(LOAD_ORDER_TASK_TAG, "LoadOrderTask.doInBackground(): json serialize or http post is failure");
  63. }
  64. return LOAD_ORDER_TASK_LOCAL_FALIURE;
  65. }
  66. }