Android WSDL 网络服务 ksoap2 [英] Android WSDL Web Service ksoap2

查看:16
本文介绍了Android WSDL 网络服务 ksoap2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ksoap2 库通过 Android 设备连接到 Web 服务.我已经让它在两种不同的服务上正常工作,但现在我遇到了问题.每当我传递一个对象而不是原始类型时,它都会给我一个错误.我已经使用 KvmSerializable 实现了这些类,但仍然没有运气.这是从正常工作的 Web 浏览器发送的请求的 xml,以及从不起作用的 Android 设备发送的请求的 xml.任何建议都会很棒!

I am trying to connect to a web service via an Android device by using the ksoap2 library. I've gotten it to work on two different services fine, but now I've ran into a problem. Whenever I pass an object instead of a primitive type it gives me an error. I've implemented the classes using KvmSerializable and still no luck. Here is the xml of the request sent from a web browser that works, and the xml of the request sent from the Android device that doesn't work. Any suggestions at all would be great!

浏览器

     <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
    <CreateTour xmlns="http://tempuri.org/">
        <SecurityKey>2b9fac80-97ce-4483-b9e8-c1816656e1db</SecurityKey>
        <OriginDetails xmlns:d4p1="http://schemas.datacontract.org/2004/07/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <d4p1:Creator>3</d4p1:Creator>
            <d4p1:CreatorOS>1</d4p1:CreatorOS>
            <d4p1:CreatorVersion>1.0.0.18191</d4p1:CreatorVersion>
            <d4p1:IP>192.168.1.61</d4p1:IP>
            <d4p1:UserAgent>Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7</d4p1:UserAgent>
        </OriginDetails>
        <Details xmlns:d4p1="http://schemas.datacontract.org/2004/07/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <d4p1:Baths>1.00</d4p1:Baths>
            <d4p1:Beds>6</d4p1:Beds>
            <d4p1:City>Knoxville</d4p1:City>
            <d4p1:Country>US</d4p1:Country>
            <d4p1:CreatedDate>2012-01-24T17:50:09.0541149-05:00</d4p1:CreatedDate>
            <d4p1:Description>Another test</d4p1:Description>
            <d4p1:Garage>9</d4p1:Garage>
            <d4p1:MLSID>400</d4p1:MLSID>
            <d4p1:ModifiedDate>2012-01-24T17:50:09.0541149-05:00</d4p1:ModifiedDate>
            <d4p1:MusicFile>/music/magic.mp3</d4p1:MusicFile>
            <d4p1:Price>400</d4p1:Price>
            <d4p1:PropertyType>0</d4p1:PropertyType>
            <d4p1:SF>400</d4p1:SF>
            <d4p1:Schools i:nil="true" />
            <d4p1:ShowAddress>true</d4p1:ShowAddress>
            <d4p1:ShowPrice>true</d4p1:ShowPrice>
            <d4p1:State>TN</d4p1:State>
            <d4p1:Status i:nil="true" />
            <d4p1:Street>9111 Cross Park Drive</d4p1:Street>
            <d4p1:Title>Test</d4p1:Title>
            <d4p1:TourID i:nil="true" />
            <d4p1:YearBuilt i:nil="true" />
            <d4p1:Zip>37923</d4p1:Zip>
        </Details>
    </CreateTour>
</s:Body>

安卓

    <v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body>
    <CreateTour xmlns="http://tempuri.org/" id="o0" c:root="1">
        <SecurityKey i:type="d:string">641c38a3-b290-45db-b2d4-8c4edac5934a</SecurityKey>
        <OriginDetails i:type="d:anyType">
            <Creator i:type="d:int">4</Creator>
            <CreatorOS i:type="d:int">6</CreatorOS>
            <CreatorVersion i:type="d:string">1.0</CreatorVersion>
            <IP i:type="d:string">192.168.1.222</IP>
            <UserAgent i:type="d:string"></UserAgent>
        </OriginDetails>
        <Details i:type="d:anyType">
            <Baths i:type="d:int">1</Baths>
            <Beds i:type="d:int">2</Beds>
            <City i:type="d:string">Cleveland</City>
            <Country i:type="d:string">US</Country>
            <CreatedDate i:type="d:string"></CreatedDate>
            <Description i:type="d:string"></Description>
            <Garage i:type="d:int">2</Garage>
            <MLSID i:type="d:string">44</MLSID>
            <ModifiedDate i:type="d:string"></ModifiedDate
            ><MusicFile i:type="d:string"></MusicFile>
            <Price i:type="d:int">400</Price>
            <PropertyType i:type="d:int">0</PropertyType>
            <SF i:type="d:int">777</SF>
            <Schools i:type="d:string"></Schools>
            <ShowAddress i:type="d:boolean">true</ShowAddress>
            <ShowPrice i:type="d:boolean">true</ShowPrice>
            <State i:type="d:string">TN</State>
            <Status i:type="d:int">0</Status>
            <Street i:type="d:string">200 Post Road</Street>
            <Title i:type="d:string">Android Test Tour</Title>
            <TourID i:type="d:int">0</TourID>
            <YearBuilt i:type="d:int">0</YearBuilt>
            <Zip i:type="d:string">37362</Zip>
        </Details>          
    </CreateTour>
</v:Body>

推荐答案

我过去也遇到过类似的问题.我会给你一个我必须在我的应用程序中修复的部分的例子以及我是如何做的,检查你是否在你的情况下执行了这些步骤:
在 wsdl 中有:

I had similar problems in the past. I'll give you an example of a part i had to fix in my application and how i did, check if you did these steps in your case:
In the wsdl there was:

<message name="invokeService">
     <part name="serviceName" type="xsd:string"/>
     <part name="documents" type="tns:uriList"/>
     <part name="literalDocs" type="ns1:stringArray"/>
     <part name="connID" type="xsd:long"/>
     <part name="gateParams" type="tns:gateRuntimeParameterArray"/>
     <part name="userCtx" type="tns:userContext"/>
</message>


所以我必须按照以下步骤操作:
1- 检查您实现 KvmSerializable 的类是否被准确定义并且没有遗漏任何东西(这对于复杂类型至关重要)


So I had to follow these steps:
1- check if your classes implementing KvmSerializable are accurately defined and not missing anything (this is cruciale for complex types)

2-添加您需要的所有必要的soap对象属性,例如在我的情况下:

2-Add ALL the necessary soap object properties that you need, for example in my case :

//for the part: <part name="serviceName" type="xsd:string"/>
SoapObject.addProperty("serviceName", whateverServiceNameItWas);

//for the part: <part name="documents" type="tns:uriList"/>,where uriList was a complex type
PropertyInfo pi = new PropertyInfo();
pi.setName("documents");
pi.setValue(usrOptPr.getDocuments());
pi.setType(UriList.class);
sobj.addProperty(pi);

等等...3-构建信封:

etc... 3-Build the enveloppe:

SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.setOutputSoapObject(SoapObject);

4- 添加复杂类型之间的映射(即实现 kvmserializable 的本地类,以及 Web 服务上真正匹配的类)

4- Add mappings between complex types(ie local class that implement kvmserializable, and the real matching classes on the web service)

//---------------------------------------------------------------------------------------
// MAPPINGS:
// A mapping tells the ksoap what class to generate.
// Complex data types that are not mapped are generated as SoapObjects.
// The mapping is required for both the request and the response.
 //---------------------------------------------------------------------------------------

//for example the UriList above     
soapEnvelope.addMapping(theNamespace, UriList.class.getSimpleName(), UriList.class);

5- 添加编组:(编组使用 java 序列化将对象更改为要在 Web 服务上解组的数据流.)

5- Add marshalling:(Marshalling uses java serialization to change Objects to stream of data to be unmarshalled on the web service.)

Marshal floatMarshal = new MarshalFloat();
floatMarshal.register(soapEnvelope);

6- 使用 AndroidHttpTransport 调用网络服务
更新
我还注意到您在浏览器中的请求:

6- Use AndroidHttpTransport to call the web service
UPDATE
I also noticed that you have in the browser request:

<d4p1:Schools i:nil="true" />

在 Android 中:

While in Android:

<Schools i:type="d:string"></Schools>

有时 ksoap2 会在这种情况下出现错误,我有同样的情况,所以我所做的就是从实现 kvmserializable (当然,您必须修改类中的其他内容,例如getPropertyCount"和getPropertyInfo"以适应此更改).当我这样做时,它起作用了,所以请尝试并告诉我.

Sometimes ksoap2 bugs with such a scenario, i had the same case so what i did is i just removed (commented out, since it allows nil ie null values) this param (ie schools) from its specific class that implements kvmserializable ( of course you will have to modify other stuff in the class like "getPropertyCount" and "getPropertyInfo" to adapt to this change). When i did that it worked, so try that and let me know.

这篇关于Android WSDL 网络服务 ksoap2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆