import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
public class WebService extends Thread implements IWebService {
public class MessageType {
public final static int ErrorMessage = 0;
public final static int NormalMessage = 1;
}
// 调用方式
public enum ExecType {
// 同步模式
Sync,
// 异步模式
Async
}
// 客户端类型
public enum ConnectType {
Online, // 在线模式
Offline// 离线模式,读取本地内容
}
// 返回值结果
public enum ResultType {
Int, Double, Float, Boolean, String, Class, ClassList
}
// 是否记录下 ,读取的内容
public Boolean IsWriteData = false;
// 记录内容的路径
public String WriteDataPath = "";
// 连接模式
public ConnectType ConType = ConnectType.Online;
// 离线数据内容路径
public String OfflineDataPath = "";
// 进度对话框
ProgressDialog mProgressDialog = null;
// 当前界面 ,只是在异步的情况下使用
Activity mActivity = null;
// 异步调用的线程
// Thread mThread;
Handler mHandler;
String mUrl;
String mNameSpace;
String mMethodName;
String mMessageName;
String mEntityName;
HashMap<String, Object> mParams;
Boolean mIsList;// 是否是数组
ResultType mResultType = ResultType.Class;// 结果类别
// 异步调用
/*
* @url ws地址
*
* @namespace 命名空间
*
* @methodname 方法名称
*
* @params 参数
*
* @activity 界面
*
* @handler 异步调用接受对象
*/
public void ExecAsync(String url, String nameSpace, String methodName,
HashMap<String, Object> params, String messageName,
String entityFullName, Activity activity, Handler handler) {
ExecAsync(url, nameSpace, methodName, params, messageName,
entityFullName, activity, handler, false);
}
// 异步调用
/*
* @url ws地址
*
* @namespace 命名空间
*
* @methodname 方法名称
*
* @params 参数
*
* @activity 界面
*
* @handler 异步调用接受对象
*/
public void ExecAsync(String url, String nameSpace, String methodName,
HashMap<String, Object> params, String messageName,
String entityFullName, Activity activity, Handler handler,
Boolean isList) {
ResultType resultType = ResultType.Class;
if (isList) {
resultType = ResultType.ClassList;
}
this.ExecAsync(url, nameSpace, methodName, params, messageName,
entityFullName, activity, handler, resultType);
}
public void ExecAsync(String url, String nameSpace, String methodName,
HashMap<String, Object> params, String messageName,
String entityFullName, Activity activity, Handler handler,
ResultType resultType) {
mUrl = url;
mActivity = activity;
mHandler = handler;
mNameSpace = nameSpace;
mMethodName = methodName;
mParams = params;
mActivity = activity;
mHandler = handler;
mMessageName = messageName;
mEntityName = entityFullName;
this.mResultType = resultType;
ShowProgress();
this.start();
}
@Override
public void run() {
super.run();
try {
// web service请求
Object result = Exec(mUrl, mNameSpace, mMethodName, mParams);
DismissProgress();
// 构造消息
Message message = this.mHandler.obtainMessage();
message.setData(CreateBundle(result));
message.what = MessageType.NormalMessage;
mHandler.sendMessage(message);
} catch (Exception ex) {
DismissProgress();
Message message = mHandler.obtainMessage();
Bundle b = new Bundle();
message.what = MessageType.ErrorMessage;
//String exStr = ex.toString() + ":" + ex.getMessage();
b.putSerializable("ErrorMessage", ex);
message.setData(b);
mHandler.sendMessage(message);
}
}
private Bundle CreateBundle(Object result) {
Bundle b = new Bundle();
if (this.mResultType == ResultType.ClassList)
b.putSerializable(mMessageName,
ToSerializableList((SoapObject) result, mEntityName));
else if (this.mResultType == ResultType.Class) {
b.putSerializable(mMessageName,
ToSerializable((SoapObject) result, mEntityName));
} else if (this.mResultType == ResultType.Double) {
b.putDouble(mEntityName, Double.parseDouble(result.toString()));
} else if (this.mResultType == ResultType.Boolean) {
b.putBoolean(mEntityName, Boolean.parseBoolean(result.toString()));
} else if (this.mResultType == ResultType.Int) {
b.putInt(mEntityName, Integer.parseInt(result.toString()));
} else if (this.mResultType == ResultType.Float) {
b.putFloat(this.mEntityName, Float.parseFloat(result.toString()));
} else if (this.mResultType == ResultType.String) {
b.putString(this.mEntityName, result.toString());
}
return b;
}
/*
* 显示进度条
*/
private void ShowProgress() {
mProgressDialog = new ProgressDialog(mActivity);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setTitle("提示");
mProgressDialog.setMessage("读取数据中...");
mProgressDialog.setButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mProgressDialog.dismiss();
}
});
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
/*
* 隐藏进度
*/
private void DismissProgress() {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
}
// 同步调用
/*
* @url ws地址
*
* @namespace 命名空间
*
* @methodname 方法名称
*
* @params 参数
*/
public Object Exec(String url, String nameSpace, String methodName,
HashMap<String, Object> params) throws Exception {
mNameSpace = nameSpace;
mMethodName = methodName;
mParams = params;
mUrl = url;
String SOAP_ACTION = nameSpace + methodName;
// 创建SoapObject实例
SoapObject request = new SoapObject(nameSpace, methodName); // 指定WebService的命名空间和调用方法
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11); // 生成调用WebService方法调用的soap信息,并且指定Soap版本
// 设置.net web service
envelope.dotNet = true; // 是否调用DotNet开发的WebService
// 发送请求
envelope.setOutputSoapObject(request);
// 请求参数
if (params != null && !params.isEmpty()) {
for (Iterator<Entry<String, Object>> it = params.entrySet()
.iterator(); it.hasNext();) {
Map.Entry e = (Entry) it.next();
request.addProperty(e.getKey().toString(), e.getValue()); // 设置需要调用WebService接口的参数
}
}
//
HttpTransportSE androidHttpTrandsport = new HttpTransportSE(url,60000);
Object result = null;
try {
// 得到返回结果
// 判断是否是离线模式,离线模式 直接从SD卡读取
if (ConType == ConnectType.Offline) {
result = WebServiceData.GetResult(mUrl, SOAP_ACTION, mParams,
this.OfflineDataPath);
} else {
androidHttpTrandsport.call(SOAP_ACTION, envelope);
result = envelope.getResponse();
}
// 是否写入数据
if (this.IsWriteData) {
WebServiceData.SaveResult(mUrl, SOAP_ACTION, mParams, result,
this.WriteDataPath);
}
} catch (Exception e) {
throw e;
}
return result;
}
private Serializable ToSerializableList(SoapObject object, String entityName) {
try {
// 通过放射来生成类
// 这里规定,这些类必需包含无参数的构造函数
// 这些类也必需实现IEntity
ArrayList<Serializable> values = new ArrayList<Serializable>();
for (int i = 0; i < object.getPropertyCount(); i++) {
values.add(ToSerializable((SoapObject) object.getProperty(i),
entityName));
}
return values;
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
// entityName 需要转换成的实体类名
private Serializable ToSerializable(SoapObject object, String entityName) {
try {
// 通过放射来生成类
// 这里规定,这些类必需包含无参数的构造函数
// 这些类也必需实现IEntity
Class<?> c = Class.forName(entityName);
Object object2 = c.newInstance();
IEntity entity = (IEntity) object2;
return entity.ToSerializable(object);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.getMessage());
} catch (IllegalArgumentException e) {
throw new RuntimeException(e.getMessage());
} catch (SecurityException e) {
throw new RuntimeException(e.getMessage());
} catch (InstantiationException e) {
throw new RuntimeException(e.getMessage());
} catch (IllegalAccessException e) {
throw new RuntimeException(e.getMessage());
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
@Override
public void ExecAsync(String url, String nameSpace, String methodName,
HashMap<String, Object> params, String messageName,
String entityFullName, Context context,View view,Handler handler) {
// TODO Auto-generated method stub
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
public class WebService extends Thread implements IWebService {
public class MessageType {
public final static int ErrorMessage = 0;
public final static int NormalMessage = 1;
}
// 调用方式
public enum ExecType {
// 同步模式
Sync,
// 异步模式
Async
}
// 客户端类型
public enum ConnectType {
Online, // 在线模式
Offline// 离线模式,读取本地内容
}
// 返回值结果
public enum ResultType {
Int, Double, Float, Boolean, String, Class, ClassList
}
// 是否记录下 ,读取的内容
public Boolean IsWriteData = false;
// 记录内容的路径
public String WriteDataPath = "";
// 连接模式
public ConnectType ConType = ConnectType.Online;
// 离线数据内容路径
public String OfflineDataPath = "";
// 进度对话框
ProgressDialog mProgressDialog = null;
// 当前界面 ,只是在异步的情况下使用
Activity mActivity = null;
// 异步调用的线程
// Thread mThread;
Handler mHandler;
String mUrl;
String mNameSpace;
String mMethodName;
String mMessageName;
String mEntityName;
HashMap<String, Object> mParams;
Boolean mIsList;// 是否是数组
ResultType mResultType = ResultType.Class;// 结果类别
// 异步调用
/*
* @url ws地址
*
* @namespace 命名空间
*
* @methodname 方法名称
*
* @params 参数
*
* @activity 界面
*
* @handler 异步调用接受对象
*/
public void ExecAsync(String url, String nameSpace, String methodName,
HashMap<String, Object> params, String messageName,
String entityFullName, Activity activity, Handler handler) {
ExecAsync(url, nameSpace, methodName, params, messageName,
entityFullName, activity, handler, false);
}
// 异步调用
/*
* @url ws地址
*
* @namespace 命名空间
*
* @methodname 方法名称
*
* @params 参数
*
* @activity 界面
*
* @handler 异步调用接受对象
*/
public void ExecAsync(String url, String nameSpace, String methodName,
HashMap<String, Object> params, String messageName,
String entityFullName, Activity activity, Handler handler,
Boolean isList) {
ResultType resultType = ResultType.Class;
if (isList) {
resultType = ResultType.ClassList;
}
this.ExecAsync(url, nameSpace, methodName, params, messageName,
entityFullName, activity, handler, resultType);
}
public void ExecAsync(String url, String nameSpace, String methodName,
HashMap<String, Object> params, String messageName,
String entityFullName, Activity activity, Handler handler,
ResultType resultType) {
mUrl = url;
mActivity = activity;
mHandler = handler;
mNameSpace = nameSpace;
mMethodName = methodName;
mParams = params;
mActivity = activity;
mHandler = handler;
mMessageName = messageName;
mEntityName = entityFullName;
this.mResultType = resultType;
ShowProgress();
this.start();
}
@Override
public void run() {
super.run();
try {
// web service请求
Object result = Exec(mUrl, mNameSpace, mMethodName, mParams);
DismissProgress();
// 构造消息
Message message = this.mHandler.obtainMessage();
message.setData(CreateBundle(result));
message.what = MessageType.NormalMessage;
mHandler.sendMessage(message);
} catch (Exception ex) {
DismissProgress();
Message message = mHandler.obtainMessage();
Bundle b = new Bundle();
message.what = MessageType.ErrorMessage;
//String exStr = ex.toString() + ":" + ex.getMessage();
b.putSerializable("ErrorMessage", ex);
message.setData(b);
mHandler.sendMessage(message);
}
}
private Bundle CreateBundle(Object result) {
Bundle b = new Bundle();
if (this.mResultType == ResultType.ClassList)
b.putSerializable(mMessageName,
ToSerializableList((SoapObject) result, mEntityName));
else if (this.mResultType == ResultType.Class) {
b.putSerializable(mMessageName,
ToSerializable((SoapObject) result, mEntityName));
} else if (this.mResultType == ResultType.Double) {
b.putDouble(mEntityName, Double.parseDouble(result.toString()));
} else if (this.mResultType == ResultType.Boolean) {
b.putBoolean(mEntityName, Boolean.parseBoolean(result.toString()));
} else if (this.mResultType == ResultType.Int) {
b.putInt(mEntityName, Integer.parseInt(result.toString()));
} else if (this.mResultType == ResultType.Float) {
b.putFloat(this.mEntityName, Float.parseFloat(result.toString()));
} else if (this.mResultType == ResultType.String) {
b.putString(this.mEntityName, result.toString());
}
return b;
}
/*
* 显示进度条
*/
private void ShowProgress() {
mProgressDialog = new ProgressDialog(mActivity);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setTitle("提示");
mProgressDialog.setMessage("读取数据中...");
mProgressDialog.setButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mProgressDialog.dismiss();
}
});
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
/*
* 隐藏进度
*/
private void DismissProgress() {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
}
// 同步调用
/*
* @url ws地址
*
* @namespace 命名空间
*
* @methodname 方法名称
*
* @params 参数
*/
public Object Exec(String url, String nameSpace, String methodName,
HashMap<String, Object> params) throws Exception {
mNameSpace = nameSpace;
mMethodName = methodName;
mParams = params;
mUrl = url;
String SOAP_ACTION = nameSpace + methodName;
// 创建SoapObject实例
SoapObject request = new SoapObject(nameSpace, methodName); // 指定WebService的命名空间和调用方法
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11); // 生成调用WebService方法调用的soap信息,并且指定Soap版本
// 设置.net web service
envelope.dotNet = true; // 是否调用DotNet开发的WebService
// 发送请求
envelope.setOutputSoapObject(request);
// 请求参数
if (params != null && !params.isEmpty()) {
for (Iterator<Entry<String, Object>> it = params.entrySet()
.iterator(); it.hasNext();) {
Map.Entry e = (Entry) it.next();
request.addProperty(e.getKey().toString(), e.getValue()); // 设置需要调用WebService接口的参数
}
}
//
HttpTransportSE androidHttpTrandsport = new HttpTransportSE(url,60000);
Object result = null;
try {
// 得到返回结果
// 判断是否是离线模式,离线模式 直接从SD卡读取
if (ConType == ConnectType.Offline) {
result = WebServiceData.GetResult(mUrl, SOAP_ACTION, mParams,
this.OfflineDataPath);
} else {
androidHttpTrandsport.call(SOAP_ACTION, envelope);
result = envelope.getResponse();
}
// 是否写入数据
if (this.IsWriteData) {
WebServiceData.SaveResult(mUrl, SOAP_ACTION, mParams, result,
this.WriteDataPath);
}
} catch (Exception e) {
throw e;
}
return result;
}
private Serializable ToSerializableList(SoapObject object, String entityName) {
try {
// 通过放射来生成类
// 这里规定,这些类必需包含无参数的构造函数
// 这些类也必需实现IEntity
ArrayList<Serializable> values = new ArrayList<Serializable>();
for (int i = 0; i < object.getPropertyCount(); i++) {
values.add(ToSerializable((SoapObject) object.getProperty(i),
entityName));
}
return values;
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
// entityName 需要转换成的实体类名
private Serializable ToSerializable(SoapObject object, String entityName) {
try {
// 通过放射来生成类
// 这里规定,这些类必需包含无参数的构造函数
// 这些类也必需实现IEntity
Class<?> c = Class.forName(entityName);
Object object2 = c.newInstance();
IEntity entity = (IEntity) object2;
return entity.ToSerializable(object);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.getMessage());
} catch (IllegalArgumentException e) {
throw new RuntimeException(e.getMessage());
} catch (SecurityException e) {
throw new RuntimeException(e.getMessage());
} catch (InstantiationException e) {
throw new RuntimeException(e.getMessage());
} catch (IllegalAccessException e) {
throw new RuntimeException(e.getMessage());
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
@Override
public void ExecAsync(String url, String nameSpace, String methodName,
HashMap<String, Object> params, String messageName,
String entityFullName, Context context,View view,Handler handler) {
// TODO Auto-generated method stub
}
}