[英]Calling java webservice from Android using ksoap2: parameters are all null
I'm using the ksoap2-android
library to call a java jax-ws
webservice from an Android application. 我正在使用ksoap2-android
库从Android应用程序调用Java jax-ws
。
The problem is that while the method I request is actually called correctly, all its parameters (which are all simple strings) are always null
! 问题是,虽然我请求的方法实际上被正确调用,但其所有参数(都是简单字符串)始终为null
!
Here's my code: 这是我的代码:
jax-ws webservice: jax-ws网络服务:
@WebService(targetNamespace="webservices.delta.elia.unipd")
public class DeltaUploadService {
@WebMethod(operationName = "uploadLogData")
public boolean uploadLogData(String deviceID, String experimentID, String filename, String data){
System.out.println("Incoming data from device: " + deviceID + ", Experiment ID: " + experimentID);
//All the parameters here are NULL!
}
}
Android code: Android代码:
public static boolean uploadLog(String deviceID, String experimentID, String filename, String serverUrl, byte[] data){
String NAMESPACE = "webservices.delta.elia.unipd";
String SOAP_ACTION = "webservices.delta.elia.unipd/uploadLogData";
// Create request
SoapObject request = new SoapObject(NAMESPACE, "uploadLogData");
//add parameters
request.addProperty("deviceID", deviceID);
request.addProperty("experimentID", experimentID);
request.addProperty("filename", filename);
request.addProperty("data", Base64.encodeToString(data, Base64.DEFAULT));
// Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// Set output SOAP object
envelope.setOutputSoapObject(request);
// Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(serverUrl);
try {
// Invoke web service
androidHttpTransport.call(SOAP_ACTION, envelope);
// Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
return Boolean.parseBoolean(response.toString());
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
What am I doing wrong? 我究竟做错了什么?
PS: I tested the Java web service with the SoapUI tool and it works correctly, so the issue must be in either ksoap2 library or my Android code. PS:我用SoapUI工具测试了Java Web服务,它可以正常工作,因此问题必须出在ksoap2库或我的Android代码中。
Note: I've already read the other answers like ksoap2 - request parameters is set null and KSoap calling .NET ASMX service passing null arguments but they didn't help. 注意:我已经阅读了其他答案,例如ksoap2-请求参数设置为null,并且KSoap调用.NET ASMX服务传递了null参数,但它们没有帮助。
Well, after hours of changing namespaces/method names/annotations etc, I found the problem by comparing the request generated by soapUI and the one that was arriving at my server. 好了,经过数小时的更改命名空间/方法名称/注释等之后,我通过比较soapUI生成的请求和到达我的服务器的请求发现了问题。
The problem lies in the property names. 问题出在属性名称上。 Setting them the same as the parameter names in the jax-ws webservice doesn't work unless you specify those names with the correct annotations (something I didn't find in ANY example on the web, weirdly enough) 除非您使用正确的注释指定了这些名称,否则将它们与jax-ws网络服务中的参数名称设置相同是不可行的(奇怪的是,我在网络上的任何示例中都找不到这种名称)
Here's the correct web service code to make it work: 这是使其正常工作的正确Web服务代码:
@WebService(targetNamespace="webservices.delta.elia.unipd")
public class DeltaUploadService {
@WebMethod(operationName = "uploadLogData")
public boolean uploadLogData(@WebParam(name = "deviceID") String deviceID, @WebParam(name = "experimentID") String experimentID,
@WebParam(name = "filename") String filename, @WebParam(name = "data") String data){
System.out.println("Incoming data from device: " + deviceID + ", Experiment ID: " + experimentID);
//All the parameters now work!
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.