Blame view

src/com/ectrip/trips/net/HttpHelperCore.java 2.02 KB
3c2353cd   杜方   1、畅游通核销app源码提交;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  package com.ectrip.trips.net;
  
  import java.lang.reflect.Array;
  import java.util.Collection;
  import java.util.HashMap;
  import java.util.Map;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  
  import com.ectrip.cyt.callback.HttpCallback;
  
  /**
   * @author jigo 网络访问核心类
   */
  public class HttpHelperCore {
  	// 引入线程池来管理多线程
  	public ExecutorService executorService = Executors.newFixedThreadPool(3);
  
  	/**
  	 * Post 网络请求
  	 *
  	 * @param <T1>
  	 * @param <T2>
  	 *
  	 * @param url
  	 *            域名
  	 * @param method
  	 *            方法
  	 * @param errorData
  	 *            这个参数是判断fastJson 解析的数据是否正确,如果数据为空的情况下,返回失败
  	 * @param map
  	 *            参数
  	 */
  	public <T> void executeHttpPost(String url,String method,
  									HashMap<String, String> map,HttpCallback<T> resultListener,
  									String errorData,Class<T> cls) {
  		// 用于子线程与主线程通信的Handler
  		final HttpPostHandler<T> mHandler = new HttpPostHandler<T>(
  				resultListener);
  
  		executorService.submit(new HttpPostRunnable<T>(resultListener, url,
  				method, map, mHandler,errorData,cls));
  	}
  
  	/**
  	 * 判断对象或对象数组中每一个对象是否为空: 对象为null,字符序列长度为0,集合类、Mapempty
  	 *
  	 * @param obj
  	 * @return
  	 */
  	public static boolean isNullOrEmpty(Object obj) {
  		if (obj == null)
  			return true;
  		if (obj instanceof CharSequence)
  			return ((CharSequence) obj).length() == 0;
  		if (obj instanceof Collection)
  			return ((Collection) obj).isEmpty();
  		if (obj instanceof Map)
  			return ((Map) obj).isEmpty();
  		if (obj.getClass().isArray()) {
  			return Array.getLength(obj) == 0;
  		}
  		if (obj instanceof Object[]) {
  			Object[] object = (Object[]) obj;
  			if (object.length == 0) {
  				return true;
  			}
  			boolean empty = true;
  			for (int i = 0; i < object.length; i++) {
  				if (!isNullOrEmpty(object[i])) {
  					empty = false;
  					break;
  				}
  			}
  			return empty;
  		}
  		return false;
  	}
  }