1、有泛型的类
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
public class HttpClientObject<T> {
private Logger logger = Logger.getLogger(HttpClientObject.class);
private String url;
private T t;
public HttpClientObject() {
}
public void ts(Map<String,Integer> s){
}
/**
* HttpClient工具类
* @param url
* @param param:Map<String,String>
* @return
* @throws Exception
*/
public String post(String url,Map<String,String> param)throws Exception{
if(param.isEmpty() || StringUtils.isBlank(url))
throw new Exception("请求参数不合法");
Iterator<String> it = param.keySet().iterator();
List<NameValuePair> list = new ArrayList<NameValuePair>();
NameValuePair nv = null;
String key = "";
while(it.hasNext()){
key = it.next();
nv = new BasicNameValuePair(key,(String)param.get(key));
list.add(nv);
}
HttpClientObject<NameValuePair> client = new HttpClientObject<NameValuePair>();
client.setUrl(url);
String rsjson = client.doPost(list);
if(StringUtils.isBlank(rsjson))
throw new Exception("请求接口无结果 ,url = "+url);
return rsjson;
}
public String doPost(List<NameValuePair> list) {
String result = null;
try {
HttpEntity requesthttpEntity = new UrlEncodedFormEntity(list, "UTF-8");
HttpPost httpPost = new HttpPost(this.url);
httpPost.setEntity(requesthttpEntity);
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter("http.connection.timeout", Integer.valueOf(20000));
httpClient.getParams().setParameter("http.socket.timeout", Integer.valueOf(20000));
this.logger.debug(httpPost.getURI());
StringBuffer sb = new StringBuffer();
for (NameValuePair p : list) {
sb.append("&" + p.getName() + "=" + p.getValue());
}
this.logger.debug("param=" + sb.toString());
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() != 200) {
throw new Exception("Failed : HTTP error code : " + httpResponse.getStatusLine().getStatusCode());
}
HttpEntity httpEntity = httpResponse.getEntity();
result = EntityUtils.toString(httpEntity);
}
catch (Exception e) {
logger.error("异常007"+e.getMessage());
}
return result;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @return the t
*/
public T getT() {
return t;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/**
* @param t the t to set
*/
public void setT(T t) {
this.t = t;
}
}
2、无法泛型的类
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xc.b2c.vo.Tree;
import com.xc.base.util.StringUtil;
public class CustomCommonUtil {
private static Log log = LogFactory.getLog(CustomCommonUtil.class);
public CustomCommonUtil() {
}
/**
* 保留Map中 keepKey对应数据,去掉其他的key-value
* @param mapList
* @param keepKey
*/
public static void filterMap(List<Map<String,String>> mapList,String ...keepKey ){
if(mapList != null){
List<String> keepKeyList = Arrays.asList(keepKey);
for(Map<String,String> map : mapList){
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
//如果不是要保留的key,则romove掉
if (!keepKeyList.contains(key)) {
iterator.remove();
map.remove(key);
}
}
}
}
}
public static void filterMap(String value,List<Map<String,String>> mapList,String ...keepKey ){
if(mapList != null){
List<String> keepKeyList = Arrays.asList(keepKey);
for(Map<String,String> map : mapList){
Iterator<String> iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
log.info("key is:"+key);
if(key.startsWith("imgUrl")){
map.put(key,value+map.get(key));
}
//如果不是要保留的key,则romove掉
if (!keepKeyList.contains(key)) {
iterator.remove();
map.remove(key);
}
}
}
}
}
/**
* 保留单个JSONObject中 keepKey对应数据,去掉其他的key-value
* @param jSONObject
* @param keepKey
*/
public static void filterJSONObject(JSONObject jSONObject,String ...keepKey ){
if(jSONObject != null){
List<String> keepKeyList = Arrays.asList(keepKey);
Iterator<String> iterator = jSONObject.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
//如果不是要保留的key,则romove掉
if (!keepKeyList.contains(key)) {
iterator.remove();
jSONObject.remove(key);
}
}
}
}
/**
* 深度 返回结果级过滤
* @param jSONObject
* @param keepNodes 不能为 null 否则会抛空指针异常
*/
public static void filterJSONObject(JSONObject jSONObject,List<Tree> keepNodes ){
//0、如果jSONObject 逻辑继续
if(jSONObject != null){
Iterator<String> iterator = jSONObject.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
//1、如果没有设置 保留的字段,当前的 jSONObject 数据全部不动
if(keepNodes==null) break;
//2、如果有配置 保留字段, 继续执行非保留字段去除工作
Tree subTree=TreeUtils.pickTreeByNodeName(keepNodes, key);
if (subTree==null) { //如果不是要保留的key,则romove掉
iterator.remove();
jSONObject.remove(key);
}else{
if(subTree.getNodeType()==ResultType.OBJECT){
Object subJsonObject = jSONObject.get(key); //是否JSON间传递的是引用,,,应该是引用后面的 原来的 filterJSONArray, 执行后得到的结果就是引用
if(subJsonObject instanceof JSONObject) //消除基本数据类型对处理的影响
CustomCommonUtil.filterJSONObject((JSONObject)subJsonObject, subTree.getSubTrees());
}else{
JSONArray subJsonArray = jSONObject.getJSONArray(key);
CustomCommonUtil.filterJSONArray(subJsonArray, subTree.getSubTrees());
}
}
}
}
}
/**
* 保留数组类型的中每个单个JSONObject中 keepKey对应数据,去掉其他的key-value
* @param jSONArray
* @param keepKey
*/
public static void filterJSONArray(JSONArray jSONArray,List<Tree> keepNodes ){
if(jSONArray != null){
for(int i=0; i<jSONArray.size(); i++){
JSONObject jSONObject = jSONArray.getJSONObject(i);
CustomCommonUtil.filterJSONObject(jSONObject, keepNodes);
}
}
}
/**
* 保留数组类型的中每个单个JSONObject中 keepKey对应数据,去掉其他的key-value
* @param jSONArray
* @param keepKey
*/
public static void filterJSONArray(JSONArray jSONArray,String ...keepKey ){
if(jSONArray != null){
for(int i=0; i<jSONArray.size(); i++){
JSONObject jSONObject = jSONArray.getJSONObject(i);
CustomCommonUtil.filterJSONObject(jSONObject, keepKey);
}
}
}
/***
* filter json string
*
* @param jsonStr
* @return
*/
public static JSONObject JsonFilter(JSONObject jsonStr, String... keepKey) {
JSONObject reagobj = jsonStr;
// 取出 jsonObject 中的字段的值的空格
Iterator itt = reagobj.keySet().iterator();
List<String> keepKeyList = Arrays.asList(keepKey);
while (itt.hasNext()) {
String key = itt.next().toString();
if (!keepKeyList.contains(key)) {
itt.remove();
reagobj.remove(key);
}
}
return reagobj;
}
/***
* 编码修改
* @param str
* @return
*/
public static String getChangeEncoded(String str) {
if (StringUtil.isEmpty(str)) {
return null;
} else {
try {
return new String(str.getBytes("iso-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
log.error("ChangeEncoded错误", e);
return null;
}
}
}
/**
* get the class name
* @param clazz
* @return
*/
public static String getClassName(Class clazz) {
if (clazz != null) {
return clazz.getName();
}
return null;
}
/***
* 获取LeftFront45Picture 方法
*
* @param jSONArray
* @param name
* @return
*/
public static String getLeftFront45Picture(JSONArray jSONArray, String name) {
String target = "";
if (jSONArray != null) {
for (int i = 0; i < jSONArray.size(); i++) {
String orders = jSONArray.getString(i);
if (!StringUtils.isBlank(orders)) {
int lastIndexOf = orders.lastIndexOf("/");
if (name.equals(orders.substring(lastIndexOf + 1,
orders.length()))) {
target = orders;
break;
}
}
}
}
return target;
}
}