huangbo 9 years ago
parent
commit
f09366b964

BIN
wade-mobile-func/libs/wade-mobile-com.jar


+ 21 - 105
wade-mobile-func/src/com/wade/mobile/func/MobileNetWork.java

1
package com.wade.mobile.func;
1
package com.wade.mobile.func;
2

2

3
import java.io.File;
4
import java.io.InputStream;
5
import java.util.HashMap;
3
import java.util.HashMap;
6
import java.util.HashSet;
4
import java.util.HashSet;
7
import java.util.Map;
5
import java.util.Map;
10

8

11
import android.bluetooth.BluetoothAdapter;
9
import android.bluetooth.BluetoothAdapter;
12
import android.content.Intent;
10
import android.content.Intent;
13
import android.os.AsyncTask;
14
import android.os.Build;
11
import android.os.Build;
15
import android.widget.Toast;
12
import android.widget.Toast;
16

13

34
import com.wade.mobile.util.BusinessCache;
31
import com.wade.mobile.util.BusinessCache;
35
import com.wade.mobile.util.Constant;
32
import com.wade.mobile.util.Constant;
36
import com.wade.mobile.util.EscapeUnescape;
33
import com.wade.mobile.util.EscapeUnescape;
37
import com.wade.mobile.util.FileUtil;
38
import com.wade.mobile.util.FuncConstant;
34
import com.wade.mobile.util.FuncConstant;
39
import com.wade.mobile.util.Messages;
40
import com.wade.mobile.util.StringUtil;
35
import com.wade.mobile.util.StringUtil;
41
import com.wade.mobile.util.Utility;
42
import com.wade.mobile.util.http.HttpTool;
36
import com.wade.mobile.util.http.HttpTool;
43
import com.wade.mobile.util.http.UnirestUtil;
44

37

45
public class MobileNetWork extends Plugin {
38
public class MobileNetWork extends Plugin {
46
	private boolean hasSetSmsListener;
39
	private boolean hasSetSmsListener;
199
		}
192
		}
200
	}
193
	}
201

194

202
	@SuppressWarnings({ "static-access" })
195
	@SuppressWarnings({ "rawtypes", "static-access" })
203
	private String requestBizData(String dataAction, IData param) throws Exception {
196
	private String requestBizData(String dataAction, IData param) throws Exception {
204
		Map<String,String> postData = transPostData(dataAction, param);
205
		String encode = MobileConfig.getInstance().getEncode();
206
		String dataUrl = HttpTool.toQueryString(postData);
207
		dataUrl = HttpTool.urlEncode(dataUrl, encode);
208
		String result = HttpTool.httpRequest(MobileConfig.getInstance().getRequestUrl(),
209
				dataUrl, Constant.HTTP_POST);
197
		Map<String,String> postParam = new HashMap<String,String>();
198
		postParam.put(Constant.Server.ACTION, dataAction);
210
		if (ServerDataConfig.getInstance().isEncrypt(dataAction)) {
199
		if (ServerDataConfig.getInstance().isEncrypt(dataAction)) {
211
			result = MobileSecurity.responseDecrypt(result);
212
		}
213
		return result;
214
	}
215
	
216
	public Map<String, String> transPostData(String dataAction, IData dataParam) throws Exception {
217
		// TODO Auto-generated method stub
218
		Map<String, String> postData = new HashMap<String, String>();
219
		postData.put(Constant.Server.ACTION, dataAction);
220
		if (ServerDataConfig.isEncrypt(dataAction)) {
221
			String paramData = dataParam == null ? "{}" : dataParam.toString();
200
			String paramData = param == null ? "{}" : param.toString();
222
			/* 参数加密处理 */
201
			/* 参数加密处理 */
223
			String encryptData = MobileSecurity.requestEncrypt(paramData);
202
			String encryptData = MobileSecurity.requestEncrypt(paramData);
224
			String key = MobileSecurity.getDesKey(context);
203
			String key = MobileSecurity.getDesKey(context);
225
			/* 将+号全部转换成-号 */
204
			/* 将+号全部转换成-号 */
226
			encryptData = encryptData.replace("+", "-");
205
			encryptData = encryptData.replace("+", "-");
227
			key = key.replace("+", "-");
206
			key = key.replace("+", "-");
228
			postData.put(Constant.Server.DATA, encryptData);
229
			postData.put(Constant.Server.KEY, key);
207
			postParam.put(Constant.Server.DATA, encryptData);
208
			postParam.put(Constant.Server.KEY, key);
230
		} else {
209
		} else {
231
			if (dataParam != null) {
232
				postData.put(Constant.Server.DATA, dataParam.toString());
210
			if (param != null) {
211
				postParam.put(Constant.Server.DATA, param.toString());
233
			}
212
			}
234
		}
213
		}
235
		return postData;
214

215
		String encode = MobileConfig.getInstance().getEncode();
216
		String qs = HttpTool.toQueryString(postParam);
217
		String data = HttpTool.urlEncode(qs, encode);
218
		String result = HttpTool.httpRequest(MobileConfig.getInstance().getRequestUrl(), data, Constant.HTTP_POST);
219
		if (ServerDataConfig.getInstance().isEncrypt(dataAction)) {
220
			result = MobileSecurity.responseDecrypt(result);
221
		}
222
		return result;
236
	}
223
	}
237

224

238
	public void storageDataByThread(JSONArray param) throws Exception {
225
	public void storageDataByThread(JSONArray param) throws Exception {
305
	}
292
	}
306
	
293
	
307
	public void uploadWithServlet(JSONArray param) throws Exception {
294
	public void uploadWithServlet(JSONArray param) throws Exception {
295
		
308
		JSONArray filePaths = param.getJSONArray(0);
296
		JSONArray filePaths = param.getJSONArray(0);
309
		String dataAction = param.getString(1);
297
		String dataAction = param.getString(1);
310
		IData dataParam = isNull(param.getString(2))?new DataMap():new DataMap(param.getString(2));
298
		String dataParam = param.getString(2);
311
		uploadWithServlet(filePaths, dataAction, dataParam);
299
		uploadWithServlet(filePaths, dataAction, dataParam);
312
	}
300
	}
313
	
301
	
314
	public void uploadWithServlet(final JSONArray filePaths, String dataAction, IData dataParam) throws Exception {
302
	public void uploadWithServlet(JSONArray filePaths, String dataAction, String dataParam) {
315
		// TODO Auto-generated method stub
303
		// TODO Auto-generated method stub
316
		final Map<String, String> postData = transPostData(dataAction, dataParam);
317
		final Map<String, Object> filePostData = new HashMap<String, Object>();
318
		filePostData.putAll(postData);//先装载参数
319
		new AsyncTask<String, Integer, String>() {
320
			@Override
321
			protected String doInBackground(String... arg0) {
322
				// TODO Auto-generated method stub
323
				String result = null;
324
				try{
325
					String filePath = null;
326
					File file;
327
					for(int i=0;i<filePaths.length();i++){
328
						filePath = filePaths.getString(i);
329
						file = new File(filePath);
330
						if(!file.exists()){
331
							Utility.error(Messages.FILE_NOT_EXIST+":"+filePath);
332
						}
333
						filePostData.put("FILE"+i, filePath);//再装载文件
334
					}
335
					result = UnirestUtil.uploadByPost(MobileConfig.getInstance().getRequestUrl(), filePostData);
336
				}catch(Exception e){
337
					MobileNetWork.this.error(e.getMessage());// 报错回调
338
				}
339
				return result;
340
			}
341
			
342
			@Override
343
			protected void onPostExecute(String result) {
344
				// TODO Auto-generated method stub
345
				super.onPostExecute(result);
346
				if (result != null) {
347
					MobileNetWork.this.callback(result);// 正常回调
348
				}
349
			}
350
		}.execute();
351
	}
352
	
353
	public void downloadWithServlet(JSONArray param) throws Exception {
354
		String savePath = param.getString(0);
355
		String dataAction = param.getString(1);
356
		IData dataParam = param.getString(2)==null?new DataMap():new DataMap(param.getString(2));
357
		downloadWithServlet(savePath, dataAction, dataParam);
358
	}
359
	
360
	public void downloadWithServlet(final String savePath, String dataAction, IData dataParam) throws Exception {
361
		// TODO Auto-generated method stub
362
		Map<String, String> tempPostData = transPostData(dataAction, dataParam);
363
		final Map<String, Object> postData = new HashMap<String, Object>();
364
		postData.putAll(tempPostData);
365
		new AsyncTask<String, Integer, String>() {
366
			@Override
367
			protected String doInBackground(String... arg0) {
368
				// TODO Auto-generated method stub
369
				String result = null;
370
				try{
371
					InputStream in = UnirestUtil.downloadByPost(MobileConfig.getInstance().getRequestUrl(), postData);
372
					FileUtil.writeFile(in, savePath);
373
					result = "下载成功";
374
				}catch(Exception e){
375
					MobileNetWork.this.error(savePath + "异常:"+e.getMessage());// 报错回调
376
				}
377
				return result;
378
			}
379
			
380
			@Override
381
			protected void onPostExecute(String result) {
382
				// TODO Auto-generated method stub
383
				super.onPostExecute(result);
384
				if (result != null) {
385
					MobileNetWork.this.callback(result);// 正常回调
386
				}
387
			}
388
		}.execute();
304
		
389
	}
305
	}
390

306

391
	public void uploadFile(JSONArray param) throws Exception {
307
	public void uploadFile(JSONArray param) throws Exception {