我正在尝试使用.net网络服务中的方法。
Web服务背后的代码在命名空间的末尾有一个'/'
[WebService(Namespace = "http://www.mynamespace.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
这是.net方法调用
POST /TelematicsWebService/Service.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.mynamespace.com/GetDevicesByBranch"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetDevicesByBranch xmlns="http://www.mynamespace.com/">
<branchNumber>int</branchNumber>
</GetDevicesByBranch>
</soap:Body>
</soap:Envelope>
现在调用该方法的代码如下所示
@Override
protected SoapObject doInBackground(Integer... branchNumber) {
final String NAMESPACE = "http://www.mynamespace.com/";
final String METHOD_NAME = "GetDevicesByBranch";
final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
final String URL = "http://10.0.2.2/TelematicsWebService/Service.asmx";
SoapObject response = null;
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo inputArgs = new PropertyInfo();
inputArgs.setName("branchNumber");
inputArgs.setValue(branchNumber);
inputArgs.setType(Integer.class);
request.addProperty(inputArgs);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,
25000);
androidHttpTransport.call(SOAP_ACTION, envelope);
response = (SoapObject) envelope.getResponse();
} catch (Exception exception) {
response = null;
}
return response;
}
结尾处的行androidHttpTransport.call(SOAP_ACTION, envelope);
会抛出异常:java.lang.RuntimeException: Cannot serialize: [Ljava.lang.Integer;@412e0cf0
我找不到解决这个问题的方法,有什么建议吗?
答案 0 :(得分:7)
我明白了......
如果使用Override doInBackground()方法已经很明显,那么这是在AsyncTask&lt;&gt;()中。输入参数是Integer...
类型。
protected SoapObject doInBackground(Integer... branchNumber)
我不知道省略号究竟是什么,但我知道它使参数成为一个数组。
我将整数数组传递给请求对象而不是特定的数组元素。
从
改变request.addProperty("branchNumber", branchNumber);
到
request.addProperty("branchNumber", branchNumber[0]);
解决了这个问题。
谢谢@Paul-Jan和@beginner!我很感激你的答案!
答案 1 :(得分:0)
而不是
PropertyInfo inputArgs = new PropertyInfo();
inputArgs.setName("branchNumber");
inputArgs.setValue(branchNumber);
inputArgs.setType(Integer.class);
request.addProperty(inputArgs);
试试这个:
request.addProperty("branchNumber", branchNumber);
答案 2 :(得分:0)
对于那些想要添加Object列表作为Web服务参数的人:
List<Object> vect = new Vector<Object>();
vect.add("Element1");
vect.add("Element2");
vect.add("Element3");
PropertyInfo tabProp =new PropertyInfo();
tabProp.setName("LIST_PARAM");
tabProp.setValue(vect);
request.addProperty(tabProp );
当然,没有必要注意Async Task必须实现该方法:
protected String doInBackground(List<Object>... params){
....
....
}