Android利用ksoap传递复杂对象参数
2012-12-19 14:27:32 来源:WEB开发网 Android 传递复杂对象到wcf方法:
wcf 服务端相关配置:
1 创建一个User
[DataContract]
public class User
{
[DataMember]
public int Age
{
get;
set;
}
[DataMember]
public String Name
{
get;
set;
}
}
2 创建一个方法
#region IService1 成员
public string GetUserInfo(User user)
{
if (null != user)
return user.Age + " " + user.Name;
return "";
}
#endregion
3 配置先关参数,需要注意的是 bingding采用
<services>
<service name="test.Service1">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
contract="test.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/test/Service1/" />
</baseAddresses>
</host>
</service>
</services>
android端配置:
1 创建一个相同的对象在Android端
public class User{
public static Class UserClass = User.class;
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public static User fromString(String s)
{
User result = new User();
String[] tokens = s.split("=");
String age = tokens[1].split(";")[0];
String name = tokens[2].split(";")[0];
result.Age = Integer.parseInt(age);
result.Name = name;
return result;
}
private int Age;
private String Name;
}
2 创建一个User对象的Marshal对象
public class MarshalUser implements Marshal {
private static String dataContract = "http://schemas.datacontract.org/2004/07/test";//要注意这儿的定义,具体查看wcf端的xml是如何定义的
@Override
public Object readInstance(XmlPullParser parser, String arg1, String arg2,
PropertyInfo arg3) throws IOException, XmlPullParserException {
return User.fromString(parser.nextText());
}
@Override
public void register(SoapSerializationEnvelope cm) {
cm.addMapping(dataContract, "user", User.class, this);
}
@Override
public void writeInstance(XmlSerializer writer, Object obj)
throws IOException {
User user = (User)obj;
writer.startTag(dataContract, "Age");
writer.text(Integer.toString(user.getAge()));
- ››Android 当修改一些代码时,使用什么编译命令可以最...
- ››Android 如何添加一个apk使模拟器和真机都编译进去...
- ››Android 修改Camera拍照的默认保存路径
- ››Android 如何修改默认输入法
- ››android开发中finish()和System.exit(0)的区别
- ››Android手势识别简单封装类
- ››android中查看项目数字证书的两种方法
- ››Android中获取IMEI码的办法
- ››android 相机报错 setParameters failed
- ››Android重启运用程序的代码
- ››Android为ListView的Item设置不同的布局
- ››android bitmap与base64字符串的互相转换
更多精彩
赞助商链接