KSoap2 和 KvmSerialized - 如何发送复杂的对象,如 Stringarrays

2023-12-05

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

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

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

[错误] 尝试调用服务方法时发生异常 getCategoryObject org.apache.axis2.AxisFault:未知类型 {http://ws.chathura.com}类别Logcat显示以下内容:06-12 11:08:57.576:W/System.err(777):SoapFault - 故障代码: 'soapenv:服务器'错误字符串:'未知类型 {http://ws.chathura.com}类别'错误因素:'null'详细信息: org.kxml2.kdom.Node@4054a528

这是我在 Tomcat 7 上运行的 Java Web 服务:

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;
 }
}

这是我在 Web 服务器上的类别类:

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;
        }

 }

}

尝试使用

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

在您的 Android Activity 中,并将 getter 和 setter 添加到您的Category客户端中的类。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

KSoap2 和 KvmSerialized - 如何发送复杂的对象,如 Stringarrays 的相关文章

随机推荐

  • Vagrant 错误:无法在 Linux 来宾中挂载文件夹

    我对 Vagrant 共享文件夹有一些问题 我的基本系统是 Ubuntu 13 10 桌面 我不明白为什么我会出现此错误 是配置不正确吗 是 NFS 问题还是 Virtualbox Guest Additions 问题 我尝试过使用不同的许
  • 当用户尝试在 vb6 中打开新实例时返回到已打开的应用程序

    假设用户将我的 Visual Basic 应用程序最小化到任务栏通知图标 现在我希望当用户打开一个新实例时 旧实例应该恢复 通常 创建单实例应用程序的策略是在应用程序初始化中添加一些代码来确定实例是否已在运行 如果是 它会获取主窗口的句柄
  • 对 d3.js 堆积条形图进行排序

    目前这个堆积条形图根据每个条形的总计从左到右排序 我还如何对每个单独的条形图进行排序 而不是按键对每个单独的条形图进行排序 即 5 岁以下位于底部 65 岁及以上位于顶部 而是按值 最大 对每个单独的条形图进行排序位于底部 最小位于顶部 例
  • Fabric.js 定义图像的边界/限制

    I ve set up a fabric js based t shirt simulator Everything works as expected except for one detail that the client would
  • 如何在 Java 应用程序中使用 Akka Actors?

    我想用阿卡演员在爪哇 我下载了akka 1 0 zip并添加了akka actor 1 0 jar到我在 Eclipse 中的 构建路径 然后我写了这个 Actor 类 package com example import akka act
  • 剪切工具滞后荧光笔

    我尝试在 C Winforms 中创建荧光笔 我使用的大部分代码都在这里像截图工具一样突出显示效果但当我尝试画得太快时 它就会滞后 不确定我做错了什么 这是我尝试过的 private void pictureBox1 Paint objec
  • Flutter - 从数据库中获取记录并显示在 ListView Builder 中

    我正在开发一个 Flutter 项目并使用 Sqflite 数据库 我已经设法将数据保存在数据库中 现在我试图根据表名从数据库中获取所有记录的列表 并将它们显示在 ListView builder 中 数据库助手 dart Future
  • 安装cordova时卡住了

    我正在按照cordova主页上的步骤操作 但第一步我很困惑 我输入了sudo npm install g cordova在终端 但它卡住了 loadDep Xtend 网络 这需要几个小时 但没有进一步的进展 实际上 我在中国 我想这可能是
  • Java Web 应用程序。春季启动。定位图像

    我最近解决了在这个社区中定位图像的问题 你可以看here 然而 有一个答案说 您的应用程序是 Spring Boot 应用程序 我认为您还可以尝试使用 Spring Boot 提供的设施来提供静态内容 不管怎样 你现在正在这样做 因为你正在
  • HTML5 canvas 类上的 jQuery getContext

    这段代码的工作原理
  • Numpy 数组的长整数形状

    如果我构造一个像这样的 numpy 矩阵 A array 1 2 3 4 5 6 然后输入A shape我得到结果 2L 3L 为什么我得到的形状格式很长 我可以重新启动一切 但仍然遇到同样的问题 据我所知 只有当我构造数组时才会遇到这个问
  • Rails Paperclip 只能用于图像吗?

    是否有任何 Rails 库用于管理与 ActiveRecord 连接的文件附件 I know 回形针 但它似乎主要适合图像 他们确实提到了音频和 pdf 文件github项目页面 但没有进一步解释不同文件类型的用法 属性如 style如果您
  • ARM直接内存操作

    ARM中有支持直接内存操作的指令吗 所以而不是 cc cc 100 mov r3 0 ldr r1 r3 0 add r1 r1 100 str r1 r3 0 它是否有类似或接近的东西 add r3 0 100 I know this i
  • MongoEngine 指定查询的读取首选项

    我正在使用 Mongo 2 6 Pymongo 2 7 2 和 Mongoengine 0 8 7 对于特定的读取查询 我想使用副本集的辅助副本 因此 正如 mongoengine 文档中所指定的here我的查询如下 from pymong
  • JVM 如何执行 Try catch finally 块

    根据 Java 语言规范 第 14 20 2 节 带有finally 块的try 语句首先执行try 块 然后就有一个选择 If execution of the try block completes normally then the
  • 根据国家/地区获取货币符号

    我有一个显示货币的 TextView 默认情况下 我的文本视图的文本是 0 00我怎样才能做到这样 根据用户选择而变化 我有以下代码 Locale locale new Locale en US Currency currency Curr
  • 类是结构体还是结构体指针

    如果我没记错的话 结构意味着对象 结构指针意味着指向对象的指针 对吗 在一篇文章中 它说类是结构 这意味着它们是对象 Filter the list of all classes The traditional definition of
  • 从 Facebook 获取电子邮件 - FB android SDK

    我使用以下代码从 Facebook 获取用户的电子邮件和姓名 protected void onCreate Bundle savedInstanceState Display page setContentView R layout ac
  • 致命错误:sys/socket.h:32位上没有这样的文件或目录

    当使用 32 位编译我的程序时 gcc m32 program c o program 我收到以下错误致命错误 sys socket h 没有这样的文件或目录 但与 gcc program c o program 效果很好 有什么解决方法吗
  • KSoap2 和 KvmSerialized - 如何发送复杂的对象,如 Stringarrays

    我想将一个复杂的对象从我的 Ksoap 客户端发送到我的 Web 服务 它是我的类别类别的一个对象 该代码基于本教程的示例 http seesharpgears blogspot de 2010 10 ksoap android web s