gpt4 book ai didi

android-ksoap2 - Android ksoap 对象映射

转载 作者:行者123 更新时间:2023-12-01 18:32:05 25 4
gpt4 key购买 nike

如何使用 KvmSerialized 将 SoapObject 的响应映射到 MyObject?

我阅读了有关 ksoap2-android 的 wiki。但还是做不到。
我在信封中添加了一张 map :

envelope.addMapping(NAMESPACE, "return", new GoodsObject().getClass());

但我遇到了一个异常(exception):
java.lang.ClassCastException: org.ksoap2.serialization.SoapObject
在字符串上

GoodsObject resultsRequestSOAP = (GoodsObject) envelope.bodyIn

代码如下。我花了很多时间在这个问题上,但仍然没有想法(

<小时/>

Java 类 GoodsObject:

    package com.deaddy64.droid;

import java.util.Hashtable;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;


public class GoodsObject implements KvmSerializable {

/**
*
*/
public String ref;
public String DeletionMark;
public String Code;
public String Description;


private static final long serialVersionUID = -4872313975732374054L;

public void setref(String strref) {
this.ref = strref;
}

public String getref() {
return this.ref;
}

public void setDeletionMark(String strDeletionMark) {
this.DeletionMark = strDeletionMark;
}

public String getDeletionMark() {
return this.DeletionMark;
}

public void setCode(String strCode) {
this.Code = strCode;
}

public String getCode() {
return this.Code;
}

public void setDescription(String strDescription) {
this.Description = strDescription;
}

public String getDescription() {
return this.Description;
}

/* (non-Javadoc)
* @see org.ksoap2.serialization.KvmSerializable#getProperty(int)
*/
public Object getProperty(int index) {
switch (index) {
case 0:
return ref;
case 1:
return DeletionMark;
case 2:
return Code;
case 3:
return Description;

}
return null;
}

/* (non-Javadoc)
* @see org.ksoap2.serialization.KvmSerializable#getPropertyCount()
*/
public int getPropertyCount() {
// TODO Auto-generated method stub
return 4;
}



/* (non-Javadoc)
* @see org.ksoap2.serialization.KvmSerializable#setProperty(int, java.lang.Object)
*/
public void setProperty(int index, Object value) {
switch(index) {
case 0:
this.ref = value.toString();
break;
case 1:
this.DeletionMark = value.toString();
break;
case 2:
this.Code = value.toString();
break;
case 3:
this.Description = value.toString();
break;
default:
break;


}

}

public void getPropertyInfo(int index, Hashtable properties,
PropertyInfo info) {
switch (index) {
case 0:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Ref";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "DeletionMark";
break;
case 2:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Code";
break;
case 3:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Description";
break;

}
}
}

这是ResponseDump:

    <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header/>
<soap:Body> <m:getReferenceResponse xmlns:m="com.deaddy64">
<m:return xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Ref xmlns="http://v8.1c.ru/8.1/data/enterprise/current-config">7f4fb970-6a62-11e1-9bd0-001fd027b6bb</Ref>
<DeletionMark xmlns="http://v8.1c.ru/8.1/data/enterprise/current-config">false</DeletionMark>
<Code xmlns="http://v8.1c.ru/8.1/data/enterprise/current-config">000000003</Code>
<Description xmlns="http://v8.1c.ru/8.1/data/enterprise/current-config">123</Description>
</m:return>
</m:getReferenceResponse></soap:Body>
</soap:Envelope>

在调试器中envelope.bodyIn是:

      (org.ksoap2.serialization.SoapObject) getReferenceResponse{return=anyType{Ref=7f4fb970-6a62-11e1-9bd0-001fd027b6bb; DeletionMark=false; Code=000000003; Description=123; }; }

主要事件:

    package com.deaddy64.droid;

import java.io.IOException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class Droid1Activity extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


myButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {



String NAMESPACE = "com.deaddy64";
String URL = "http://10.0.2.2/InfoBase/ws/ws1.1cws";
String SOAP_ACTION = "com.deaddy64#testService:getReference";
String METHOD_NAME = "getReference";


SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

//envelope.dotNet = true;

envelope.setOutputSoapObject(request);
envelope.addMapping("http://v8.1c.ru/8.1/data/enterprise/current-config", "CatalogObject.Номенклатура", new GoodsObject().getClass());

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
androidHttpTransport.debug = true;
Log.v("dd64", "xml");
GoodsObject resultsRequestSOAP;
try {
// List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
//headerList.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("username:password".getBytes())));
androidHttpTransport.call(SOAP_ACTION, envelope);
resultsRequestSOAP = (GoodsObject) envelope.getResponse();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v("dd64",androidHttpTransport.requestDump);
Log.v("dd64", androidHttpTransport.responseDump);




}
});
}

}

最佳答案

试试这个:

envelope.addMapping(NAMESPACE, "GoodsObject", new GoodsObject().getClass());

GoodsObject resultsRequestSOAP = (GoodsObject) envelope.getResponse();

希望有帮助。

关于android-ksoap2 - Android ksoap 对象映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10160719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com