第一个对象:
该对象是调用WebService用的一个对象基类。
package Entity;
import com.google.gson.Gson;
import org.json.JSONObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Created by 王彦鹏 on 2017-09-01.
*/
public class BaseEnity {
public String toString() {
Gson gson=new Gson();
return gson.toJson(this);
}
public void LoadJson(String jsonStr)
{
try {
JSONObject json = new JSONObject(jsonStr);
Field[] field = this.getClass().getDeclaredFields(); // 获取实体类的所有属性,返回Field数组
for (int j = 0; j < field.length; j++) { // 遍历所有属性
String name = field[j].getName(); // 获取属性的名字
name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写,方便构造get,set方法
// // 获取属性的类型
try {
if (!name.equals("$change") && !name.equals("SerialVersionUID")){
Method m = this.getClass().getMethod("set" + name, field[j].getType());
String type = field[j].getType().getName();// getGenericType().toString();
switch (type) {
case "byte": {
m.invoke(this, (byte)json.getInt(field[j].getName()));
break;
}
case "int":
{
m.invoke(this, json.getInt(field[j].getName()));
break;
}
case "char":
{
m.invoke(this, json.getString(field[j].getName()).charAt(0));
break;
}
case "String":
{
m.invoke(this, json.getString(field[j].getName()));
break;
}
case "boolean":
{
m.invoke(this, json.getBoolean(field[j].getName()));
break;
}
case "double":
{
m.invoke(this, json.getDouble(field[j].getName()));
break;
}
case "long":
{
m.invoke(this, json.getLong(field[j].getName()));
break;
}
default: {
m.invoke(this, json.get(field[j].getName()));
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public Object getPropertyValue(String PName) throws Exception
{
Field[] fields = this.getClass().getDeclaredFields(); // 获取实体类的所有属性,返回Field数组
for (Field field : fields) {
if (field.getName().equalsIgnoreCase(PName))
{
String name = field.getName(); // 获取属性的名字
name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写,方便构造get,set方法
Method m = this.getClass().getMethod("get" + name);
return m.invoke(this);
}
}
return null;
}
}
第二个对象:
该对象是Http通讯对象的接口封装。
package Interface;
import org.json.JSONObject;
import Entity.BaseEnity;
/**
* Created by 王彦鹏 on 2017/9/28.
*/
public interface IHttp extends IInterface {
String Get(String url) throws Exception;
String Post(String url, String postData) throws Exception;
void Post(String url, String postData,IAsynCallBackListener callback) throws Exception;
String Post(String url, String postData,IAsynCallBackListener callback,String contentType) throws Exception;
JSONObject CallWebService(String url, String IName, BaseEnity... entity) throws Exception;
String Get(String url,IAsynCallBackListener callback) throws Exception;
void CallWebService(String url, String IName,IAsynCallBackListener callback, BaseEnity... entity) throws Exception;
}
第三个对象是IHttp接口的实现,是Http通讯的核心部分:
package Helper;
import android.accounts.NetworkErrorException;
import android.support.annotation.NonNull;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import Annotation.*;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeoutException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
//import Const.PublicDefine;
import Entity.BaseEnity;
import Interface.IAsynCallBackListener;
import Interface.IHttp;
import Helper.Log;
/**
* Created by 王彦鹏 on 2017-09-02.
*/
class HttpAsynResponseData
{
private String response="";
private Throwable exception;
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
public Throwable getException() {
return exception;
}
public void setException(Throwable exception) {
this.exception = exception;
}
}
public class Http implements IHttp {
/**
* 用Get方式请求服务器
*
* @param url url 地址
* @param contentType 请求数据类型
* @return 服务器返回的数据
*/
@NonNull
private String httpMethod(String url, String contentType, String method, String postData) throws Exception {
HttpURLConnection conn = null;
try {
long t1,t2=0;
t1=System.currentTimeMillis();
URL murl = new URL(url);
//1.得到HttpURLConnection实例化对象
conn = (HttpURLConnection) murl.openConnection();
//2.设置请求信息(请求方式... ...)
//设置请求方式和响应时间
conn.setRequestMethod(method);
conn.setRequestProperty("Content-Type", contentType);
conn.setRequestProperty("Content-Length", String.valueOf(postData.getBytes().length));
conn.setRequestProperty("encoding", "UTF-8"); //可以指定编码
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
//不使用缓存
conn.setUseCaches(false);
// 设置可取
conn.setDoInput(true);
// 设置可读
conn.setDoOutput(true);
if (method.toUpperCase() == "POST") {
//4.向服务器写入数据
conn.getOutputStream().write(postData.getBytes());
}
//3.读取响应
if (conn.getResponseCode() == 200) {
InputStream in = conn.getInputStream();
// 创建高效流对象
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
// 创建StringBuilder对象存储数据
StringBuilder response = new StringBuilder();
String line;// 一次读取一行
while ((line = reader.readLine()) != null) {
response.append(line);// 得到的数据存入StringBuilder
}
t2= System.currentTimeMillis();
Log.write("Http",String.format("%s 请求(%s)\r\nUrl:%s\r\n请求数据:%s\r\n返回数据:%s\r\n请求用时:%3f",
method,contentType,url,postData, response.toString(),(t2-t1)/Double.valueOf(1000)));
return response.toString();
} else {
Log.write("Http",String.format("%s 请求(%s):%s\r\n请求失败。",method,contentType,url));
throw new Exception("请求失败");
}
} catch (TimeoutException e)
{
Log.write("Http",String.format("%s 请求(%s):%s\r\n连接超时。",method,contentType,url));
throw new Exception("请求超时或网络已断开。");
}catch (SocketTimeoutException e)
{
throw new Exception("请求超时或网络已断开。");
}
catch (NetworkErrorException e)
{
throw new Exception("网络断开");
}
catch (Exception e) {
Log.write("Http",String.format("%s 请求(%s):%s\r\n请求异常:%s",method,contentType,url,e.getMessage()));
e.printStackTrace();
if(e.getMessage().indexOf("Network is unreachable")>=0)
{
throw new Exception("网络断开");
}
else {
throw e;
}
} finally {
//4.释放资源
if (conn != null) {
//关闭连接 即设置 http.keepAlive = false;
conn.disconnect();
}
}
}
/**
* 用Get方式请求服务器
*
* @param url url 地址
* @param callback 异步回调地址,当不为null时为异步调用
* @param contentType 请求数据类型
* @return 服务器返回的数据
*/
public String Get(final String url,
final String contentType,
final IAsynCallBackListener callback) throws Exception {
if (callback == null) {
return httpMethod(url, contentType, "GET", "");
} else {
new Thread(new Runnable() {
@Override
public void run() {
try {
String httpResponse = httpMethod(url, contentType, "GET", "");
// 回调onFinish()方法
callback.onFinish("",httpResponse);
} catch (Exception e) {
// 回调onError()方法
callback.onError(this,e);
}
}
}).start();
return "";
}
}
/**
* 用Get方式请求服务器
*
* @param url 网络地址
* @return
*/
public String Get(final String url) throws Exception {
final Object synObj = new Object();
final HttpAsynResponseData res = new HttpAsynResponseData();
Thread getthread = new Thread(new Runnable() {
@Override
public void run() {
synchronized (synObj) {
try {
res.setResponse(Get(url, "application/x-www-form-urlencoded", null));
res.setException(null);
} catch (Exception e) {
e.printStackTrace();
res.setException(e);
}
synObj.notify();
}
}
});
getthread.start();
try {
//Thread.sleep(1);
synchronized (synObj) {
synObj.wait();
if (res.getException()==null) {
return res.getResponse();
}
else
{
throw new Exception(res.getException());
}
}
//return res[0];
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* 用 Post 方式请求服务器
*
* @param url 网络地址
* @param postData 提交数据
* @param callback 回调地址
* @param contentType 请求数据类型
* @return 服务器返回数据
*/
public String Post(final String url,
final String postData,
final IAsynCallBackListener callback,
final String contentType) throws Exception {
if (callback == null) {
return httpMethod(url, contentType, "POST", postData);
} else {
new Thread(new Runnable() {
@Override
public void run() {
try {
String httpResponse = httpMethod(url, contentType, "POST", postData);
// 回调onFinish()方法
callback.onFinish("",httpResponse);
} catch (Exception e) {
// 回调onError()方法
callback.onError(this,e);
}
}
}).start();
return "";
}
}
/**
* 用 Post 方式请求服务器
*
* @param url 网络地址
* @param postData 提交数据
* @return
*/
public String Post(final String url,
final String postData) throws Exception {
final Object synObj = new Object();
final String[] res = {""};
Thread getthread = new Thread(new Runnable() {
@Override
public void run() {
synchronized (synObj) {
try {
res[0] = Post(url, postData,null,"application/x-www-form-urlencoded");
} catch (Exception e) {
e.printStackTrace();
res[0]=e.getMessage();
}
synObj.notify();
}
}
});
getthread.start();
try {
//Thread.sleep(1);
synchronized (synObj) {
synObj.wait();
return res[0];
}
//return res[0];
} catch (Exception e) {
throw new Exception(res[0]);
}
}
@Override
public void Post(String url, String postData, IAsynCallBackListener callback) throws Exception {
Post(url, postData,callback,"application/x-www-form-urlencoded");
}
/**
* 用 Post 方式请求服务器
*
* @param url 网络地址
* @param postData 提交数据
* @return
*/
public String Post(final String url,
final String postData,
final String contentType) throws Exception {
if (!PublicDefine.NetworkConnState)
{
throw new Exception("网络连接已断开,请先检查网络。");
}
final Object synObj = new Object();
final String[] res = {""};
Thread getthread = new Thread(new Runnable() {
@Override
public void run() {
synchronized (synObj) {
try {
res[0] = Post(url, postData,null,contentType);
} catch (Exception e) {
e.printStackTrace();
res[0]=e.getMessage();
}
synObj.notify();
}
}
});
getthread.start();
try {
//Thread.sleep(1);
synchronized (synObj) {
synObj.wait();
return res[0];
}
//return res[0];
} catch (Exception e) {
return res[0];
}
}
private static JSONObject LoadNodeChild(Node node) throws JSONException {
JSONObject json = new JSONObject();
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
Node subnode=node.getChildNodes().item(i);
String key =subnode.getNodeName();
Object value = null;
if (subnode.hasChildNodes()&& (!subnode.getChildNodes().item(0).getNodeName().equals("#text"))) {
value = LoadNodeChild(subnode);
} else {
value = subnode.getTextContent();
}
if (json.has(key)){
Object oldJson = json.get(key);
if (oldJson instanceof JSONObject){
JSONArray jsonArray = new JSONArray();
jsonArray.put(oldJson);
jsonArray.put(value);
json.remove(key);
json.put(key, jsonArray);
}else{
((JSONArray)oldJson).put(value);
json.put(key,oldJson);
}
}else{
json.put(key, value);
}
}
return json;
}
private static String GetPropertyName(Field f)
{
PropertyName pname = f.getAnnotation(PropertyName.class);
if (pname==null)
{
return f.getName();
}
else
{
return pname.value();
}
}
private static String GetKeyValue(BaseEnity enity) throws Exception {
String values = "";
Field[] field = enity.getClass().getDeclaredFields(); // 获取实体类的所有属性,返回Field数组
for (int j = 0; j < field.length; j++) { // 遍历所有属性
String name = field[j].getName(); // 获取属性的名字
name = name.substring(0, 1).toUpperCase() + name.substring(1); // 将属性的首字符大写,方便构造get,set方法
try {
Method m = enity.getClass().getMethod("get" + name);
Object instance = m.invoke(enity);
if (instance.getClass().getSimpleName().equals("ArrayList")){
for (int i = 0; i < ((List)instance).size(); i++) {
if (((List) instance).get(i).getClass().getSuperclass().getSimpleName().equals("BaseEnity")) {
values += "<" + GetPropertyName(field[j]) + ">" + GetKeyValue((BaseEnity) ((List) instance).get(i)) + "</" + GetPropertyName(field[j]) + ">\n";
} else {
values += "<" + GetPropertyName(field[j]) + ">" + ((List) instance).get(i).toString() + "</" + GetPropertyName(field[j]) + ">\n";
}
}
}else if (instance.getClass().getSuperclass().getSimpleName().equals("BaseEnity")) {
values += "<" + GetPropertyName(field[j]) + ">" + GetKeyValue((BaseEnity) instance) + "</" + GetPropertyName(field[j]) + ">\n";
} else {
values += "<" + GetPropertyName(field[j]) + ">" + instance.toString() + "</" + GetPropertyName(field[j]) + ">\n";
}
}
catch (Exception e)
{
}
}
return values;
}
/**
* 调用WebService接口
*
* @param IName
* @param args
* @return
*/
public JSONObject CallWebService(String url, String IName, BaseEnity... args)
throws Exception {
String pdata = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
/*"<soap:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\""+*/
" <soap:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" >\n" +
"<%s xmlns=\"http://tempuri.org/\">\n";
if (args!=null) {
for (BaseEnity entity:args ) {
pdata+=GetKeyValue(entity);
}
}
pdata += "</%s>\n" +
" </soap:Body>\n" +
"</soap:Envelope>";
pdata = String.format(pdata, IName, IName);
String resxml = Post(url, pdata,"text/xml");
DocumentBuilderFactory xml = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = xml.newDocumentBuilder();
Document dom = builder.parse(new InputSource(new ByteArrayInputStream(resxml.getBytes("utf-8"))));
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName(IName + "Response");//查找所有person节点
Node response = items.item(0);
JSONObject resJson = LoadNodeChild(response);
return resJson;
}
@Override
public String Get(String url, IAsynCallBackListener callback) throws Exception {
return Get(url,"application/x-www-form-urlencoded",callback);
}
@Override
public void CallWebService(String url, final String IName, final IAsynCallBackListener callback, BaseEnity... args) throws Exception {
String pdata = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
/*"<soap:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\""+*/
" <soap:Body xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" >\n" +
"<%s xmlns=\"http://tempuri.org/\">\n";
if (args!=null) {
for (BaseEnity entity:args ) {
pdata+=GetKeyValue(entity);
}
}
pdata += "</%s>\n" +
" </soap:Body>\n" +
"</soap:Envelope>";
pdata = String.format(pdata, IName, IName);
Post(url, pdata, new IAsynCallBackListener() {
@Override
public void onFinish(Object sender,Object data) {
try {
String resxml = data.toString();
DocumentBuilderFactory xml = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = xml.newDocumentBuilder();
Document dom = builder.parse(new InputSource(new ByteArrayInputStream(resxml.getBytes("utf-8"))));
Element root = dom.getDocumentElement();
NodeList items = root.getElementsByTagName(IName + "Response");//查找所有person节点
Node response = items.item(0);
JSONObject resJson = LoadNodeChild(response);
callback.onFinish("",resJson);
}
catch (Exception e)
{
}
}
@Override
public void onError(Object sender, Exception e) {
callback.onError(this,e);
}
}, "text/xml");
}
}