kSOAP 2 Android - 将复杂类型发送到Java Axis2 Webservice

时间:2015-01-29 10:53:30

标签: android axis2 android-ksoap2

我编写了一个Axis 2 Java Web服务。在这个Web服务中,有一个名为“insert Entry”的方法。

从Android我想将我自己的对象解析为web服务,但还有一点我必须知道发送过程。

Web服务方法

public int insertEntry(Object entry)
{
    return 1;
}

来自webservice的类正在实现可序列化的

Android中的条目对象

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

import java.util.Hashtable;

public class Entry implements KvmSerializable {

private String destination_appliance;
private String contact;
private String card;
private String tariff;
private String tara_in;
private String reference;

public Entry(String destination_appliance, String contact, String card, String tariff, String tara_in, String reference)
{
    this.destination_appliance = destination_appliance;
    this.contact = contact;
    this.card = card;
    this.tariff = tariff;
    this.tara_in = tara_in;
    this.reference = reference;
}

public String getDestinationAppliance() {
    return destination_appliance;
}

public void setDestinationAppliance(String destination_appliance) {
    this.destination_appliance = destination_appliance;
}

public String getContact() {
    return contact;
}

public void setContact(String contact) {
    this.contact = contact;
}

public String getCard() {
    return card;
}

public void setCard(String card) {
    this.card = card;
}

public String getTariff() {
    return tariff;
}

public void setTariff(String tariff) {
    this.tariff = tariff;
}

public String getTaraIn() {
    return tara_in;
}

public void setTaraIn(String tara_in) {
    this.tara_in = tara_in;
}

public String getReference() {
    return reference;
}

public void setReference(String reference) {
    this.reference = reference;
}

@Override
public Object getProperty(int pid) {
    switch (pid) {
        case 0:
            return this.destination_appliance;

        case 1:
            return this.contact;

        case 2:
            return this.card;

        case 3:
            return this.tariff;

        case 4:
            return this.tara_in;

        case 5:
            return this.reference;

        default:
            break;
    }

    return null;
}

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

@Override
public void getPropertyInfo(int index, Hashtable htable, PropertyInfo info) {
    switch (index) {
        case 0:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "destination_appliance";
            break;

        case 1:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "contact";
            break;
        case 2:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "card";
            break;

        case 3:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "tariff";
            break;

        case 4:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "tara_in";
            break;

        case 5:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "reference";
            break;

    }

}

@Override
public String getInnerText() {
    return null;
}

@Override
public void setInnerText(String s) {

}

@Override
public void setProperty(int index, Object value) {
    switch (index) {
        case 0:
            this.destination_appliance = value.toString();
            break;
        case 1:
            this.contact = value.toString();
            break;
        case 2:
            this.card = value.toString();
            break;
        case 3:
            this.tariff = value.toString();
            break;
        case 4:
            this.tara_in = value.toString();
            break;
        case 5:
            this.reference = value.toString();
            break;
    }
}
}

我的AsyncTask的doBackground方法

private class InsertEntryAsyncTask extends AsyncTask<Object, Void, Integer> {

    private String resp;
    private KSoapHandler kSoap = new KSoapHandler();
    private static final String METHOD_NAME = "insertEntry";
    private static final String SOAP_ACTION = "XXXX/" + METHOD_NAME;

    @Override
    protected Integer doInBackground(Object... params) {

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

        PropertyInfo prop = new PropertyInfo();

        prop.setName("Entry");
        prop.setValue(params[0]);
        prop.setType(params[0].getClass());
        request.addProperty(prop);

        envelope.setOutputSoapObject(request);

        HttpTransportSE transport = new HttpTransportSE(kSoap.getURL());

        try {
            transport.call(kSoap.getNAMESPACE() + kSoap.getSOAP_ACTION_PREFIX() + METHOD_NAME, envelope);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }

        try {
            SoapObject response = (SoapObject) envelope.getResponse();


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return 1;
    }

    @Override
    protected void onPostExecute(Integer response) {
    }
}

从AsyncTask执行调用

Entry entry = new Entry("Test","0","0","0","Test", "Test");

InsertEntryAsyncTask asyncTaskInsertEntry = new InsertEntryAsyncTask();

asyncTaskInsertEntry.execute(entry);

请告诉我如何将条目对象发送到Axis2 Web服务。

这是正确的方法还是代码中有一些错误?!

谢谢你的帮助!

最好的问候佛罗伦斯

-----更新----

目前,来自Android的请求是:

<?xml version="1.0" encoding="UTF-8"?>
<v:Envelope xmlns:v="http://schemas.xmlsoap.org/soap/envelope/" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   <v:Header />
   <v:Body>
      <n0:insertEntry xmlns:n0="http://wmswebservice.development.ais.at" id="o0" c:root="1">
         <E i:type="n0:Entry">
            <destinationAppliance i:type="d:string">021</destinationAppliance>
            <contact i:type="d:string">0</contact>
            <card i:type="d:string">0</card>
            <tariff i:type="d:string">0</tariff>
            <taraIn i:type="d:string">12</taraIn>
            <reference i:type="d:string">KR</reference>
         </E>
      </n0:insertEntry>
   </v:Body>
</v:Envelope>

SoapUI的默认请求就是这个 - 这完美无缺 - &gt;

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wms="http://wmswebservice.development.ais.at" xmlns:xsd="http://model.wmswebservice.development.ais.at/xsd">
   <soapenv:Header/>
   <soapenv:Body>
      <wms:insertEntry>
         <!--Optional:-->
         <wms:E>
            <!--Optional:-->
            <xsd:card>?</xsd:card>
            <!--Optional:-->
            <xsd:contact>?</xsd:contact>
            <!--Optional:-->
            <xsd:destinationAppliance>?</xsd:destinationAppliance>
            <!--Optional:-->
            <xsd:reference>?</xsd:reference>
            <!--Optional:-->
            <xsd:taraIn>?</xsd:taraIn>
            <!--Optional:-->
            <xsd:tariff>?</xsd:tariff>
         </wms:E>
      </wms:insertEntry>
   </soapenv:Body>
</soapenv:Envelope>

那么如何使用kSoap2创建这样的请求? :/

2 个答案:

答案 0 :(得分:0)

您必须使用XML或JSON serialize您的对象才能发送它们。 WebService的概念能够与使用不同语言编写的应用程序进行通信,因此协议不了解Java对象。

希望这会让你走上正确的道路。

答案 1 :(得分:0)

您尚未通过addMapping在信封中注册您的参赛作品。此外,你的代码工作得很好。添加映射(是的,我使用模拟映射asdf.com):

envelope.addMapping("http://asdf.com", "Entry", Entry.class);

它产生了很好的信封:

<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>
        <n0:insertEntry id="o0" c:root="1" xmlns:n0="http://asdf.com">
            <Entry i:type="n0:Entry">
                <destination_appliance i:type="d:string">Test</destination_appliance>
                <contact i:type="d:string">0</contact>
                <card i:type="d:string">0</card>
                <tariff i:type="d:string">0</tariff>
                <tara_in i:type="d:string">Test</tara_in>
                <reference i:type="d:string">Test</reference>
            </Entry>
        </n0:insertEntry>
    </v:Body>
</v:Envelope>

什么是问题 - 我不知道你的网络服务是否需要它。安装SoapUI并通过调用您的WS进行一些操作,您将看到您需要的内容。 在transport.call之前添加标记transport.debug = true,并打印到日志等请求转储 - 它可以帮助您查看您的内容和内容。

嗯 - 添加映射的代码应该根据我的情况对你的WS进行适当的调用&#34;&#34;。

第2部分

您在服务器端WS接口声明为

public int insertEntry(Object entry)
{
    return 1;
}

正如我在评论中提到的,您可以删除addMapping。然后请求将不携带i:type =&#34; n0:Entry&#34;,但i:type =&#34; anyType&#34;它可能是 - WS消耗请求。

其他:第3部分。

我对Axis WS有一点了解。我只使用Axis的客户。但是......一般来说 - 使用Entry类型(i:type =&#34; no:Entry&#34;)你必须将接口声明为

public int insertEntry(Entry entry)
{
    return 1;
}

其中Entry将是服务器端的附加类。这将是类似于您的Android代码中类似的小POJO:

public class Entry {
public String destination_appliance;
public String contact;
public String card;
public String tariff;
public String tara_in;
public String reference;
}

据我所知,Axis将构建这样的WS。但是如何做到这一点我不知道 - 以谷歌叔叔为例。 &#34; Axis webservice复合函数参数&#34;。

或者让一些Axis专业人士告诉我们:)

第4部分。

好的,所以 - 删除addMapping并添加envelope.implicitTypes = true。如果它仍然不正常,那么(但只有这样)在属性Entry上添加具有setNamespace的行。

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.implicitTypes=true;
SoapObject request = new SoapObject(kSoap.getNAMESPACE(), METHOD_NAME);

PropertyInfo prop = new PropertyInfo();

prop.setName("Entry");
prop.setValue(params[0]);
prop.setType(params[0].getClass());
prop.setNamespace(kSoap.getNAMESPACE());
request.addProperty(prop);
相关问题