android与服务端的服务架构,整体设计概念,模仿了ibatis的早期设计理念。
提供一个关于如何android解析webservice相应报文的解析工具,我大致概括了下,应该能够大致满足大家的需要,主要参考了ksoap2框架的api,进行再一次的封装。
package cc.ewell.mcs.portal.engine.parse;
import java.util.ArrayList;
import java.util.List;
import org.ksoap2.serialization.SoapObject;
public class KSoap2Utils {
private static Object property;
/**
* extract an int value from the soap response
*
* @param response
* @param id
* @return
*/
public static int getInt(SoapObject response, String id) {
String property = response.getProperty(id).toString();
return Integer.valueOf(property).intValue();
}
public static long getLong(SoapObject response, String id) {
String property = response.getProperty(id).toString();
return Long.valueOf(property);
}
public static short getShort(SoapObject response, String id) {
String property = response.getProperty(id).toString();
return Short.valueOf(property);
}
public static byte getByte(SoapObject response, String id) {
String property = response.getProperty(id).toString();
return Byte.valueOf(property);
}
/**
*
*
* @param response
* @param id
* @return
*/
public static float getFloat(SoapObject response, String id) {
String property = response.getProperty(id).toString();
return Float.valueOf(property);
}
public static double getDouble(SoapObject response, String id) {
String property = response.getProperty(id).toString();
return Double.valueOf(property);
}
/**
*
*
* @param response
* @param id
* @return
*/
public static boolean getBoolean(SoapObject response, String id) {
String property = response.getProperty(id).toString();
return Boolean.valueOf(property);
}
/**
*
*
* @param response
* @param id
* @return
*/
public static String getString(SoapObject response, String id) {
if (response.getProperty(id) != null)
return response.getProperty(id).toString();
else
return "null";
}
/**
*
*
* @param response
* @param theClass
* @return
*/
public static Soapeabilisable getObject(SoapObject response,
Soapeabilisable theClass) {
return theClass.fromSoapResponse(response);
}
/**
*
*
* @param response
* @param theClass
* @return
*/
public static List<Soapeabilisable> getArray(List<SoapObject> soapObjects,
Soapeabilisable theClass) {
List<Soapeabilisable> rets = new ArrayList<Soapeabilisable>(
soapObjects.size());
for (SoapObject record : soapObjects) {
rets.add(getObject(record, theClass));
}
return rets;
}
/**
*
*
* @param response
* @param theClass
* @return
*/
public static List<Soapeabilisable> getList(SoapObject response,
Soapeabilisable theClass) {
int recordsCount = response.getPropertyCount();
List<Soapeabilisable> rets = new ArrayList<Soapeabilisable>();
for (int i = 0; i < recordsCount; i++) {
SoapObject record = (SoapObject) response.getProperty(i);
rets.add(getObject(record, theClass));
}
return rets;
}
/**
*
*
* @param response
* @param theClass
* @return
*/
public static Soapeabilisable[] getArray(SoapObject response,
Soapeabilisable theClass) {
int recordsCount = response.getPropertyCount();
Soapeabilisable[] rets = new Soapeabilisable[recordsCount];
for (int i = 0; i < recordsCount; i++) {
SoapObject record = (SoapObject) response.getProperty(i);
rets[i] = getObject(record, theClass);
}
return rets;
}
/**
*
*
* @param response
*
* @return Object[]
*/
public static Object[] getObjectArray(SoapObject response) {
int recordsCount = response.getPropertyCount();
Object[] rets = new Object[recordsCount];
for (int i = 0; i < recordsCount; i++) {
Object record = response.getProperty(i);
rets[i] = record;
}
return rets;
}
public static String[] getStringArray(SoapObject response) {
int recordsCount = response.getPropertyCount();
String[] rets = new String[recordsCount];
for (int i = 0; i < recordsCount; i++)
rets[i] = response.getProperty(i).toString();
return rets;
}
public static boolean[] getBooleanArray(SoapObject response) {
int recordsCount = response.getPropertyCount();
boolean[] rets = new boolean[recordsCount];
for (int i = 0; i < recordsCount; i++)
rets[i] = ((Boolean) response.getProperty(i)).booleanValue();
return rets;
}
public static int[] getIntegerArray(SoapObject response) {
String property = null;
int recordsCount = response.getPropertyCount();
int[] rets = new int[recordsCount];
for (int i = 0; i < recordsCount; i++) {
property = response.getProperty(i).toString();
rets[i] = Integer.valueOf(property);
}
return rets;
}
public static long[] getLongArray(SoapObject response) {
String property = null;
int recordsCount = response.getPropertyCount();
long[] rets = new long[recordsCount];
for (int i = 0; i < recordsCount; i++) {
property = response.getProperty(i).toString();
rets[i] = Long.valueOf(property);
}
return rets;
}
public static short[] getShortArray(SoapObject response) {
String property = null;
int recordsCount = response.getPropertyCount();
short[] rets = new short[recordsCount];
for (int i = 0; i < recordsCount; i++) {
property = response.getProperty(i).toString();
rets[i] = Short.valueOf(property);
}
return rets;
}
public static byte[] getByteArray(SoapObject response) {
int recordsCount = response.getPropertyCount();
byte[] rets = new byte[recordsCount];
for (int i = 0; i < recordsCount; i++)
rets[i] = ((Byte) response.getProperty(i)).byteValue();
return rets;
}
public static float[] getFloatArray(SoapObject response) {
String property = null;
int recordsCount = response.getPropertyCount();
float[] rets = new float[recordsCount];
for (int i = 0; i < recordsCount; i++) {
property = response.getProperty(i).toString();
rets[i] = Float.valueOf(property);
}
return rets;
}
public static double[] getDoubleArray(SoapObject response) {
String property = null;
int recordsCount = response.getPropertyCount();
double[] rets = new double[recordsCount];
for (int i = 0; i < recordsCount; i++) {
property = response.getProperty(i).toString();
rets[i] = Double.valueOf(property);
}
return rets;
}
private static String[] PRIMITIVE_TYPES = { "boolean", "int", "long",
"short", "byte", "float", "double" };
private static String[] PRIMITIVE_WRAPPER_TYPES = { "Boolean", "Integer",
"Long", "Short", "Byte", "Float", "Double" };
private static String[] PRIMITIVE_FETCH_VALUES = {
"Boolean response = (Boolean) envelope.getResponse();",
"Integer response = (Integer) envelope.getResponse();",
"Long response = (Long) envelope.getResponse();",
"Short response = (Short) envelope.getResponse();",
"Byte response = (Byte) envelope.getResponse();",
"Float response = (Float) envelope.getResponse();",
"Double response = (Double) envelope.getResponse();" };
private static String[] PRIMITIVE_RETURN_VALUES = {
"return response.booleanValue();", "return response.intValue();",
"return response.longValue();", "return response.shortValue();",
"return response.byteValue();", "return response.floatValue();",
"return response.doubleValue();" };
private static String[] PRIMITIVE_RETURN_FAULT_VALUES = { "return false;",
"return 0;", "return 0;", "return 0;", "return 0;", "return 0;",
"return 0;" };
private static int findPrimitiveTypeIndex(String typeName) {
for (int i = 0; i < PRIMITIVE_TYPES.length; i++)
if (typeName.equals(PRIMITIVE_TYPES[i]))
return i;
return -1;
}
public static String getWrapperClassName(String typeName) {
int i = findPrimitiveTypeIndex(typeName);
if (i != -1)
return PRIMITIVE_WRAPPER_TYPES[i];
else
return null;
}
public static boolean isPrimitiveType(String typeName) {
return findPrimitiveTypeIndex(typeName) != -1;
}
public static boolean isArrayType(String typeName) {
return typeName.indexOf("[]") >= 0;
}
public static boolean isStringType(String typeName) {
return typeName.endsWith("String");
}
public static boolean isWrapperType(String typeName) {
String shortTypeName = getBaseType(typeName);
for (int i = 0; i < PRIMITIVE_WRAPPER_TYPES.length; i++)
if (shortTypeName.equals(PRIMITIVE_WRAPPER_TYPES[i]))
return true;
return false;
}
public static String getPrimitiveTypeFetchValue(String typeName) {
int i = findPrimitiveTypeIndex(typeName);
if (i != -1)
return PRIMITIVE_FETCH_VALUES[i];
else
return "";
}
public static String getPrimitiveTypeReturnValue(String typeName,
boolean fault) {
int i = findPrimitiveTypeIndex(typeName);
if (i != -1)
return fault ? PRIMITIVE_RETURN_FAULT_VALUES[i]
: PRIMITIVE_RETURN_VALUES[i];
else
return "";
}
public static String getBaseType(String typeName) {
String ret = typeName.substring(typeName.lastIndexOf('.') + 1);
return ret.replace("[]", "");
}
}