【发布时间】:2015-03-28 13:42:47
【问题描述】:
我编写了一个 Axis 2 Java Web 服务。在这个网络服务中,有一种方法叫做“插入条目”。
我想从 Android 解析我自己的对象到 web 服务,但是关于发送过程,我还需要了解更多。
网络服务方法
public int insertEntry(Object entry)
{
return 1;
}
来自 web 服务的类正在实现可序列化
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 的默认请求是这个 - 这很完美 -->
<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 创建这样的请求? :/
【问题讨论】:
-
非常非常漂亮的代码演示。喜欢你编写代码的方式。
标签: android axis2 android-ksoap2