19
20 public classCommandBase {21
22 private static CommandBase m_instance = null;23
24 private HashMap m_task = new HashMap();25
26 private AsyncHttpClient m_httpClient = newAsyncHttpClient();27
28 private UploadQueue m_uploadQueue = null;29 private HashMap m_tempRequest = new HashMap();30
31 private static String m_account = "";32 private static String m_session = "";33
34 private static String HostUrl = "http://222.18.162.187:8080/wasp/webservice/ap/";35 private static JSONObject m_postData = newJSONObject();36
37 Context m_context;38 Context m_currContext;39 Activity m_currActivity;40
41 public voidsetCurrActivityContext(Context context, Activity activity) {42 m_currContext =context;43 m_currActivity =activity;44 }45
46 public voidsetMainActivityContext(Context context) {47 m_context =context;48
49 if (m_uploadQueue == null)50 m_uploadQueue = newUploadQueue(m_context);51 }52
53 public staticCommandBase instance() {54 if (m_instance == null)55 m_instance = newCommandBase();56
57 returnm_instance;58 }59
60 privateCommandBase() {61 m_httpClient.addHeader("Content-Type", "application/json");62 m_httpClient.addHeader("Request-Client", "mobile/1.0.0");63
64 /*
65 * JSONObject userData = new JSONObject(); try { userData.put("account",66 * m_account); userData.put("session", m_session);67 * m_postData.put("user", userData); } catch (JSONException e) {68 * e.printStackTrace(); }69 */
70 }71
72 public voidsetUserInfo(String name, String session) {73 m_account =name;74 m_session =session;75 System.out.println("setUser");76 System.out.println("account=" +m_account);77 JSONObject userData = newJSONObject();78 try{79 userData.put("account", m_account);80 userData.put("session", m_session);81 m_postData.put("user", userData);82 } catch(JSONException e) {83 e.printStackTrace();84 }85 }86
87 CacheManager manager = newCacheManager();88
89 public void readCache(finalTaskListener taskListener) {90 List> list = new ArrayList>();91 String queryStr =taskListener.readCache();92 list =manager.getCacheData(queryStr);93 taskListener.updateCacheDate(list);94 request(taskListener);95 }96
97 public void request(finalTaskListener task) {98 RequestParams postParam = newRequestParams();99 postParam.put("req", requestData(task.requestData()));100
101 JsonHttpResponseHandler handler = newJsonHttpResponseHandler() {102 @Override103 public voidonFailure(Throwable arg0, String arg1) {104 task.failure(arg1);105 super.onFailure(arg0, arg1);106 System.out.println("网络连接失败..");107 if (task.needCacheTask() && m_uploadQueue != null
108 && m_tempRequest.containsKey(this))109 m_uploadQueue.addItemToQueue(m_tempRequest.get(this));110 m_tempRequest.remove(this);111 Toast.makeText(m_currContext, "网络连接失败", Toast.LENGTH_SHORT)112 .show();113 }114
115 @Override116 public voidonFinish() {117 task.finish();118 super.onFinish();119 System.out.println("请求结束");120 }121
122 @Override123 public voidonSuccess(JSONObject arg1) {124 super.onSuccess(arg1);125 System.out.println("连接服务器成功!");126 System.out.println("结果=" +arg1);127 readData(arg1, this);128 }129
130 @Override131 public voidonStart() {132 task.start();133 super.onStart();134 System.out.println("请求开始");135 }136 };137
138 //TODO: save network request to local DB
139 if(task.needCacheTask()) {140 System.out.println("++++++++++++++++++++++++++++++++++++++++");141 m_tempRequest.put(142 handler,143 new UploadItem(0, requestUrl(task.requestUrl()),144 requestData(task.requestData()),145 task.contentype() == "text" ? 0 : 1, task146 .filepath()));147
148 }149
150 m_task.put(handler, task);151 m_httpClient.post(requestUrl(task.requestUrl()), postParam, handler);152
153
154
155 }156
157 privateString requestUrl(String ap) {158 return HostUrl + ap + ".action";159 }160
161 privateString requestData(JSONObject data) {162 JSONObject postData = newJSONObject();163 postData =m_postData;164 try{165 postData.put("data", data);166 } catch(JSONException e) {167 e.printStackTrace();168 }169 System.out.println("postData=" +postData);170 returnpostData.toString();171 }172
173 private voidreadData(JSONObject data, AsyncHttpResponseHandler handler) {174 TaskListener task =m_task.get(handler);175
176 if (task == null) {177 m_task.remove(handler);178 m_tempRequest.remove(handler);179 return;180 }181 //TODO: Check the response data has Error or not
182 try{183 JSONObject object = data.getJSONObject("error");184 String code = object.getString("code");185 String errorTag = object.getString("string");186 System.out.println("code=" +code);187 System.out.println("error=" +errorTag);188 if (code.equals("0")) {189 object = data.getJSONObject("data");190 task.messageUpdated(data);191 manager.updateCacheData(data);192 m_tempRequest.remove(handler);193 } else if (code.equals("-99")) {194 Toast.makeText(m_currContext, "用户信息已过期,请重新登陆",195 Toast.LENGTH_SHORT).show();196 m_currActivity.finish();197 } else{198 System.out.println("#####################");199 if (task.needCacheTask() && m_uploadQueue != null
200 &&m_tempRequest.containsKey(handler)) {201 System.out.println("_____________________________");202 m_uploadQueue.addItemToQueue(m_tempRequest.get(handler));203 }204 m_tempRequest.remove(handler);205 Toast.makeText(m_currContext, errorTag, Toast.LENGTH_SHORT)206 .show();207 }208 } catch(JSONException e) {209 e.printStackTrace();210 if (task.needCacheTask() && m_uploadQueue != null
211 &&m_tempRequest.containsKey(handler)) {212 m_uploadQueue.addItemToQueue(m_tempRequest.get(handler));213 }214 m_tempRequest.remove(handler);215
216 }217
218
219 m_task.remove(handler);220 }221
222 }