Ver Código Fonte

dataRequest与dataRequestWithHost 新增头字段参数

leijie 6 anos atrás
pai
commit
78adb48532

+ 506 - 460
ipu-plugin-basic/src/main/java/com/ai/ipu/mobile/plugin/MobileNetWork.java

@ -1,460 +1,506 @@
1
package com.ai.ipu.mobile.plugin;
2

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

9
import org.json.JSONArray;
10

11
import android.bluetooth.BluetoothAdapter;
12
import android.content.Intent;
13
import android.net.Uri;
14
import android.os.AsyncTask;
15
import android.os.Build;
16
import android.widget.Toast;
17

18
import com.ai.ipu.basic.file.FileUtil;
19
import com.ai.ipu.basic.net.http.HttpTool;
20
import com.ai.ipu.basic.string.StringUtil;
21
import com.ai.ipu.basic.thread.IpuThread;
22
import com.ai.ipu.mobile.app.ApkUtil;
23
import com.ai.ipu.mobile.app.AppInfoUtil;
24
import com.ai.ipu.mobile.common.bluetooth.activity.ShareByBluetoothActivity;
25
import com.ai.ipu.mobile.common.bluetooth.listener.OnOpenBluetoothListener;
26
import com.ai.ipu.mobile.common.bluetooth.util.BluetoothTool;
27
import com.ai.ipu.mobile.common.sms.listener.OnSmsReceiveListener;
28
import com.ai.ipu.mobile.common.sms.util.SmsTool;
29
import com.ai.ipu.mobile.frame.IIpuMobile;
30
import com.ai.ipu.mobile.frame.config.MobileConfig;
31
import com.ai.ipu.mobile.frame.config.ServerDataConfig;
32
import com.ai.ipu.mobile.frame.plugin.Plugin;
33
import com.ai.ipu.mobile.safe.MobileSecurity;
34
import com.ai.ipu.mobile.util.BizManager;
35
import com.ai.ipu.mobile.util.BusinessCache;
36
import com.ai.ipu.mobile.util.Constant;
37
import com.ai.ipu.mobile.util.DirectionUtil;
38
import com.ai.ipu.mobile.util.FuncConstant;
39
import com.ai.ipu.mobile.util.IpuMobileUtility;
40
import com.ai.ipu.mobile.util.Messages;
41
import com.ai.ipu.mobile.util.http.UnirestUtil;
42
import com.ailk.common.data.IData;
43
import com.ailk.common.data.impl.DataMap;
44
import com.litesuits.http.LiteHttpClient;
45
import com.litesuits.http.request.Request;
46
import com.litesuits.http.response.Response;
47

48
public class MobileNetWork extends Plugin {
49
	private boolean hasSetSmsListener;
50
	private DirectionUtil directionUtil;
51

52
	public MobileNetWork(IIpuMobile ipumobile) {
53
		super(ipumobile);
54
		directionUtil = DirectionUtil.getInstance(context);
55
	}
56

57
	public void setSmsListener(JSONArray param) throws Exception {
58
		String telString = param.getString(0);
59
		setSmsListener(telString);
60
	}
61

62
	public void setSmsListener(String telString) throws Exception {
63
		HashSet<String> telSet = null;
64
		if(!isNull(telString)){
65
			telSet = new HashSet<String>();
66
			if(StringUtil.isJSONArray(telString)) {
67
				JSONArray teles = new JSONArray(telString);
68
				for(int i=0; i <teles.length() ; i++){
69
					telSet.add(teles.getString(i));
70
				}
71
			} else {
72
				telSet.add(telString);
73
			}
74
		}
75
		
76
		final HashSet<String> _telSet = telSet;
77
		if (!hasSetSmsListener) {
78
			SmsTool.setSmsListener(context, new OnSmsReceiveListener() {
79
				@Override
80
				public boolean onSmsReceive(String content, String sender, long time) {
81
					if (_telSet == null || _telSet.contains(sender)) {// 没有滤号码或者存在过滤号码均处理
82
						IData resultData = new DataMap();
83
						resultData.put(FuncConstant.CONTENT, content);
84
						resultData.put(FuncConstant.SENDER, sender);
85
						resultData.put(FuncConstant.TIME, time);
86
						String result = resultData.toString();
87
						callback(result, true);
88
						hasSetSmsListener = false;
89
						return true;
90
					} else {
91
						return false;
92
					}
93
				}
94
			});
95
			hasSetSmsListener = true;
96
		}
97
	}
98

99
	public void httpRequest(JSONArray param) throws Exception {
100
		String requestUrl = param.getString(0);
101
		String encode = param.getString(1);
102
		if (isNull(encode)) {
103
			encode = MobileConfig.getInstance().getEncode();
104
		}
105
		String result;
106
		try {
107
			result = httpRequest(requestUrl, encode);
108
		} catch (Exception e) {
109
			result = "{\"X_RESULTCODE\":\"-1\",\"X_RESULTINFO\":\"" + e.getMessage() + "\"}";
110
		}
111
		callback(result, true);
112
	}
113

114
	public String httpRequest(String requestUrl, String encode) throws Exception {
115
		// TODO Auto-generated method stub
116
		MobileConfig config = MobileConfig.getInstance();
117
		// 预处理
118
		if (!requestUrl.startsWith(Constant.HTTP)) {
119
			if (!requestUrl.startsWith("/"))
120
				requestUrl = "/" + requestUrl;
121
			requestUrl = config.getRequestHost() + requestUrl;
122
		}
123

124
		requestUrl = HttpTool.urlEncode(requestUrl, encode);
125
		return HttpTool.httpRequest(requestUrl, null, Constant.HTTP_POST, encode);
126
	}
127

128
	public void httpGet(JSONArray param) throws Exception {
129
		String requestUrl = param.getString(0);
130
		String encode = param.getString(1);
131
		if (isNull(encode)) {
132
			encode = MobileConfig.getInstance().getEncode();// UTF-8
133
		}
134
		String result;
135

136
		try {
137
			/* 方式1 */
138
			/*result = httpGet(requestUrl,encode);*/
139
			/* 方式2 */
140
			LiteHttpClient client = LiteHttpClient.getInstance(context);
141
			Response res = client.execute(new Request(requestUrl));
142
			result = res.getString();
143
		} catch (Exception e) {
144
			result = e.getMessage();
145
		}
146
		callback(result, true);
147
	}
148

149
	public String httpGet(String requestUrl, String encode) throws Exception {
150
		MobileConfig config = MobileConfig.getInstance();
151
		/* 预处理 */
152
		if (!requestUrl.startsWith(Constant.HTTP)) {
153
			if (!requestUrl.startsWith("/"))
154
				requestUrl = "/" + requestUrl;
155
			requestUrl = config.getRequestHost() + requestUrl;
156
		}
157

158
		requestUrl = HttpTool.urlEncode(requestUrl, encode);
159
		return HttpTool.httpRequest(requestUrl, null, Constant.HTTP_GET, encode);
160
	}
161

162
	public void dataRequest(JSONArray param) throws Exception {
163
		String dataAction = param.getString(0);
164
		String data = param.getString(1);
165
		String result = dataRequest(dataAction, isNull(data) ? null : new DataMap(data));
166
		callback(result, true);
167
	}
168
	
169
	public void dataRequestWithHost(JSONArray param) throws Exception {
170
	    String requestUrl = param.getString(0);// http://ip:port/servername/mobiledata
171
        String dataAction = param.getString(1);
172
        String data = param.getString(2);
173
        String result = dataRequestWithHost(requestUrl, dataAction, isNull(data) ? null : new DataMap(data));
174
        callback(result, true);
175
    }
176

177
	/**
178
	 * @param dataAction
179
	 * @param param
180
	 * @param isMustEncrypt
181
	 *            是否一定加密传输
182
	 * @return
183
	 * @throws Exception
184
	 */
185
	public String dataRequest(String dataAction, IData param) throws Exception ) {
186
		synchronized (dataAction) {
187
			String result = (String) BusinessCache.getInstance().get(dataAction);
188
			if (result != null) {
189
				BusinessCache.getInstance().remove(dataAction);
190
			} else {
191
				try {
192
				    result = requestBizData(dataAction, param);
193
				} catch (Exception e) {
194
				    DataMap data = new DataMap();
195
                    data.put("X_RESULTCODE", -1);
196
                    data.put("X_RESULTINFO", e.getMessage());
197
                    result = data.toString();
198
                }
199
			}
200
			return result;
201
		}
202
	}
203
	
204
	public String dataRequestWithHost(String requestUrl, String dataAction, IData param) throws Exception {
205
        synchronized (dataAction) {
206
            String result = (String) BusinessCache.getInstance().get(dataAction);
207
            if (result != null) {
208
                BusinessCache.getInstance().remove(dataAction);
209
            } else {
210
                try {
211
                    result = requestBizDataWithHost(requestUrl, dataAction, param);
212
                } catch (Exception e) {
213
                    DataMap data = new DataMap();
214
                    data.put("X_RESULTCODE", -1);
215
                    data.put("X_RESULTINFO", e.getMessage());
216
                    result = data.toString();
217
                }
218
            }
219
            return result;
220
        }
221
    }
222

223
	private String requestBizData(String dataAction, IData param) throws Exception {
224
		Map<String,String> postData = transPostData(dataAction, param);
225
		String dataUrl = HttpTool.toQueryStringWithEncode(postData);
226
		BizManager.recordReqUrl(MobileConfig.getInstance().getRequestUrl());
227
		String result = HttpTool.httpRequest(MobileConfig.getInstance().getRequestUrl(),
228
				dataUrl, Constant.HTTP_POST);
229
		if (ServerDataConfig.isEncrypt(dataAction)) {
230
			result = MobileSecurity.responseDecrypt(result);
231
		}
232
		return result;
233
	}
234
	
235
	private String requestBizDataWithHost(String requestUrl, String dataAction, IData param) throws Exception {
236
        Map<String,String> postData = transPostData(dataAction, param);
237
        String dataUrl = HttpTool.toQueryStringWithEncode(postData);
238
        String result = HttpTool.httpRequest(requestUrl, dataUrl, Constant.HTTP_POST);
239
        if (ServerDataConfig.isEncrypt(dataAction)) {
240
            result = MobileSecurity.responseDecrypt(result);
241
        }
242
        return result;
243
    }
244
	
245
	public Map<String, String> transPostData(String dataAction, IData dataParam) throws Exception ) {
246
		// TODO Auto-generated method stub
247
		Map<String, String> postData = new HashMap<String, String>();
248
		postData.put(Constant.Server.ACTION, dataAction);
249
		if (ServerDataConfig.isEncrypt(dataAction)) {
250
			MobileSecurity.init();
251
			/* 参数加密处理 */
252
			String key = MobileSecurity.getDesKey();
253
			postData.put(Constant.Server.KEY, key);
254
			
255
			if(dataParam!=null){
256
				String encryptData = MobileSecurity.requestEncrypt(dataParam.toString());
257
				postData.put(Constant.Server.DATA, encryptData);
258
			}
259
		} else {
260
			if (dataParam != null) {
261
				postData.put(Constant.Server.DATA, dataParam.toString());
262
			}
263
		}
264
		return postData;
265
	}
266

267
	public void storageDataByThread(JSONArray param) throws Exception {
268
		String dataAction = param.getString(0);
269
		String data = param.getString(1);
270
		boolean isEncrypt = false;
271
		if (param.length() > 2) {
272
			isEncrypt = "true".equals(param.getString(2)) ? true : false;
273
		}
274

275
		int waitoutTime = 5;
276
		if (param.length() > 3) {
277
			waitoutTime = param.getInt(3);
278
		}
279
		storageDataByThread(dataAction, isNull(data) ? null : new DataMap(data), isEncrypt, waitoutTime);
280
	}
281

282
	public void storageDataByThread(final String dataAction, final IData param,
283
			final boolean isEncrypt, long waitoutTime) throws Exception {
284
		waitoutTime = (waitoutTime < 3 || waitoutTime > 10) ? 5 : waitoutTime;// 修正waitoutTime,确保在一定范围
285
		new IpuThread(dataAction, waitoutTime) {
286
			@Override
287
			protected void execute() throws Exception {
288
				String result = requestBizData(dataAction, param);
289
				synchronized (dataAction) {
290
					BusinessCache.getInstance().put(dataAction, result);
291
				}
292
			}
293
		}.start();
294
	}
295

296
	public void shareByBluetooth(JSONArray param) throws Exception {
297
		try {
298
			BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
299
			if (adapter == null) {
300
				Toast.makeText(context, "蓝牙初始化出错!", Toast.LENGTH_LONG);
301
				return;
302
			}
303
		} catch (Exception e) {
304
			Toast.makeText(context, "蓝牙初始化出错!", Toast.LENGTH_LONG);
305
			return;
306
		}
307

308
		if (Build.VERSION.SDK_INT < AppInfoUtil.Android_4_1_2) {
309
			try {
310
				Intent intent = new Intent(context, ShareByBluetoothActivity.class);
311
				context.startActivity(intent);
312
			} catch (Exception e) {
313
				e.printStackTrace();
314
				Toast.makeText(context, "蓝牙初始化出错!", Toast.LENGTH_LONG);
315
			}
316
		} else {
317
			final BluetoothTool bluetoothTools = new BluetoothTool(context);
318
			if (bluetoothTools.isEnabled()) {
319
				bluetoothTools.sendFile(ApkUtil.getCurrApk());
320
			} else {
321
				bluetoothTools.openBluetooth(new OnOpenBluetoothListener() {
322
					public void OnOpened(BluetoothAdapter adapter) {
323
						// 开始查找设备。
324
						try {
325
							bluetoothTools.sendFile(ApkUtil.getCurrApk());
326
						} catch (Exception e) {
327
							e.printStackTrace();
328
						}
329
					}
330
				});
331
			}
332
		}
333
	}
334
	/**
335
	 * 打开浏览器
336
	 */
337
	public void openBrowser(JSONArray param) throws Exception {
338
		String url = param.getString(0);
339
		openBrowser(url);
340
	}
341
	/**
342
	 * 打开浏览器
343
	 */
344
	public void openBrowser(String url) {
345
		Uri uri = Uri.parse(url);
346
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
347
		context.startActivity(intent);
348
	}
349
	
350
	public void uploadWithServlet(JSONArray param) throws Exception {
351
		JSONArray filePaths = param.getJSONArray(0);
352
		String dataAction = param.getString(1);
353
		IData dataParam = isNull(param.getString(2))?new DataMap():new DataMap(param.getString(2));
354
		uploadWithServlet(filePaths, dataAction, dataParam);
355
	}
356
	
357
	@SuppressWarnings("unchecked")
358
	public void uploadWithServlet(final JSONArray filePaths, final String dataAction, IData dataParam) throws Exception ) {
359
		// TODO Auto-generated method stub
360
		Map<String, String> postData = transPostData(dataAction, dataParam);
361
		new AsyncTask<Map<String, String>, Integer, String>() {
362
			@Override
363
			protected String doInBackground(Map<String, String>... args) {
364
				Map<String, String> postData = args[0];
365
				Map<String, Object> fileData = new HashMap<String, Object>();
366
				// TODO Auto-generated method stub
367
				String result = null;
368
				try{
369
					String filePath = null;
370
					File file;
371
					for(int i=0;i<filePaths.length();i++){
372
						filePath = filePaths.getString(i);
373
						file = new File(filePath);
374
						if(!file.exists()){
375
							IpuMobileUtility.error(Messages.FILE_NOT_EXIST+":"+filePath);
376
						}
377
						fileData.put("UPLOAD_FILE"+i, file);//装载文件
378
					}
379
					fileData.put("UPLOAD_FILE_COUNT", filePaths.length());//装载文件数量
380
					
381
					String encode = MobileConfig.getInstance().getEncode();
382
					String dataUrl = HttpTool.urlEncode(HttpTool.toQueryString(postData), encode);
383
					String url = MobileConfig.getInstance().getRequestUrl() + "?" + dataUrl;
384
					result = UnirestUtil.uploadByPost(HttpTool.urlEscape(url).toString(), fileData);
385
					if (ServerDataConfig.isEncrypt(dataAction)) {
386
						result = MobileSecurity.responseDecrypt(result);
387
					}
388
				}catch(Exception e){
389
					MobileNetWork.this.error(e.getMessage());// 报错回调
390
					IpuMobileUtility.error(e);
391
				}
392
				return result;
393
			}
394
			
395
			@Override
396
			protected void onPostExecute(String result) {
397
				// TODO Auto-generated method stub
398
				super.onPostExecute(result);
399
				if (result != null) {
400
					MobileNetWork.this.callback(result, true);// 正常回调
401
				}
402
			}
403
		}.execute(postData);
404
	}
405
	
406
	public void downloadWithServlet(JSONArray param) throws Exception {
407
		String savePath = param.getString(0);
408
		String dataAction = param.getString(1);
409
		IData dataParam = param.getString(2) == null ? new DataMap() : new DataMap(param.getString(2));
410
		downloadWithServlet(savePath, dataAction, dataParam);
411
	}
412
	
413
	public void downloadWithServlet(final String _savePath, String dataAction, IData dataParam) throws Exception {
414
		// TODO Auto-generated method stub
415
		Map<String, String> tempPostData = transPostData(dataAction, dataParam);
416
		final Map<String, Object> postData = new HashMap<String, Object>();
417
		postData.putAll(tempPostData);
418
		new AsyncTask<String, Integer, String>() {
419
			@Override
420
			protected String doInBackground(String... arg0) {
421
				// TODO Auto-generated method stub
422
				String savePath = _savePath;
423
				try {
424
					InputStream in = UnirestUtil.downloadByPost(MobileConfig.getInstance().getRequestUrl(), postData);
425
					if (!savePath.startsWith(File.separator)) {
426
						savePath = directionUtil.getDirection(savePath, true);
427
					}
428
					File file = new File(savePath).getParentFile();
429
					if (!file.exists() && !file.mkdirs()) {
430
						IpuMobileUtility.error("创建下载文件夹失败!");
431
					}
432

433
					FileUtil.writeFile(in, savePath);
434
				} catch (Exception e) {
435
					MobileNetWork.this.error("[" + savePath + "]异常:" + e.getMessage());// 报错回调
436
				}
437
				return savePath;
438
			}
439
			
440
			@Override
441
			protected void onPostExecute(String savePath) {
442
				// TODO Auto-generated method stub
443
				super.onPostExecute(savePath);
444
				if (savePath != null) {
445
					MobileNetWork.this.callback(savePath, true);// 正常回调
446
				}
447
			}
448
		}.execute();
449
	}
450

451
	public void uploadFile(JSONArray param) throws Exception {
452
		String path = param.getString(0);
453
		uploadFile(path);
454
	}
455

456
	public void uploadFile(String path) throws Exception {
457
		// TODO Auto-generated method stub
458
		
459
	}
460
}
1
package com.ai.ipu.mobile.plugin;
2
3
import java.io.File;
4
import java.io.InputStream;
5
import java.util.HashMap;
6
import java.util.HashSet;
7
import java.util.Map;
8
9
import org.json.JSONArray;
10
import org.json.JSONException;
11
12
import android.bluetooth.BluetoothAdapter;
13
import android.content.Intent;
14
import android.net.Uri;
15
import android.os.AsyncTask;
16
import android.os.Build;
17
import android.widget.Toast;
18
19
import com.ai.ipu.basic.file.FileUtil;
20
import com.ai.ipu.basic.net.http.HttpTool;
21
import com.ai.ipu.basic.string.StringUtil;
22
import com.ai.ipu.basic.thread.IpuThread;
23
import com.ai.ipu.mobile.app.ApkUtil;
24
import com.ai.ipu.mobile.app.AppInfoUtil;
25
import com.ai.ipu.mobile.common.bluetooth.activity.ShareByBluetoothActivity;
26
import com.ai.ipu.mobile.common.bluetooth.listener.OnOpenBluetoothListener;
27
import com.ai.ipu.mobile.common.bluetooth.util.BluetoothTool;
28
import com.ai.ipu.mobile.common.sms.listener.OnSmsReceiveListener;
29
import com.ai.ipu.mobile.common.sms.util.SmsTool;
30
import com.ai.ipu.mobile.frame.IIpuMobile;
31
import com.ai.ipu.mobile.frame.config.MobileConfig;
32
import com.ai.ipu.mobile.frame.config.ServerDataConfig;
33
import com.ai.ipu.mobile.frame.plugin.Plugin;
34
import com.ai.ipu.mobile.safe.MobileSecurity;
35
import com.ai.ipu.mobile.util.BizManager;
36
import com.ai.ipu.mobile.util.BusinessCache;
37
import com.ai.ipu.mobile.util.Constant;
38
import com.ai.ipu.mobile.util.DirectionUtil;
39
import com.ai.ipu.mobile.util.FuncConstant;
40
import com.ai.ipu.mobile.util.IpuMobileLog;
41
import com.ai.ipu.mobile.util.IpuMobileUtility;
42
import com.ai.ipu.mobile.util.Messages;
43
import com.ai.ipu.mobile.util.http.UnirestUtil;
44
import com.ailk.common.data.IData;
45
import com.ailk.common.data.impl.DataMap;
46
import com.litesuits.http.LiteHttpClient;
47
import com.litesuits.http.request.Request;
48
import com.litesuits.http.response.Response;
49
50
public class MobileNetWork extends Plugin {
51
	private boolean hasSetSmsListener;
52
	private DirectionUtil directionUtil;
53
54
	public MobileNetWork(IIpuMobile ipumobile) {
55
		super(ipumobile);
56
		directionUtil = DirectionUtil.getInstance(context);
57
	}
58
59
	public void setSmsListener(JSONArray param) throws Exception {
60
		String telString = param.getString(0);
61
		setSmsListener(telString);
62
	}
63
64
	public void setSmsListener(String telString) throws Exception {
65
		HashSet<String> telSet = null;
66
		if(!isNull(telString)){
67
			telSet = new HashSet<String>();
68
			if(StringUtil.isJSONArray(telString)) {
69
				JSONArray teles = new JSONArray(telString);
70
				for(int i=0; i <teles.length() ; i++){
71
					telSet.add(teles.getString(i));
72
				}
73
			} else {
74
				telSet.add(telString);
75
			}
76
		}
77
		
78
		final HashSet<String> _telSet = telSet;
79
		if (!hasSetSmsListener) {
80
			SmsTool.setSmsListener(context, new OnSmsReceiveListener() {
81
				@Override
82
				public boolean onSmsReceive(String content, String sender, long time) {
83
					if (_telSet == null || _telSet.contains(sender)) {// 没有滤号码或者存在过滤号码均处理
84
						IData resultData = new DataMap();
85
						resultData.put(FuncConstant.CONTENT, content);
86
						resultData.put(FuncConstant.SENDER, sender);
87
						resultData.put(FuncConstant.TIME, time);
88
						String result = resultData.toString();
89
						callback(result, true);
90
						hasSetSmsListener = false;
91
						return true;
92
					} else {
93
						return false;
94
					}
95
				}
96
			});
97
			hasSetSmsListener = true;
98
		}
99
	}
100
101
	public void httpRequest(JSONArray param) throws Exception {
102
		String requestUrl = param.getString(0);
103
		String encode = param.getString(1);
104
		if (isNull(encode)) {
105
			encode = MobileConfig.getInstance().getEncode();
106
		}
107
		String result;
108
		try {
109
			result = httpRequest(requestUrl, encode);
110
		} catch (Exception e) {
111
			result = "{\"X_RESULTCODE\":\"-1\",\"X_RESULTINFO\":\"" + e.getMessage() + "\"}";
112
		}
113
		callback(result, true);
114
	}
115
116
	public String httpRequest(String requestUrl, String encode) throws Exception {
117
		// TODO Auto-generated method stub
118
		MobileConfig config = MobileConfig.getInstance();
119
		// 预处理
120
		if (!requestUrl.startsWith(Constant.HTTP)) {
121
			if (!requestUrl.startsWith("/"))
122
				requestUrl = "/" + requestUrl;
123
			requestUrl = config.getRequestHost() + requestUrl;
124
		}
125
126
		requestUrl = HttpTool.urlEncode(requestUrl, encode);
127
		return HttpTool.httpRequest(requestUrl, null, Constant.HTTP_POST, encode);
128
	}
129
130
	public void httpGet(JSONArray param) throws Exception {
131
		String requestUrl = param.getString(0);
132
		String encode = param.getString(1);
133
		if (isNull(encode)) {
134
			encode = MobileConfig.getInstance().getEncode();// UTF-8
135
		}
136
		String result;
137
138
		try {
139
			/* 方式1 */
140
			/*result = httpGet(requestUrl,encode);*/
141
			/* 方式2 */
142
			LiteHttpClient client = LiteHttpClient.getInstance(context);
143
			Response res = client.execute(new Request(requestUrl));
144
			result = res.getString();
145
		} catch (Exception e) {
146
			result = e.getMessage();
147
		}
148
		callback(result, true);
149
	}
150
151
	public String httpGet(String requestUrl, String encode) throws Exception {
152
		MobileConfig config = MobileConfig.getInstance();
153
		/* 预处理 */
154
		if (!requestUrl.startsWith(Constant.HTTP)) {
155
			if (!requestUrl.startsWith("/"))
156
				requestUrl = "/" + requestUrl;
157
			requestUrl = config.getRequestHost() + requestUrl;
158
		}
159
160
		requestUrl = HttpTool.urlEncode(requestUrl, encode);
161
		return HttpTool.httpRequest(requestUrl, null, Constant.HTTP_GET, encode);
162
	}
163
164
	public void dataRequest(JSONArray param) throws Exception {
165
		String dataAction = param.getString(0);
166
		String data = param.getString(1);
167
		String headers = null;
168
		try {
169
			headers = param.getString(5);//应用更新api后,前端未同步导致的数组越界异常
170
		} catch (JSONException e) {
171
			IpuMobileLog.e(TAG, e.getMessage());
172
		} 
173
		String result = isNull(headers) ? dataRequest(dataAction, isNull(data) ? null : new DataMap(data)) :
174
			 dataRequest(dataAction, isNull(data) ? null : new DataMap(data),new DataMap(headers));
175
		callback(result, true);
176
	}
177
	
178
	public void dataRequestWithHost(JSONArray param) throws Exception {
179
	    String requestUrl = param.getString(0);// http://ip:port/servername/mobiledata
180
        String dataAction = param.getString(1);
181
        String data = param.getString(2);
182
        String headers = null;
183
        try {
184
        	headers = param.getString(6);
185
		} catch (Exception e) ) {
186
			IpuMobileLog.d(TAG, e.getMessage());
187
		}
188
        String result = isNull(headers) ? dataRequestWithHost(requestUrl, dataAction, isNull(data) ? null : new DataMap(data)) :
189
        	dataRequestWithHost(requestUrl, dataAction, isNull(data) ? null : new DataMap(data), new DataMap(headers));
190
        callback(result, true);
191
    }
192
	
193
	/**
194
	 * @param dataAction
195
	 * @param param
196
	 * @param isMustEncrypt
197
	 *            是否一定加密传输
198
	 * @return
199
	 * @throws Exception
200
	 */
201
	public String dataRequest(String dataAction, IData param) throws Exception {
202
		return dataRequest(dataAction, param, null);
203
	}
204
205
	/**
206
	 * @param dataAction
207
	 * @param param
208
	 * @param isMustEncrypt
209
	 *            是否一定加密传输
210
	 * @return
211
	 * @throws Exception
212
	 */
213
	public String dataRequest(String dataAction, IData param,IData headers) throws Exception {
214
		synchronized (dataAction) {
215
			String result = (String) BusinessCache.getInstance().get(dataAction);
216
			if (result != null) {
217
				BusinessCache.getInstance().remove(dataAction);
218
			} else {
219
				try {
220
				    result = (null == headers) ? requestBizData(dataAction, param) : requestBizData(dataAction, param, headers);
221
				} catch (Exception e) {
222
				    DataMap data = new DataMap();
223
                    data.put("X_RESULTCODE", -1);
224
                    data.put("X_RESULTINFO", e.getMessage());
225
                    result = data.toString();
226
                }
227
			}
228
			return result;
229
		}
230
	}
231
	
232
	public String dataRequestWithHost(String requestUrl, String dataAction, IData param) throws Exception {
233
            return dataRequestWithHost(requestUrl, dataAction, param, null);
234
        }
235
236
	public String dataRequestWithHost(String requestUrl, String dataAction, IData param,IData headers) throws Exception {
237
        synchronized (dataAction) {
238
            String result = (String) BusinessCache.getInstance().get(dataAction);
239
            if (result != null) {
240
                BusinessCache.getInstance().remove(dataAction);
241
            } else {
242
                try {
243
                    result = (null == headers) ? requestBizDataWithHost(requestUrl, dataAction, param) :
244
                    	requestBizDataWithHost(requestUrl, dataAction, param, headers);
245
                } catch (Exception e) ) {
246
                    DataMap data = new DataMap();
247
                    data.put("X_RESULTCODE", -1);
248
                    data.put("X_RESULTINFO", e.getMessage());
249
                    result = data.toString();
250
                }
251
            }
252
            return result;
253
        }
254
    }
255
	
256
	/**
257
	 * 新增头字段参数
258
	 */
259
	private String requestBizData(String dataAction, IData param,IData headers) throws Exception {
260
		Map<String,String> postData = transPostData(dataAction, param);
261
		String dataUrl = HttpTool.toQueryStringWithEncode(postData);
262
		BizManager.recordReqUrl(MobileConfig.getInstance().getRequestUrl());
263
		String result = (null == headers) ? HttpTool.httpRequest(MobileConfig.getInstance().getRequestUrl(),
264
				dataUrl, Constant.HTTP_POST) : HttpTool.httpRequest(MobileConfig.getInstance().getRequestUrl(),
265
						dataUrl, Constant.HTTP_POST,headers);
266
		if (ServerDataConfig.isEncrypt(dataAction)) {
267
			result = MobileSecurity.responseDecrypt(result);
268
		}
269
		return result;
270
	}
271
	
272
	private String requestBizData(String dataAction, IData param) throws Exception {
273
		return requestBizData(dataAction, param, null);
274
	}
275
	
276
	private String requestBizDataWithHost(String requestUrl, String dataAction, IData param) throws Exception {
277
        return requestBizDataWithHost(requestUrl, dataAction, param, null);
278
    }
279
	
280
	private String requestBizDataWithHost(String requestUrl, String dataAction, IData param,IData headers) throws Exception{
281
        Map<String,String> postData = transPostData(dataAction, param);
282
        String dataUrl = HttpTool.toQueryStringWithEncode(postData);
283
        String result = (null == headers) ? HttpTool.httpRequest(requestUrl, dataUrl, Constant.HTTP_POST) : 
284
        	HttpTool.httpRequest(requestUrl, dataUrl, Constant.HTTP_POST, headers);
285
        if (ServerDataConfig.isEncrypt(dataAction)) {
286
            result = MobileSecurity.responseDecrypt(result);
287
        }
288
        return result;
289
	}
290
	
291
	public Map<String, String> transPostData(String dataAction, IData dataParam) throws Exception {
292
		// TODO Auto-generated method stub
293
		Map<String, String> postData = new HashMap<String, String>();
294
		postData.put(Constant.Server.ACTION, dataAction);
295
		if (ServerDataConfig.isEncrypt(dataAction)) {
296
			MobileSecurity.init();
297
			/* 参数加密处理 */
298
			String key = MobileSecurity.getDesKey();
299
			postData.put(Constant.Server.KEY, key);
300
			
301
			if(dataParam!=null){
302
				String encryptData = MobileSecurity.requestEncrypt(dataParam.toString());
303
				postData.put(Constant.Server.DATA, encryptData);
304
			}
305
		} else {
306
			if (dataParam != null) {
307
				postData.put(Constant.Server.DATA, dataParam.toString());
308
			}
309
		}
310
		return postData;
311
	}
312
313
	public void storageDataByThread(JSONArray param) throws Exception {
314
		String dataAction = param.getString(0);
315
		String data = param.getString(1);
316
		boolean isEncrypt = false;
317
		if (param.length() > 2) {
318
			isEncrypt = "true".equals(param.getString(2)) ? true : false;
319
		}
320
321
		int waitoutTime = 5;
322
		if (param.length() > 3) {
323
			waitoutTime = param.getInt(3);
324
		}
325
		storageDataByThread(dataAction, isNull(data) ? null : new DataMap(data), isEncrypt, waitoutTime);
326
	}
327
328
	public void storageDataByThread(final String dataAction, final IData param,
329
			final boolean isEncrypt, long waitoutTime) throws Exception {
330
		waitoutTime = (waitoutTime < 3 || waitoutTime > 10) ? 5 : waitoutTime;// 修正waitoutTime,确保在一定范围
331
		new IpuThread(dataAction, waitoutTime) {
332
			@Override
333
			protected void execute() throws Exception {
334
				String result = requestBizData(dataAction, param);
335
				synchronized (dataAction) {
336
					BusinessCache.getInstance().put(dataAction, result);
337
				}
338
			}
339
		}.start();
340
	}
341
342
	public void shareByBluetooth(JSONArray param) throws Exception {
343
		try {
344
			BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
345
			if (adapter == null) {
346
				Toast.makeText(context, "蓝牙初始化出错!", Toast.LENGTH_LONG);
347
				return;
348
			}
349
		} catch (Exception e) {
350
			Toast.makeText(context, "蓝牙初始化出错!", Toast.LENGTH_LONG);
351
			return;
352
		}
353
354
		if (Build.VERSION.SDK_INT < AppInfoUtil.Android_4_1_2) {
355
			try {
356
				Intent intent = new Intent(context, ShareByBluetoothActivity.class);
357
				context.startActivity(intent);
358
			} catch (Exception e) ) {
359
				e.printStackTrace();
360
				Toast.makeText(context, "蓝牙初始化出错!", Toast.LENGTH_LONG);
361
			}
362
		} else {
363
			final BluetoothTool bluetoothTools = new BluetoothTool(context);
364
			if (bluetoothTools.isEnabled()) {
365
				bluetoothTools.sendFile(ApkUtil.getCurrApk());
366
			} else {
367
				bluetoothTools.openBluetooth(new OnOpenBluetoothListener() {
368
					public void OnOpened(BluetoothAdapter adapter) {
369
						// 开始查找设备。
370
						try {
371
							bluetoothTools.sendFile(ApkUtil.getCurrApk());
372
						} catch (Exception e) {
373
							e.printStackTrace();
374
						}
375
					}
376
				});
377
			}
378
		}
379
	}
380
	/**
381
	 * 打开浏览器
382
	 */
383
	public void openBrowser(JSONArray param) throws Exception {
384
		String url = param.getString(0);
385
		openBrowser(url);
386
	}
387
	/**
388
	 * 打开浏览器
389
	 */
390
	public void openBrowser(String url) {
391
		Uri uri = Uri.parse(url);
392
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
393
		context.startActivity(intent);
394
	}
395
	
396
	public void uploadWithServlet(JSONArray param) throws Exception {
397
		JSONArray filePaths = param.getJSONArray(0);
398
		String dataAction = param.getString(1);
399
		IData dataParam = isNull(param.getString(2))?new DataMap():new DataMap(param.getString(2));
400
		uploadWithServlet(filePaths, dataAction, dataParam);
401
	}
402
	
403
	@SuppressWarnings("unchecked")
404
	public void uploadWithServlet(final JSONArray filePaths, final String dataAction, IData dataParam) throws Exception {
405
		// TODO Auto-generated method stub
406
		Map<String, String> postData = transPostData(dataAction, dataParam);
407
		new AsyncTask<Map<String, String>, Integer, String>() {
408
			@Override
409
			protected String doInBackground(Map<String, String>... args) {
410
				Map<String, String> postData = args[0];
411
				Map<String, Object> fileData = new HashMap<String, Object>();
412
				// TODO Auto-generated method stub
413
				String result = null;
414
				try{
415
					String filePath = null;
416
					File file;
417
					for(int i=0;i<filePaths.length();i++){
418
						filePath = filePaths.getString(i);
419
						file = new File(filePath);
420
						if(!file.exists()){
421
							IpuMobileUtility.error(Messages.FILE_NOT_EXIST+":"+filePath);
422
						}
423
						fileData.put("UPLOAD_FILE"+i, file);//装载文件
424
					}
425
					fileData.put("UPLOAD_FILE_COUNT", filePaths.length());//装载文件数量
426
					
427
					String encode = MobileConfig.getInstance().getEncode();
428
					String dataUrl = HttpTool.urlEncode(HttpTool.toQueryString(postData), encode);
429
					String url = MobileConfig.getInstance().getRequestUrl() + "?" + dataUrl;
430
					result = UnirestUtil.uploadByPost(HttpTool.urlEscape(url).toString(), fileData);
431
					if (ServerDataConfig.isEncrypt(dataAction)) {
432
						result = MobileSecurity.responseDecrypt(result);
433
					}
434
				}catch(Exception e){
435
					MobileNetWork.this.error(e.getMessage());// 报错回调
436
					IpuMobileUtility.error(e);
437
				}
438
				return result;
439
			}
440
			
441
			@Override
442
			protected void onPostExecute(String result) {
443
				// TODO Auto-generated method stub
444
				super.onPostExecute(result);
445
				if (result != null) {
446
					MobileNetWork.this.callback(result, true);// 正常回调
447
				}
448
			}
449
		}.execute(postData);
450
	}
451
	
452
	public void downloadWithServlet(JSONArray param) throws Exception {
453
		String savePath = param.getString(0);
454
		String dataAction = param.getString(1);
455
		IData dataParam = param.getString(2) == null ? new DataMap() : new DataMap(param.getString(2));
456
		downloadWithServlet(savePath, dataAction, dataParam);
457
	}
458
	
459
	public void downloadWithServlet(final String _savePath, String dataAction, IData dataParam) throws Exception {
460
		// TODO Auto-generated method stub
461
		Map<String, String> tempPostData = transPostData(dataAction, dataParam);
462
		final Map<String, Object> postData = new HashMap<String, Object>();
463
		postData.putAll(tempPostData);
464
		new AsyncTask<String, Integer, String>() {
465
			@Override
466
			protected String doInBackground(String... arg0) {
467
				// TODO Auto-generated method stub
468
				String savePath = _savePath;
469
				try {
470
					InputStream in = UnirestUtil.downloadByPost(MobileConfig.getInstance().getRequestUrl(), postData);
471
					if (!savePath.startsWith(File.separator)) {
472
						savePath = directionUtil.getDirection(savePath, true);
473
					}
474
					File file = new File(savePath).getParentFile();
475
					if (!file.exists() && !file.mkdirs()) {
476
						IpuMobileUtility.error("创建下载文件夹失败!");
477
					}
478
479
					FileUtil.writeFile(in, savePath);
480
				} catch (Exception e) {
481
					MobileNetWork.this.error("[" + savePath + "]异常:" + e.getMessage());// 报错回调
482
				}
483
				return savePath;
484
			}
485
			
486
			@Override
487
			protected void onPostExecute(String savePath) {
488
				// TODO Auto-generated method stub
489
				super.onPostExecute(savePath);
490
				if (savePath != null) {
491
					MobileNetWork.this.callback(savePath, true);// 正常回调
492
				}
493
			}
494
		}.execute();
495
	}
496
497
	public void uploadFile(JSONArray param) throws Exception {
498
		String path = param.getString(0);
499
		uploadFile(path);
500
	}
501
502
	public void uploadFile(String path) throws Exception {
503
		// TODO Auto-generated method stub
504
		
505
	}
506
}