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

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

https://github.com/weiganyi/dr-helper
Java | 73 lines | 55 code | 14 blank | 4 comment | 12 complexity | bde847b06fa71e97754f573d270be357 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 SubmitOrderTask extends AsyncTask<Order, Integer, Integer> {
  11. private static final String SUBMIT_ORDER_TASK_TAG = "SubmitOrderTask";
  12. private Activity act;
  13. public static final int SUBMIT_ORDER_TASK_SUCCESS = 0;
  14. public static final int SUBMIT_ORDER_TASK_LOCAL_FALIURE = 1;
  15. public static final int SUBMIT_ORDER_TASK_REMOTE_FALIURE = 2;
  16. public SubmitOrderTask(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).doSubmitOrderResult(result);
  26. }
  27. @Override
  28. protected Integer doInBackground(Order... param) {
  29. if (param.length != 1) {
  30. Log.e(SUBMIT_ORDER_TASK_TAG, "SubmitOrderTask.doInBackground(): there isn't one input param");
  31. return SUBMIT_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 = "submitOrder.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 SUBMIT_ORDER_TASK_SUCCESS;
  48. }else {
  49. return SUBMIT_ORDER_TASK_REMOTE_FALIURE;
  50. }
  51. }else {
  52. Log.e(SUBMIT_ORDER_TASK_TAG, "SubmitOrderTask.doInBackground(): respBody is null");
  53. }
  54. }catch(Exception e) {
  55. Log.e(SUBMIT_ORDER_TASK_TAG, "SubmitOrderTask.doInBackground(): json serialize or http post is failure");
  56. }
  57. return SUBMIT_ORDER_TASK_LOCAL_FALIURE;
  58. }
  59. }