修复http请求工具:ContentType为空的情况,修复多层xml解析问题

This commit is contained in:
egzosn@gmail.com
2017-06-23 17:10:17 +08:00
parent 84c3044061
commit d71e7dff12
2 changed files with 34 additions and 5 deletions

View File

@@ -134,8 +134,12 @@ public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase impleme
responseType = (Class<T>) String.class;
}
String[] value = entity.getContentType().getValue().split(";");
String[] value = null;
if (null == entity.getContentType()){
value = new String[]{"application/x-www-form-urlencoded"};
}else {
value = entity.getContentType().getValue().split(";");
}
if (ContentType.APPLICATION_OCTET_STREAM.getMimeType().equals(value[0])){
@@ -160,7 +164,6 @@ public class ClientHttpRequest<T> extends HttpEntityEnclosingRequestBase impleme
charset = value[1].substring(value[1].indexOf("=") + 1);
}
String result = EntityUtils.toString(entity, charset);
if (responseType.isAssignableFrom(String.class)){
return (T)result;
}

View File

@@ -85,12 +85,12 @@ public class XML {
while (it.hasNext()) {
Element e = (Element) it.next();
String k = e.getName();
String v = "";
Object v = "";
List children = e.getChildren();
if (children.isEmpty()) {
v = e.getTextNormalize();
} else {
v = getChildrenText(children);
v = getChildren(children);
}
m.put(k, v);
}
@@ -129,6 +129,32 @@ public class XML {
return sb.toString();
}
/**
* 获取子结点的xml
*
* @param children 集合
* @return String 子结点的xml
*/
public static Object getChildren(List children) {
JSONObject json = new JSONObject();
if (!children.isEmpty()) {
Iterator it = children.iterator();
while (it.hasNext()) {
Element e = (Element) it.next();
String name = e.getName();
String value = e.getTextNormalize();
List list = e.getChildren();
if (!list.isEmpty()) {
json.put(name, getChildren(list));
}else {
json.put(name, value);
}
}
}
return json;
}
/**
* 将请求参数转换为xml格式的string
* @param parameters 请求参数