Java kSoap2 POJO映射类强制转换异常
我在使用android的kSOAP2库时遇到困难。我能够发送请求并获得响应,但我希望将响应的内容映射到普通java对象。基本上,kSOAP2允许您通过指定映射到方法中的参数来实现这一点。无论如何,当我试图将响应转换为Java对象时,我遇到了一个类转换异常。以下是我的实现:Java kSoap2 POJO映射类强制转换异常,java,android,soap,android-ksoap2,Java,Android,Soap,Android Ksoap2,我在使用android的kSOAP2库时遇到困难。我能够发送请求并获得响应,但我希望将响应的内容映射到普通java对象。基本上,kSOAP2允许您通过指定映射到方法中的参数来实现这一点。无论如何,当我试图将响应转换为Java对象时,我遇到了一个类转换异常。以下是我的实现: final SoapSerializationEnvelope envelope = LoginInfo.buildLoginObject(login,password); envelope.addMapping(NAMESP
final SoapSerializationEnvelope envelope = LoginInfo.buildLoginObject(login,password);
envelope.addMapping(NAMESPACE, "GetCustomerByIdPwdResponse", new GetCustomerByIdPwdResponse().getClass());
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
HttpTransportSE httpTransport = new HttpTransportSE("URL");
httpTransport.debug = true;
try {
httpTransport.call(URL, envelope);
Log.d("SOAP OBJECT SENT",httpTransport.requestDump);
Log.d("SOAP OBJECT RESPONSE",httpTransport.responseDump);
GetCustomerByIdPwdResponse customerByIdPwd = (GetCustomerByIdPwdResponse)envelope.bodyIn;
} catch (IOException | XmlPullParserException e) {
e.printStackTrace();
}
return null;
}
}.execute();
任何帮助都将是巨大的,因为这是让我走向web服务实现的最后一步
谢谢
更新:
这是我给客户的课程。现在,我在尝试将客户映射到我的RexUser类时出错。
我添加了envelope.addMapping(“rexwebservices”、“RexUser”、“RexUser.class”)代码>映射到我的信封。
我有一个由以下原因引起的:java.lang.RuntimeException:未知属性:FirstName
将客户数据保存到用户对象时出错
public class RexUser implements KvmSerializable{
public static final int FIRSTNAME = 0;
public static final int ID = 1;
public static final int LASTNAME = 6;
public static final int LOGINMAIL = 7;
public static final int MOBILEPHONE = 8;
public static final int LANDLINEPHONE = 5;
public static final int TITLE = 9;
public static final int IS_ACTIVATED = 2;
public static final int IS_LOCKED_OUT = 3;
public static final int IS_NEWSLETTER_ACCEPTED = 4;
String FirstName;
long Id;
boolean IsActived;
boolean IsLockedOut;
boolean IsNewsLetterAccepted;
String LandLinePhone;
String LastName;
String LoginMail;
String MobilePhone;
String Title;
public RexUser(String firstName, long id, boolean isActivated,
boolean isLockedOut, boolean isNewsLetterAccepted,
String landLinePhone, String lastName, String loginMail,
String mobilePhone, String title) {
super();
this.FirstName = firstName;
this.Id = id;
this.IsActived = isActivated;
this.IsLockedOut = isLockedOut;
this.IsNewsLetterAccepted = isNewsLetterAccepted;
this.LandLinePhone = landLinePhone;
this.LastName = lastName;
this.LoginMail = loginMail;
this.MobilePhone = mobilePhone;
this.Title = title;
}
public RexUser() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
HashMap<String,Object> desc = new HashMap<String,Object>();
desc.put("First name",FirstName);
desc.put("ID",Id);
desc.put("is Actived", IsActived);
desc.put("is Locked Out",IsLockedOut);
desc.put("isNewsLetterAccepted", IsNewsLetterAccepted);
desc.put("Land Line Phone", LandLinePhone);
desc.put("lastname", LastName);
desc.put("login Mail", LoginMail);
desc.put("mobile phone",MobilePhone);
desc.put("title", Title);
return desc.toString();
}
@Override
public Object getProperty(int arg0) {
switch(arg0){
case 0:
return FirstName;
case 1:
return Id;
case 2:
return IsActived;
case 3:
return IsLockedOut;
case 4:
return IsNewsLetterAccepted;
case 5:
return LandLinePhone;
case 6:
return LastName;
case 7:
return LoginMail;
case 8:
return MobilePhone;
case 9:
return Title;
default:
return null;
}
}
@Override
public int getPropertyCount() {
return 10;
}
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
switch(arg0){
case 0:
arg2.name = "First Name";
arg2.type = String.class;
break;
case 1:
arg2.name = "ID";
arg2.type = int.class;
break;
case 2:
arg2.name = "Is Actived";
arg2.type = boolean.class;
break;
case 3:
arg2.name = "Is Locked Out";
arg2.type = boolean.class;
break;
case 4:
arg2.name = "Is Newsletter Accepted";
arg2.type = boolean.class;
break;
case 5:
arg2.name = "Landline Phone";
arg2.type = String.class; break;
case 6:
arg2.name = "Last Name";
arg2.type = String.class;
break;
case 7:
arg2.name = "Login mail";
arg2.type = String.class;
break;
case 8:
arg2.name = "Mobile Phone";
arg2.type = String.class;
break;
case 9:
arg2.name = "Title";
arg2.type = String.class;
break;
default:
break;
}
}
@Override
public void setProperty(int arg0, Object arg1) {
switch(arg0){
case 0:
FirstName = (String)arg1;
break;
case 1:
Id = (int)arg1;
break;
case 2:
IsActived = (boolean)arg1;
break;
case 3:
IsLockedOut = (boolean)arg1;
break;
case 4:
IsNewsLetterAccepted = (boolean)arg1;
break;
case 5:
LandLinePhone = (String)arg1;
break;
case 6:
LastName = (String)arg1;
break;
case 7:
LoginMail = (String)arg1;
break;
case 8:
MobilePhone = (String)arg1;
break;
case 9:
Title = (String)arg1;
break;
default:
break;
}
}
}
public类RexUser实现了kvmseriable{
公共静态final int FIRSTNAME=0;
公共静态最终int ID=1;
公共静态final int LASTNAME=6;
公共静态最终int LOGINMAIL=7;
公共静态终端int移动电话=8;
公共静态终端int固定电话=5;
公共静态最终整数标题=9;
公共静态最终int被激活=2;
公共静态最终int为锁定状态=3;
公共静态最终int为_新闻稿_接受=4;
字符串名;
长Id;
布尔激活;
布尔IsLockedOut;
接受布尔值;
有线电话;
字符串LastName;
字符串登录邮件;
线状手机;
字符串标题;
公共用户(字符串名,长id,布尔值为已激活,
布尔值isLockedOut,布尔值isLockedOut,
字符串landLinePhone、字符串lastName、字符串loginMail、,
字符串移动电话,字符串标题){
超级();
this.FirstName=FirstName;
这个.Id=Id;
this.isActivated=已激活;
this.IsLockedOut=IsLockedOut;
this.isnewsletteAccepted=isnewsletteAccepted;
this.LandLinePhone=LandLinePhone;
this.LastName=LastName;
this.LoginMail=LoginMail;
this.MobilePhone=MobilePhone;
这个.Title=Title;
}
公共用户(){
//TODO自动生成的构造函数存根
}
@凌驾
公共字符串toString(){
HashMap desc=新HashMap();
desc.put(“名字”,名字);
描述放置(“ID”,ID);
desc.put(“已激活”,已激活);
描述输出(“锁定”,IsLockedOut);
desc.put(“IsNewsletteAccepted”,IsNewsletteAccepted);
描述put(“陆地电话线”,陆地电话线);
desc.put(“lastname”,lastname);
desc.put(“登录邮件”,登录邮件);
desc.put(“移动电话”,移动电话);
描述放置(“标题”,标题);
返回desc.toString();
}
@凌驾
公共对象getProperty(int arg0){
开关(arg0){
案例0:
返回名字;
案例1:
返回Id;
案例2:
返回被激活;
案例3:
返回锁定输出;
案例4:
已接受退货;
案例5:
返回固定电话;
案例6:
返回姓氏;
案例7:
返回登录邮件;
案例8:
返回手机;
案例9:
返回标题;
违约:
返回null;
}
}
@凌驾
public int getPropertyCount(){
返回10;
}
@凌驾
public void getPropertyInfo(int arg0、哈希表arg1、PropertyInfo arg2){
开关(arg0){
案例0:
arg2.name=“名字”;
arg2.type=String.class;
打破
案例1:
arg2.name=“ID”;
arg2.type=int.class;
打破
案例2:
arg2.name=“已激活”;
arg2.type=boolean.class;
打破
案例3:
arg2.name=“已锁定”;
arg2.type=boolean.class;
打破
案例4:
arg2.name=“是否接受新闻稿”;
arg2.type=boolean.class;
打破
案例5:
arg2.name=“固定电话”;
arg2.type=String.class;中断;
案例6:
arg2.name=“姓氏”;
arg2.type=String.class;
打破
案例7:
arg2.name=“登录邮件”;
arg2.type=String.class;
打破
案例8:
arg2.name=“手机”;
arg2.type=String.class;
打破
案例9:
arg2.name=“Title”;
arg2.type=String.class;
打破
违约:
打破
}
}
@凌驾
公共void setProperty(int arg0,对象arg1){
开关(arg0){
案例0:
FirstName=(字符串)arg1;
打破
案例1:
Id=(int)arg1;
打破
案例2:
IsActived=(布尔)arg1;
打破
案例3:
IsLockedOut=(布尔)arg1;
打破
案例4:
ISR=(布尔值)arg1;
打破
案例5:
固定电话=(字符串)arg1;
打破
案例6:
LastName=(字符串)arg1;
打破
案例7:
LoginMail=(字符串)arg1;
打破
案例8:
手机=(字符串)arg1;
打破
案例9:
Title=(字符串)arg1;
打破
违约:
打破
}
}
}
首先确保结构中大多数外部对象(GetCustomerByDPWDResponse)的命名空间为“rexwebservices”,即
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">rexwebservices/IWebShopServices/GetCustomerByIdPwdResponse</a:Action>
</s:Header>
<s:Body>
<GetCustomerByIdPwdResponse xmlns="rexwebservices">
<GetCustomerByIdPwdResult xmlns:b="http://schemas.datacontract.org/2004/07/RexWebServices" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<b:Customer>
<b:FirstName>...</b:FirstName>
<b:Id>...</b:Id>
<b:IsActived>true</b:IsActived>
<b:IsLockedOut>false</b:IsLockedOut>
<b:IsNewsLetterAccepted>false</b:IsNewsLetterAccepted>
<b:LandLinePhone />
<b:LastName>...</b:LastName>
<b:LoginMail>...</b:LoginMail>
<b:MobilePhone />
<b:Title>Mr</b:Title>
</b:Customer>
<b:ErrorMessage>+00X00000</b:ErrorMessage>
<b:SessionId>SID</b:SessionId>
</GetCustomerByIdPwdResult>
</GetCustomerByIdPwdResponse>
</s:Body>
</s:Envelope>
11-18 11:36:57.967: E/AndroidRuntime(19208): Caused by: java.lang.ClassCastException: org.ksoap2.serialization.SoapObject cannot be cast to com.example.ksoaptest.GetCustomerByIdPwdResponse
11-18 11:36:57.967: E/AndroidRuntime(19208): at com.example.ksoaptest.MainActivity$2.doInBackground(MainActivity.java:70)
11-18 11:36:57.967: E/AndroidRuntime(19208): at com.example.ksoaptest.MainActivity$2.doInBackground(MainActivity.java:1)
11-18 11:36:57.967: E/AndroidRuntime(19208): at android.os.AsyncTask$2.call(AsyncTask.java:288)
11-18 11:36:57.967: E/AndroidRuntime(19208): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-18 11:36:57.967: E/AndroidRuntime(19208): ... 4 more
public class RexUser implements KvmSerializable{
public static final int FIRSTNAME = 0;
public static final int ID = 1;
public static final int LASTNAME = 6;
public static final int LOGINMAIL = 7;
public static final int MOBILEPHONE = 8;
public static final int LANDLINEPHONE = 5;
public static final int TITLE = 9;
public static final int IS_ACTIVATED = 2;
public static final int IS_LOCKED_OUT = 3;
public static final int IS_NEWSLETTER_ACCEPTED = 4;
String FirstName;
long Id;
boolean IsActived;
boolean IsLockedOut;
boolean IsNewsLetterAccepted;
String LandLinePhone;
String LastName;
String LoginMail;
String MobilePhone;
String Title;
public RexUser(String firstName, long id, boolean isActivated,
boolean isLockedOut, boolean isNewsLetterAccepted,
String landLinePhone, String lastName, String loginMail,
String mobilePhone, String title) {
super();
this.FirstName = firstName;
this.Id = id;
this.IsActived = isActivated;
this.IsLockedOut = isLockedOut;
this.IsNewsLetterAccepted = isNewsLetterAccepted;
this.LandLinePhone = landLinePhone;
this.LastName = lastName;
this.LoginMail = loginMail;
this.MobilePhone = mobilePhone;
this.Title = title;
}
public RexUser() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
HashMap<String,Object> desc = new HashMap<String,Object>();
desc.put("First name",FirstName);
desc.put("ID",Id);
desc.put("is Actived", IsActived);
desc.put("is Locked Out",IsLockedOut);
desc.put("isNewsLetterAccepted", IsNewsLetterAccepted);
desc.put("Land Line Phone", LandLinePhone);
desc.put("lastname", LastName);
desc.put("login Mail", LoginMail);
desc.put("mobile phone",MobilePhone);
desc.put("title", Title);
return desc.toString();
}
@Override
public Object getProperty(int arg0) {
switch(arg0){
case 0:
return FirstName;
case 1:
return Id;
case 2:
return IsActived;
case 3:
return IsLockedOut;
case 4:
return IsNewsLetterAccepted;
case 5:
return LandLinePhone;
case 6:
return LastName;
case 7:
return LoginMail;
case 8:
return MobilePhone;
case 9:
return Title;
default:
return null;
}
}
@Override
public int getPropertyCount() {
return 10;
}
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
switch(arg0){
case 0:
arg2.name = "First Name";
arg2.type = String.class;
break;
case 1:
arg2.name = "ID";
arg2.type = int.class;
break;
case 2:
arg2.name = "Is Actived";
arg2.type = boolean.class;
break;
case 3:
arg2.name = "Is Locked Out";
arg2.type = boolean.class;
break;
case 4:
arg2.name = "Is Newsletter Accepted";
arg2.type = boolean.class;
break;
case 5:
arg2.name = "Landline Phone";
arg2.type = String.class; break;
case 6:
arg2.name = "Last Name";
arg2.type = String.class;
break;
case 7:
arg2.name = "Login mail";
arg2.type = String.class;
break;
case 8:
arg2.name = "Mobile Phone";
arg2.type = String.class;
break;
case 9:
arg2.name = "Title";
arg2.type = String.class;
break;
default:
break;
}
}
@Override
public void setProperty(int arg0, Object arg1) {
switch(arg0){
case 0:
FirstName = (String)arg1;
break;
case 1:
Id = (int)arg1;
break;
case 2:
IsActived = (boolean)arg1;
break;
case 3:
IsLockedOut = (boolean)arg1;
break;
case 4:
IsNewsLetterAccepted = (boolean)arg1;
break;
case 5:
LandLinePhone = (String)arg1;
break;
case 6:
LastName = (String)arg1;
break;
case 7:
LoginMail = (String)arg1;
break;
case 8:
MobilePhone = (String)arg1;
break;
case 9:
Title = (String)arg1;
break;
default:
break;
}
}
}
envelope.addMapping("rexwebservices", "GetCustomerByIdPwdResponse", GetCustomerByIdPwdResponse.class);
envelope.addMapping("rexwebservices", "GetCustomerByIdPwdResult", GetCustomerByIdPwdResult.class);
public class GetCustomerByIdPwdResponse implements KvmSerializable {
GetCustomerByIdPwdResult getCustomerByIdPwdResult;
@Override
public Object getProperty(int arg0) {
if(arg0==0){
return getCustomerByIdPwdResult;
}
return null;
}
@Override
public int getPropertyCount() {
return 1;
}
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
if(arg0==0){
arg2.name="GetCustomerByIdPwdResult";
arg2.type=GetCustomerByIdPwdResult.class;
}
}
@Override
public void setProperty(int arg0, Object arg1) {
if(arg0==0){
getCustomerByIdPwdResult=(GetCustomerByIdPwdResult)arg1;
}
}
}
public class GetCustomerByIdPwdResult implements KvmSerializable {
SoapObject Customer; //here goes Customer class ofcourse
String ErrorMessage;
String SessionId;
@Override
public Object getProperty(int arg0) {
switch(arg0){
case 0: return Customer;
case 1: return ErrorMessage;
case 2: return SessionId;
}
return null;
}
@Override
public int getPropertyCount() {
return 3;
}
@Override
public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) {
switch(arg0){
case 0: arg2.name="Customer"; arg2.type=SoapObject.class;break; //Customer class
case 1: arg2.name="ErrorMessage"; arg2.type=PropertyInfo.STRING_CLASS;break;
case 2: arg2.name="SessionId"; arg2.type=PropertyInfo.STRING_CLASS;break;
}
}
@Override
public void setProperty(int arg0, Object arg1) {
switch(arg0){
case 0: Customer=(SoapObject)arg1;break;//Customer class
case 1: ErrorMessage=(String)arg1;break;
case 2: SessionId=(String)arg1;break;
}
}
}