KSoap2和KvmSerializable - 如何发送类似Stringarrays复杂的对象 [英] KSoap2 and KvmSerializable - How to Send complex Objects like Stringarrays
问题描述
我想从我的KSOAP客户端发送一个复杂的对象,以我的web服务。这是我的类属的对象。在code是根据本教程的例子:
的http://seesharpgears.blogspot.de/2010/10/ksoap-android-web-service-tutorial-with.html
web服务正在运行,且WSDL文件显示类对象。但是,如果我发送一个类别对象,以我的肥皂web服务,以下错误显示出来:
试图调用服务方法发生[错误]异常 getCategoryObject org.apache.axis2.AxisFault:未知类型 {http://ws.chathura.com}类别的logcat显示了以下内容:06-12 11:08:57.576:W / System.err的(777):的SOAPFault - 故障code: soapenv:服务器在faultstring:未知类型 {http://ws.chathura.com}类别faultactor:'空'的细节: org.kxml2.kdom.Node@4054a528
这是我的Java Webservice的其他计算机上的Tomcat 7:
包com.chathura.ws;
进口java.util.Vector中;
公共类HelloWorldWS
{
公共范畴getCategoryObject(类别OBJ)
{
C1类=新类别();
C1.setCategoryId(1);
C1.setDescription(服务器描述);
C1.setName(服务器名称);
的System.out.println(obj.getCategoryId());
返回C1;
}
}
这是我的web服务器上的分类类别:
包com.chathura.ws;
公共类分类{
私人诠释的CategoryId;
私人字符串名称;
私人字符串描述;
/ **
* @返回的的categoryId
* /
公众诠释getCategoryId(){
返回的CategoryId;
}
/ **
*参数类别ID的的categoryId设置
* /
公共无效setCategoryId(INT的categoryId){
类别id =的categoryId;
}
/ **
返回:名称
* /
公共字符串的getName(){
返回名称;
}
/ **
* @参数命名的名称设置
* /
公共无效setname可以(字符串名称){
名称=名称;
}
/ **
返回:描述
* /
公共字符串getDescription(){
返回说明;
}
/ **
* @参数描述的描述来设置
* /
公共无效setDescription(字符串描述){
说明=描述;
}
}
这是WSDL文件:
此XML文件没有出现能与它相关的任何样式信息。文档树如下所示。
< WSDL:定义的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:哑剧=http://schemas.xmlsoap.org/wsdl / MIME /的xmlns:肥皂=http://schemas.xmlsoap.org/wsdl/soap/的xmlns:SOAP12 =http://schemas.xmlsoap.org/wsdl/soap12/的targetNamespace =HTTP:/ /ws.chathura.com">
< WSDL:文件>请此处&lt键入您的服务描述; / WSDL:文件>
< WSDL:类型>
< XS:模式attributeFormDefault =合格将elementFormDefault =合格的targetNamespace =http://ws.chathura.com/xsd>
< XS:复杂类型的名称=分类>
< XS:序列>
< XS:元素的minOccurs =0NAME =的categoryId类型=XS:INT/>
< XS:元素的minOccurs =0NAME =说明的nillable =真类型=XS:字符串/>
< XS:元素的minOccurs =0NAME =名的nillable =真类型=XS:字符串/>
< / XS:序列>
< / XS:复杂类型>
< / XS:模式>
< XS:模式的xmlns:AX22 =http://ws.chathura.com/xsdattributeFormDefault =合格将elementFormDefault =合格的targetNamespace =http://ws.chathura.com>
< XS:导入命名空间=http://ws.chathura.com/xsd/>
< XS:元素的名称=getCategoryObject>
< XS:复杂类型>
< XS:序列>
< XS:元素的minOccurs =0NAME =目标文件的nillable =真TYPE =AX21:类/>
< / XS:序列>
< / XS:复杂类型>
< / XS:组件>
< XS:元素的名称=getCategoryObjectResponse>
< XS:复杂类型>
< XS:序列>
< XS:元素的minOccurs =0NAME =回归的nillable =真TYPE =AX21:类/>
< / XS:序列>
< / XS:复杂类型>
< / XS:组件>
< / XS:模式>
< / WSDL:类型>
< WSDL:消息名称=getCategoryObjectRequest>
< WSDL:部分名称=参数元素=NS:getCategoryObject/>
< / WSDL:消息>
< WSDL:消息名称=getCategoryObjectResponse>
< WSDL:部分名称=参数元素=NS:getCategoryObjectResponse/>
< / WSDL:消息>
< WSDL:portType的名称=HelloWorldWSPortType>
< WSDL:操作名称=getCategoryObject>
< WSDL:输入消息=NS:getCategoryObjectRequestWSAW:行动=金塔:getCategoryObject/>
< WSDL:输出消息=NS:getCategoryObjectResponseWSAW:行动=金塔:getCategoryObjectResponse/>
< / WSDL:操作>
< / WSDL:端口类型>
< WSDL:绑定名称=HelloWorldWSSoap11BindingTYPE =NS:HelloWorldWSPortType>
<肥皂:绑定传输=http://schemas.xmlsoap.org/soap/http的风格=文件/>
< WSDL:操作名称=getCategoryObject>
<肥皂:操作的soapAction =金塔:getCategoryObject的风格=文件/>
< WSDL:输入>
<肥皂:身体使用=文字/>
< / WSDL:输入>
< WSDL:输出>
<肥皂:身体使用=文字/>
< / WSDL:输出>
< / WSDL:操作>
< / WSDL:绑定>
< WSDL:绑定名称=HelloWorldWSSoap12BindingTYPE =NS:HelloWorldWSPortType>
< SOAP12:绑定传输=http://schemas.xmlsoap.org/soap/http的风格=文件/>
< WSDL:操作名称=getCategoryObject>
< SOAP12:操作的soapAction =金塔:getCategoryObject的风格=文件/>
< WSDL:输入>
< SOAP12:身体使用=文字/>
< / WSDL:输入>
< WSDL:输出>
< SOAP12:身体使用=文字/>
< / WSDL:输出>
< / WSDL:操作>
< / WSDL:绑定>
< WSDL:绑定名称=HelloWorldWSHttpBindingTYPE =NS:HelloWorldWSPortType>
< HTTP:结合动词=POST/>
< WSDL:操作名称=getCategoryObject>
< HTTP:操作位置=getCategoryObject/>
< WSDL:输入>
<默:内容类型=文/ XML部分=参数/>
< / WSDL:输入>
< WSDL:输出>
<默:内容类型=文/ XML部分=参数/>
< / WSDL:输出>
< / WSDL:操作>
< / WSDL:绑定>
< WSDL:服务名称=HelloWorldWS>
< WSDL:端口名称=HelloWorldWSHttpSoap11Endpoint绑定=NS:HelloWorldWSSoap11Binding>
<肥皂:地址location="http://localhost:8080/WebProject_KomplexObjects/services/HelloWorldWS.HelloWorldWSHttpSoap11Endpoint/"/>
< / WSDL:端口>
< WSDL:端口名称=HelloWorldWSHttpSoap12Endpoint绑定=NS:HelloWorldWSSoap12Binding>
< SOAP12:地址location="http://localhost:8080/WebProject_KomplexObjects/services/HelloWorldWS.HelloWorldWSHttpSoap12Endpoint/"/>
< / WSDL:端口>
< WSDL:端口名称=HelloWorldWSHttpEndpoint绑定=NS:HelloWorldWSHttpBinding>
< HTTP:地址location="http://localhost:8080/WebProject_KomplexObjects/services/HelloWorldWS.HelloWorldWSHttpEndpoint/"/>
< / WSDL:端口>
< / WSDL:服务>
< / WSDL:定义>
这是我的Android测试应用程序内置:
包de.bachelor.marcel;
进口java.util.Vector中;
进口org.ksoap2.SoapEnvelope;
进口org.ksoap2.serialization.PropertyInfo;
进口org.ksoap2.serialization.SoapObject;
进口org.ksoap2.serialization.SoapSerializationEnvelope;
进口org.ksoap2.transport.AndroidHttpTransport;
进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.TextView;
公共类WSClientKomplexeObjekteActivity延伸活动{
私有静态最后弦乐SOAP_ACTION =http://ws.chathura.com/getCategoryObject;
私有静态最后弦乐METHOD_NAME =getCategoryObject;
私有静态最后弦乐NAMESPACE =http://ws.chathura.com;
私有静态最后字符串的URL =http://192.168.2.102:8080/WebProject_KomplexObjects/services/HelloWorldWS?wsdl;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
SoapObject请求=新SoapObject(命名空间METHOD_NAME);
/ *
* ID为创建类别将作为参数传递
*
* * /
C类=新类别();
C.CategoryId = 1;
C.Description =达斯北京时间死Beschreibung;
C.Name =ObjektName;
/ *
*设置的类别是Web服务方法的参数
*
* * /
的PropertyInfo OBJEKT =新的PropertyInfo();
objekt.setName(物镜);
objekt.setValue(C);
objekt.setType(C.getClass());
Request.addProperty(OBJEKT);
/ *
*设置Web服务包
*
* * /
SoapSerializationEnvelope包=新SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(请求);
envelope.addMapping(命名空间类别,新的类别()的getClass());
AndroidHttpTransport androidHttpTransport =新AndroidHttpTransport(URL);
/ *
*调用Web服务和检索结果......怎么luvly 3;
*
* * /
尝试
{
androidHttpTransport.call(SOAP_ACTION,包);
SoapObject响应=(SoapObject)envelope.getResponse();
C.CategoryId =的Integer.parseInt(response.getProperty(0)的ToString());
C.Name = response.getProperty(1)的ToString();
C.Description =(字符串)response.getProperty(2)的ToString();
TextView的电视=(TextView中)findViewById(R.id.textview1);
tv.setText(的CategoryId:+ C.CategoryId +姓名:+ C.Name +说明+ C.Description);
}
赶上(例外五)
{
e.printStackTrace();
}
}
}
这里是Android客户端的分类类别:
包de.bachelor.marcel;
进口的java.util.Hashtable;
进口org.ksoap2.serialization.KvmSerializable;
进口org.ksoap2.serialization.PropertyInfo;
公共类分类实现KvmSerializable
{
公众诠释的CategoryId;
公共字符串描述;
公共字符串名称;
公共类别(){}
公共范畴(INT的categoryId,字符串描述,字符串名称)
{
类别id =的categoryId;
名称=名称;
说明=描述;
}
@覆盖
公共对象的getProperty(INT为arg0)
{
开关(将arg0)
{
情况下0:
返回的CategoryId;
情况1:
返回说明;
案例2:
返回名称;
}
返回null;
}
@覆盖
公众诠释getPropertyCount()
{
返回3;
}
@覆盖
公共无效为getPropertyInfo(INT为arg0,哈希表ARG1,ARG2的PropertyInfo)
{
开关(将arg0)
{
情况下0:
arg2.type = PropertyInfo.INTEGER_CLASS;
arg2.name =的CategoryId;
打破;
情况1:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name =说明;
打破;
案例2:
arg2.type = PropertyInfo.STRING_CLASS;
arg2.name =名称;
打破;
默认值:打破;
}
}
@覆盖
公共无效的SetProperty(INT为arg0,对象ARG1)
{
开关(将arg0)
{
情况下0:
的CategoryId =的Integer.parseInt(arg1.toString());
打破;
情况1:
说明= arg1.toString();
打破;
案例2:
名称= arg1.toString();
打破;
默认:
打破;
}
}
}
尝试使用
私有静态最后弦乐NAMESPACE =http://ws.chathura.com/xsd;
在你的android活动,并在客户端中添加getter和setter方法,以你的类别
类。
I want to send a complex object from my Ksoap Client to my webservice. It’s an object of my Class Category. The Code is based on the example of this tutorial:
http://seesharpgears.blogspot.de/2010/10/ksoap-android-web-service-tutorial-with.html
The webservice is running and the WSDL-file shows the category object. But if I send a category object to my soap webservice, the following error is showing up:
[ERROR] Exception occurred while trying to invoke service method getCategoryObject org.apache.axis2.AxisFault: Unknow type {http://ws.chathura.com}Category Logcat shows up the following: 06-12 11:08:57.576: W/System.err(777): SoapFault - faultcode: 'soapenv:Server' faultstring: 'Unknow type {http://ws.chathura.com}Category' faultactor: 'null' detail: org.kxml2.kdom.Node@4054a528
This is my Java Webservice runnning on Tomcat 7:
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;
}
}
This is my Category Class on the 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;
}
}
This is the WSDL File:
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>
And this is my Android-Testing-App:
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();
}
}
}
And here is the Category Class from Android Client:
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;
}
}
}
Try using
private static final String NAMESPACE = "http://ws.chathura.com/xsd";
in your android activity and add the getters and setters to your Category
class in the client.
这篇关于KSoap2和KvmSerializable - 如何发送类似Stringarrays复杂的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!