【问题标题】:KSoap2 and KvmSerializable - How to Send complex Objects like StringarraysKSoap2 和 KvmSerializable - 如何发送复杂的对象,如 Stringarrays
【发布时间】:2025-11-30 03:05:02
【问题描述】:

我想将一个复杂对象从我的 Ksoap 客户端发送到我的网络服务。这是我的类类别的一个对象。代码基于本教程的示例:

http://seesharpgears.blogspot.de/2010/10/ksoap-android-web-service-tutorial-with.html

Web 服务正在运行,WSDL 文件显示类别对象。但是,如果我将类别对象发送到我的肥皂网络服务,则会出现以下错误:

[ERROR] 尝试调用服务方法时发生异常 getCategoryObject org.apache.axis2.AxisFault:未知类型 {http://ws.chathura.com}Category Logcat 显示如下:06-12 11:08:57.576:W/System.err(777):SoapFault - 故障代码: 'soapenv:Server' faultstring: '未知类型 {http://ws.chathura.com}Category' faultactor:'null' 详细信息: org.kxml2.kdom.Node@4054a528

这是我在 Tomcat 7 上运行的 Java Webservice:

package com.chathura.ws;

import java.util.Vector;



public class HelloWorldWS
{


 public Category getCategoryObject(Category obj)
 {
  Category C1 = new Category();


    C1.setCategoryId(1);
    C1.setDescription("server desc");
    C1.setName("server Name");


    System.out.println(obj.getCategoryId());

    return C1;
 }
}

这是我在 WebServer 上的类别类:

package com.chathura.ws;

public class Category {
 private int CategoryId;
 private String Name;
 private String Description;
 /**
  * @return the categoryId
  */
 public int getCategoryId() {
  return CategoryId;
 }
 /**
  * @param categoryId the categoryId to set
  */
 public void setCategoryId(int categoryId) {
  CategoryId = categoryId;
 }
 /**
  * @return the name
  */
 public String getName() {
  return Name;
 }
 /**
  * @param name the name to set
  */
 public void setName(String name) {
  Name = name;
 }
 /**
  * @return the description
  */
 public String getDescription() {
  return Description;
 }
 /**
  * @param description the description to set
  */
 public void setDescription(String description) {
  Description = description;
 }

}

这是 WSDL 文件:

    This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://ws.chathura.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ax21="http://ws.chathura.com/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://ws.chathura.com">
<wsdl:documentation>Please Type your service description here</wsdl:documentation>
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://ws.chathura.com/xsd">
<xs:complexType name="Category">
<xs:sequence>
<xs:element minOccurs="0" name="categoryId" type="xs:int"/>
<xs:element minOccurs="0" name="description" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
<xs:schema xmlns:ax22="http://ws.chathura.com/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://ws.chathura.com">
<xs:import namespace="http://ws.chathura.com/xsd"/>
<xs:element name="getCategoryObject">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="obj" nillable="true" type="ax21:Category"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="getCategoryObjectResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="ax21:Category"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="getCategoryObjectRequest">
<wsdl:part name="parameters" element="ns:getCategoryObject"/>
</wsdl:message>
<wsdl:message name="getCategoryObjectResponse">
<wsdl:part name="parameters" element="ns:getCategoryObjectResponse"/>
</wsdl:message>
<wsdl:portType name="HelloWorldWSPortType">
<wsdl:operation name="getCategoryObject">
<wsdl:input message="ns:getCategoryObjectRequest" wsaw:Action="urn:getCategoryObject"/>
<wsdl:output message="ns:getCategoryObjectResponse" wsaw:Action="urn:getCategoryObjectResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWorldWSSoap11Binding" type="ns:HelloWorldWSPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="getCategoryObject">
<soap:operation soapAction="urn:getCategoryObject" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="HelloWorldWSSoap12Binding" type="ns:HelloWorldWSPortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="getCategoryObject">
<soap12:operation soapAction="urn:getCategoryObject" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="HelloWorldWSHttpBinding" type="ns:HelloWorldWSPortType">
<http:binding verb="POST"/>
<wsdl:operation name="getCategoryObject">
<http:operation location="getCategoryObject"/>
<wsdl:input>
<mime:content type="text/xml" part="parameters"/>
</wsdl:input>
<wsdl:output>
<mime:content type="text/xml" part="parameters"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorldWS">
<wsdl:port name="HelloWorldWSHttpSoap11Endpoint" binding="ns:HelloWorldWSSoap11Binding">
<soap:address location="http://localhost:8080/WebProject_KomplexObjects/services/HelloWorldWS.HelloWorldWSHttpSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="HelloWorldWSHttpSoap12Endpoint" binding="ns:HelloWorldWSSoap12Binding">
<soap12:address location="http://localhost:8080/WebProject_KomplexObjects/services/HelloWorldWS.HelloWorldWSHttpSoap12Endpoint/"/>
</wsdl:port>
<wsdl:port name="HelloWorldWSHttpEndpoint" binding="ns:HelloWorldWSHttpBinding">
<http:address location="http://localhost:8080/WebProject_KomplexObjects/services/HelloWorldWS.HelloWorldWSHttpEndpoint/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

这是我的 Android 测试应用程序:

    package de.bachelor.marcel;

    import java.util.Vector;

    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.PropertyInfo;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.AndroidHttpTransport;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;

    public class WSClientKomplexeObjekteActivity extends Activity {

     private static final String SOAP_ACTION = "http://ws.chathura.com/getCategoryObject";
        private static final String METHOD_NAME = "getCategoryObject";
        private static final String NAMESPACE = "http://ws.chathura.com";
        private static final String URL = "http://192.168.2.102:8080/WebProject_KomplexObjects/services/HelloWorldWS?wsdl";

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            /*
             * Create Category with Id to be passed as an argument
             * 
             * */
            Category C = new Category();
            C.CategoryId = 1;
            C.Description = "Das ist die Beschreibung";
            C.Name = "ObjektName"; 

            /*
             * Set the category to be the argument of the web service method
             * 
             * */

            PropertyInfo objekt = new PropertyInfo();
            objekt.setName("obj");
            objekt.setValue(C);
            objekt.setType(C.getClass());

            Request.addProperty(objekt);

            /*
             * Set the web service envelope
             * 
             * */
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

            envelope.setOutputSoapObject(Request);

            envelope.addMapping(NAMESPACE, "Category",new Category().getClass());
            AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
            /*
             * Call the web service and retrieve result ... how luvly <3
             * 
             * */
            try
            {
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapObject response = (SoapObject)envelope.getResponse();
                C.CategoryId =  Integer.parseInt(response.getProperty(0).toString());
                C.Name =  response.getProperty(1).toString();
                C.Description = (String) response.getProperty(2).toString();
                TextView tv = (TextView)findViewById(R.id.textview1);
                tv.setText("CategoryId: " +C.CategoryId + " Name: " + C.Name + " Description " + C.Description);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }

这是来自 Android 客户端的类别类:

package de.bachelor.marcel;

import java.util.Hashtable;

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

public class Category implements KvmSerializable 
{

 public int CategoryId;
 public String Description;
    public String Name;

    public Category(){}


    public Category(int categoryId, String description, String name) 
    {

        CategoryId = categoryId;
        Name = name;
        Description = description;
    }

 @Override
 public Object getProperty(int arg0) 
 {
  switch(arg0)
        {
        case 0:
            return CategoryId;
        case 1:
            return Description;
        case 2:
         return Name;
        }
  return null;
 }

 @Override
 public int getPropertyCount() 
 {
  return 3;
 }

 @Override
 public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) 
 {
  switch(arg0)
        {
        case 0:
            arg2.type = PropertyInfo.INTEGER_CLASS;
            arg2.name = "CategoryId";
            break;
        case 1:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name = "Description";
            break;
        case 2:
            arg2.type = PropertyInfo.STRING_CLASS;
            arg2.name = "Name";
            break;
        default:break;
        }

 }

 @Override
 public void setProperty(int arg0, Object arg1) 
 {
  switch(arg0)
        {
        case 0:
            CategoryId = Integer.parseInt(arg1.toString());
            break;
        case 1:
         Description = arg1.toString();
            break;
        case 2:
            Name = arg1.toString();
            break;
        default:
            break;
        }

 }

}

【问题讨论】:

标签: java android web-services ksoap2


【解决方案1】:

尝试使用

private static final String NAMESPACE = "http://ws.chathura.com/xsd";

在您的 android 活动中,并将 getter 和 setter 添加到客户端中的 Category 类中。

【讨论】:

  • 好的,但是如果我只使用这个命名空间,我会得到一个异常:[错误] 尝试调用服务方法 getTrank org.apache.axis2.AxisFault 时发生异常:命名空间不匹配需要 ws.chathura.com在 org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessage‌​Receiver.java: 117)——
  • @enrmarc 你能回答*.com/questions/29345970/…
【解决方案2】:

好的,我正在尝试使用您的提示,现在我非常接近解决方案:

我发现,我需要 2 个命名空间(一个用于接收,一个用于发送)

所以我做了以下更改:

private static final String NAMESPACE = "http://ws.chathura.com";
private static final String NAMESPACE2 = "http://ws.chathura.com/xsd";

还有这个:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
...
...
envelope.addMapping(NAMESPACE2, "Category",new Category().getClass());

现在我的 Web 服务接收到我的对象 Category,但 Category (CategoryID) 的第一个属性未传输,LogCat 显示以下异常:

 06-13 09:59:17.459: W/System.err(647): java.lang.RuntimeException: Unknown Property: categoryId
06-13 09:59:17.469: W/System.err(647):  at org.ksoap2.serialization.SoapSerializationEnvelope.readSerializable(SoapSerializationEnvelope.java:200)
06-13 09:59:17.469: W/System.err(647):  at org.ksoap2.serialization.SoapSerializationEnvelope.readInstance(SoapSerializationEnvelope.java:444)
06-13 09:59:17.469: W/System.err(647):  at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:387)
06-13 09:59:17.469: W/System.err(647):  at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273)
06-13 09:59:17.469: W/System.err(647):  at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389)
06-13 09:59:17.469: W/System.err(647):  at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:151)
06-13 09:59:17.469: W/System.err(647):  at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140)
06-13 09:59:17.469: W/System.err(647):  at org.ksoap2.transport.Transport.parseResponse(Transport.java:100)
06-13 09:59:17.469: W/System.err(647):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:197)
06-13 09:59:17.479: W/System.err(647):  at com.androidclient.ws.WSClientActivity$1$1.run(WSClientActivity.java:91)
06-13 09:59:17.479: W/System.err(647):  at java.lang.Thread.run(Thread.java:1019)

我在没有CategoryID 的情况下对其进行了第二次测试,但是Description 是未知属性:-(

怎么了?

编辑:

我认为例外是因为envelope.addMapping 中的命名空间不同。我能做什么,只使用一个命名空间?

【讨论】:

  • 试试这个网址:private static final String URL = "http://192.168.2.102:8080/WebProject_KomplexObjects/services/HelloWorldWS"
  • 不,这不是问题,我添加了这个:envelope.avoidExceptionForUnknownProperty=true; 现在例外是:java.lang.ClassCastException:de.bachelor.marcel.Category。我认为这是从铸件到 SoabObject 的问题。像这里一样的问题:*.com/questions/11037852/…
  • 接收复杂对象:Category c = null;尝试 { transport.call(NAMESPACE + METHOD_NAME, 信封); c = (Category)envelope.getResponse() } catch (Exception e){}
  • 这不起作用 TomCat 显示:java.lang.ClassCastException: org.ksoap2.serialization.SoapObject 。所以我认为返回的对象Category 现在没有正确序列化。