`
cywhoyi
  • 浏览: 418606 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

android与服务端的访问设计层,webservice的解析

    博客分类:
  • JAVA
 
阅读更多



 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("[]", "");
	}

}

 

  • 大小: 47.6 KB
分享到:
评论

相关推荐

    Android通过WebService访问SQLServer

    本教程主要探讨如何在Android应用中通过WebService访问SQLServer数据库,实现数据的增、删、查操作。以下是相关知识点的详细说明: 1. **Android平台与Web服务的交互**: Android系统提供了多种方式与远程服务器...

    android webservice 客户端、服务端实例

    本实例将深入探讨如何构建一个Android Web Service客户端和服务端,并提供可直接运行的代码示例。以下是对这两个重要组件的详细说明。 **一、Android Web Service客户端** 在Android客户端,通常使用HTTP库来调用...

    通过webservice与android实现通信(C#与android)

    通过WebService与Android实现通信是跨平台开发中常见的一种方式,特别是在使用C#作为后端语言进行服务端开发,而Android应用作为前端时。本文将基于提供的文件信息,深入解析这一技术实现的关键步骤与原理。 ### ...

    RSS阅读器的webservice服务端

    【标题解析】 "RSS阅读器的webservice...总结来说,这个“RSS阅读器的webservice服务端”项目涉及了Android应用开发、Web服务设计、数据库管理和网络安全等多个IT领域,对于开发者来说,是一个综合实践和学习的好素材。

    android webserviceandroid webserviceandroid

    在本篇文章中,我们将基于给定的代码片段来探讨如何在Android应用程序中集成WebService功能,并实现与服务器端的数据交换。本文将详细介绍WebService的基本概念、在Android中的实现方法以及具体的代码示例分析。 ##...

    源码 网站交互&JSON&xml&Webservice Android解析服务端XML.zip

    免责声明:资料部分来源于合法的互联网渠道收集和整理,部分自己学习积累成果,供大家学习参考与交流。收取的费用仅用于收集和整理资料耗费时间的酬劳。 本人尊重原创作者或出版方,资料版权归原作者或出版方所有,...

    Android axis调用Webservice

    总结,Android利用Axis2调用Web Service涉及服务端接口设计、客户端代码实现、网络请求和数据解析等多个环节。了解这些知识点有助于构建稳定可靠的跨平台通信。在实际开发中,还需要考虑错误处理、性能优化以及安全...

    Android调用webservice进行实体对象的传递Json

    三、Android与WebService的通信步骤 1. 创建SoapObject,设置方法名和参数。 2. 创建SoapSerializationEnvelope,设置SOAP版本并添加请求对象。 3. 使用HttpTransport(如AndroidHttpTransport)调用WebService。 4....

    Android通过Ksoap2访问dotNet_WebService

    它使得Android应用程序能够轻松地与使用SOAP协议的Web服务进行通信,而dotNet_WebService通常就是这样的服务,因为.NET框架广泛使用SOAP作为其Web服务的默认通信协议。 **一、集成Ksoap2** 在Android项目中,我们...

    利用JSON WebService实现Android访问远程数据库.pdf

    本文选用JSON作为数据传输和解析的方式是因为JSON方式与传统的XML解析方式相比,更轻量级、数据可读性方面更经济、越来越多的API供应商对JSON进行了封装、Android通常在蜂窝数据网络上工作的资源受限等原因。...

    Android调用Java WebService的实现方法.pdf

    文档中提到,Web Service接口的创建和发布对于Android开发者而言,是掌握网络数据交互的一个重要环节,涉及到服务端和客户端的共同协作,以及对数据格式的正确解析。 最后,文档中强调了在进行Web Service开发时,...

    rest风格webservice,服务端用的springmvc,客户端android,json传输,另附一个cxf的soap协议的wsdl服务端

    以前做java的初步理解了下...实现了3个接口服务,1个接口服务端提供登录是用的传统webservice,用的cxf。1个接口只是测试,测试了双向json数据传输解析。还一个接口是网络上提供的手机归属地查询,实现了它的接口。

    WebService服务端(java)-客户端(android)使用例子.doc

    【WebService服务端与Android客户端交互】\n\n在软件开发中,经常需要实现不同系统间的通信,例如Java Web服务端与Android客户端的数据交换。这里我们详细介绍一个基于WebService的服务端(Java)与客户端(Android...

    Android+Webservice Oracle

    首先,`Android+Webservice Oracle`的标题表明我们将关注的重点是Android应用程序如何通过Web服务与Oracle数据库进行通信。Web服务,特别是基于SOAP或RESTful架构的Webservice,提供了一种标准化的方式,使得不同...

    JAVA webservice and android端

    【JAVA WebService与Android端交互】 在移动应用开发中,尤其是Android平台,与服务器进行数据交换是必不可少的。Java WebService,特别是基于RESTful架构的Web服务,为客户端(如Android应用程序)提供了灵活、轻量...

    Android+REST WebService服务方式手机开发

    在Android应用开发中,结合RESTful Web Service是一种常见的实践,用于实现移动设备与服务器之间的数据交互。本篇文章将深入探讨如何在Android项目中利用RESTful Web Service进行开发,以及涉及的相关技术点。 首先...

    android ksoap2 3.6 webservice专用

    KSOAP2是一个轻量级的开源库,专为Android设计,用于处理SOAP(简单对象访问协议)消息,从而与基于Web Service的服务进行通信。本篇将详细介绍Android KSOAP2 3.6版本在Webservice中的应用及关键知识点。 1. **...

    在android中使用KSOAP2调用webservice

    在Android开发中,有时我们需要与服务器进行数据交互,这时候Web Service接口就显得尤为重要。Web Service是一种基于SOAP(Simple Object Access Protocol)协议的跨平台通信方式,允许不同系统间的数据交换。由于...

    android调用.net的webservice

    在.NET服务端配置允许Android客户端的源进行访问。 4. **数据库集成** - 在Android客户端,可以使用SQLite数据库存储本地数据。当从Web服务获取数据时,可以选择同步到本地数据库,以实现离线访问或提高性能。 - ...

Global site tag (gtag.js) - Google Analytics