版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android调用WebService系列之对象构建传递
上一篇我们讲了如何封装Android调用WebService的能力,把上一章的类加入我们便有了与WebService通讯的能力。往往我们会遇到WebService调用是通过对象来进行实际交互调用的。于是便有了这一章构建对象传递。首先我们了解一下。Ksoap2这个开源包里面提供了一个接口/*
Copyright
(c)
2003,2004,
Stefan
Haustein,
Oberhausen,
Rhld.,
Germany
*
*
Permission
is
hereby
granted,
free
of
charge,
to
any
person
obtaining
a
copy
*
of
this
software
and
associated
documentation
files
(the
"Software"),
to
deal
*
in
the
Software
without
restriction,
including
without
limitation
the
rights
*
to
use,
copy,
modify,
merge,
publish,
distribute,
sublicense,
and/or
*
sell
copies
of
the
Software,
and
to
permit
persons
to
whom
the
Software
is
*
furnished
to
do
so,
subject
to
the
following
conditions:
*
*
The
above
copyright
notice
and
this
permission
notice
shall
be
included
in
*
all
copies
or
substantial
portions
of
the
Software.
*
*
THE
SOFTWARE
IS
PROVIDED
"AS
IS",
WITHOUT
WARRANTY
OF
ANY
KIND,
EXPRESS
OR
*
IMPLIED,
INCLUDING
BUT
NOT
LIMITED
TO
THE
WARRANTIES
OF
MERCHANTABILITY,
*
FITNESS
FOR
A
PARTICULAR
PURPOSE
AND
NONINFRINGEMENT.
IN
NO
EVENT
SHALL
THE
*
AUTHORS
OR
COPYRIGHT
HOLDERS
BE
LIABLE
FOR
ANY
CLAIM,
DAMAGES
OR
OTHER
*
LIABILITY,
WHETHER
IN
AN
ACTION
OF
CONTRACT,
TORT
OR
OTHERWISE,
ARISING
*
FROM,
OUT
OF
OR
IN
CONNECTION
WITH
THE
SOFTWARE
OR
THE
USE
OR
OTHER
DEALINGS
*
IN
THE
SOFTWARE.
*
*
Contributor(s):
John
D.
Beatty,
F.
Hunter,
Renaud
Tognelli
*
*
*/
package
org.ksoap2.serialization;
import
java.util.Hashtable;
/**
*
Provides
get
and
set
methods
for
properties.
Can
be
used
to
replace
*
reflection
(to
some
extend)
for
"serialization-aware"
classes.
Currently
used
*
in
kSOAP
and
the
RMS
based
kobjects
object
repository
*/
public
interface
KvmSerializable
{
/**
*
Returns
the
property
at
a
specified
index
(for
serialization)
*
*
@param
index
*
the
specified
index
*
@return
the
serialized
property
*/
Object
getProperty(int
index);
/**
*
@return
the
number
of
serializable
properties
*/
int
getPropertyCount();
/**
*
Sets
the
property
with
the
given
index
to
the
given
value.
*
*
@param
index
*
the
index
to
be
set
*
@param
value
*
the
value
of
the
property
*/
void
setProperty(int
index,
Object
value);
/**
*
Fills
the
given
property
info
record.
*
*
@param
index
*
the
index
to
be
queried
*
@param
properties
*
information
about
the
(de)serializer.
Not
frequently
used.
*
@param
info
*
The
return
parameter,
to
be
filled
with
information
about
the
*
property
with
the
given
index.
*/
void
getPropertyInfo(int
index,
Hashtable
properties,
PropertyInfo
info);
}
接口的有这么一句话inkSOAPandtheRMSbasedkobjectsobjectrepository,大致意思应该就是基于对象存储的时候可以用到他。(当然借助翻译工具翻译的,有什么理解上错误的请联系我)那么意味着我们只需要把要传递的对象实现这个接口就可以实现对象传输了!于是乎就有很多网文实现教你如何去实现了!我示例一下!public
Test
implements
KvmSerializable
{
public
String
test1;
public
String
test2;
//Returns
the
property
at
a
specified
index
(for
serialization)
//通过索引返回特定属性(翻译:返回属性在指定的索引(序列化))
@Override
public
Object
getProperty(int
index)
{
//根据接口注释最直接的会如下操作
switch(index){
...(return
test1
之类)
}
}
//return
the
number
of
serializable
properties
//返回属性的个数(翻译:返回的数量可序列化的属性)
@Override
public
int
getPropertyCount()
{
//
TODO
Auto-generated
method
stub
//返回固定数量
return
2;
}
//Sets
the
property
with
the
given
index
to
the
given
value.
//根据index给PropertyInfo赋值参数
(翻译:属性与给定的索引设置为给定值。)
@Override
public
void
getPropertyInfo(int
index,
Hashtable
arg1,
PropertyInfo
a)
{
//根据接口注释最直接的会如下操作
swtich(index){
...
(设置a的属性值)
}
}
//
Fills
the
given
property
info
record.
//给相应索引的属性赋值(翻译:填充给定属性信息记录。)
@Override
public
void
setProperty(int
index,
Object
arg1)
{
switch(index){
...(test1
=
arg1之类)
}
}
}这样是没有错误的,但是在我们有很多不同的类需要传递的时候呢?这个类属性上百个的时候呢?那我们岂不是一直需要做重复操作。那么我们何不写一个通用的转换类!于是在不考虑更复杂,以及特定的一些数据类型的时候我们有了下面这个类:import
java.lang.reflect.Field;
import
java.lang.reflect.InvocationTargetException;
import
java.lang.reflect.Method;
import
java.lang.reflect.ParameterizedType;
import
java.lang.reflect.Type;
import
java.util.ArrayList;
import
java.util.Hashtable;
import
java.util.List;
import
java.util.Vector;
import
org.ksoap2.serialization.KvmSerializable;
import
org.ksoap2.serialization.PropertyInfo;
import
org.ksoap2.serialization.SoapObject;
/**
*
对象传输基础类
*
@author
刘亚林
*
461973266@
*
*/
public
abstract
BaseKvmSerializable
implements
KvmSerializable
{
/**
**
将首字母大写
**/
public
static
String
fristUpperCase(String
str)
{
return
String.valueOf(str.charAt(0)).toUpperCase().concat(str.substring(1));
}
//Returns
the
property
at
a
specified
index
(for
serialization)
//通过索引返回特定属性(翻译:返回属性在指定的索引(序列化))
@Override
public
Object
getProperty(int
index)
{
//既然是要返回特定索引的属性值,那么我们何不直接通过反射取对应属性返回
Field[]
fs
=
this.getClass().getDeclaredFields();
Field
f
=
fs[index];
String
name
=
f.getName();
name
=
fristUpperCase(name);
String
getMethodName
=
"get";
if
(f.getType()
==
boolean.class
||
f.getType()
==
Boolean.class)
{
getMethodName
=
"is";
}
getMethodName
+=
name;
Method
getMethod;
Object
val
=
null;
try
{
getMethod
=
this.getClass().getMethod(getMethodName);
getMethod.setAccessible(true);
val
=
getMethod.invoke(this);
}
catch
(NoSuchMethodException
e)
{
//
TODO
Auto-generated
catch
block
e.printStackTrace();
}
catch
(IllegalAccessException
e)
{
//
TODO
Auto-generated
catch
block
e.printStackTrace();
}
catch
(IllegalArgumentException
e)
{
//
TODO
Auto-generated
catch
block
e.printStackTrace();
}
catch
(InvocationTargetException
e)
{
//
TODO
Auto-generated
catch
block
e.printStackTrace();
}
return
val;
}
//return
the
number
of
serializable
properties
//返回属性的个数(翻译:返回的数量可序列化的属性)
@Override
public
int
getPropertyCount()
{
//
TODO
Auto-generated
method
stub
//返回固定数量
return
this.getClass().getDeclaredFields().length;
}
//Sets
the
property
with
the
given
index
to
the
given
value.
//根据index给PropertyInfo赋值参数
(翻译:属性与给定的索引设置为给定值。)
@Override
public
void
getPropertyInfo(int
index,
Hashtable
arg1,
PropertyInfo
a)
{
Field[]
fs
=
this.getClass().getDeclaredFields();
Field
f
=
fs[index];
String
name
=
f.getName();
//主要是设置type和name其他的需要可以继续添加
a.type
=
getTypeByClass(f.getType());
=
name;
}
//
Fills
the
given
property
info
record.
//给相应索引的属性赋值(翻译:填充给定属性信息记录。)
@Override
public
void
setProperty(int
index,
Object
arg1)
{
Field[]
fs
=
this.getClass().getDeclaredFields();
Field
f
=
fs[index];
String
name
=
f.getName();
name
=
fristUpperCase(name);
String
setMethodName
=
"set"
+
name;
Method
m;
try
{
m
=
this.getClass().getDeclaredMethod(setMethodName,
f.getType());
m.setAccessible(true);
m.invoke(this,
arg1);
}
catch
(NoSuchMethodException
e)
{
//
TODO
Auto-generated
catch
block
e.printStackTrace();
}
catch
(IllegalAccessException
e)
{
//
TODO
Auto-generated
catch
block
e.printStackTrace();
}
catch
(IllegalArgumentException
e)
{
//
TODO
Auto-generated
catch
block
e.printStackTrace();
}
catch
(InvocationTargetException
e)
{
//
TODO
Auto-generated
catch
block
e.printStackTrace();
}
}
/**
**
根据类别获得
PropertyInfo
特定类别
**
实际上除了统一类别这个没什么太多用为了心里好过而加
**
你看下面对于这些类别的的定义就知道了
** public
static
final
Class
OBJECT_CLASS
=
new
Object().getClass();
**
public
static
final
Class
STRING_CLASS
=
"".getClass();
** public
static
final
Class
INTEGER_CLASS
=
new
Integer(0).getClass();
** public
static
final
Class
LONG_CLASS
=
new
Long(0).getClass();
** public
static
final
Class
BOOLEAN_CLASS
=
new
Boolean(true).getClass();
** public
static
final
Class
VECTOR_CLASS
=
new
java.util.Vector().getClass();
**/
public
Class
getTypeByClass(Class
cls)
{
if
(cls.isAssignableFrom(Boolean.class)
||
cls.isAssignableFrom(boolean.class))
{
return
PropertyInfo.BOOLEAN_CLASS;
}
else
if
(cls.isAssignableFrom(String.class))
{
return
PropertyInfo.STRING_CLASS;
}
else
if
(cls.isAssignableFrom(Integer.class)
||
cls.isAssignableFrom(int.class)
||
cls.isAssignableFrom(byte.class)
||
cls.isAssignableFrom(Byte.class))
{
return
PropertyInfo.INTEGER_CLASS;
}
else
if
(cls.isAssignableFrom(Vector.class))
{
return
PropertyInfo.VECTOR_CLASS;
}
else
if
(cls.isAssignableFrom(Long.class)
||
cls.isAssignableFrom(long.class))
{
return
PropertyInfo.LONG_CLASS;
}
else
{
return
PropertyInfo.OBJECT_CLASS;
}
}
}当然这个类已经基本可以满足大多数不复杂类的调用了。不过一些嵌套
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 酶的本质作业设计
- 平面镜(2)(新教材)2024-2025学年八年级物理上册同步课件(苏科版)
- 2024-2030年消费者远程通信系统行业市场现状供需分析及投资评估规划分析研究报告
- 2024-2030年洋甘菊行业市场发展分析及竞争格局与投资价值研究报告
- 2024-2030年油茶行业市场发展现状及竞争格局与投资战略研究报告
- 2024-2030年汾酒产品入市调查研究报告
- 七月上旬内科主治医师考试第六次综合检测题(含答案)
- 2024-2030年汽车弹簧行业风险投资态势及投融资策略指引报告
- 2024-2030年污泥处理设备行业市场现状供需分析及投资评估规划分析研究报告
- 2024-2030年水泥搅拌设备市场发展现状分析及行业投资战略研究报告
- 预防校园欺凌事件班主任培训
- HCIA H13-111鲲鹏应用开发考试复习题库(含答案)
- 七上历史时间轴
- 学校卫生保健室使用情况登记表
- (正式版)SHT 3225-2024 石油化工安全仪表系统安全完整性等级设计规范
- 2024版医疗废物分类目录
- 中职学生安全教育课件
- 屈原【六幕话剧】郭沫若
- 鼻部整形发展与现状
- 学前教育政策与法规-全书知识点
- 仪表检测报告
评论
0/150
提交评论