使用kso​​ap2从Android调用java webservice:参数都为null

时间:2015-06-08 09:54:23

标签: java android web-services jax-ws android-ksoap2

我正在使用ksoap2-android库从Android应用程序调用java jax-ws webservice。

问题是虽然我请求的方法实际上被正确调用,但它的所有参数(都是简单的字符串)总是null

这是我的代码:

jax-ws webservice:

@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代码:

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;
    }
}

我做错了什么?

PS:我使用SoapUI工具测试了Java Web服务,它运行正常,所以问题必须在ksoap2库或我的Android代码中。

注意:我已经阅读了其他答案,例如ksoap2 - request parameters is set nullKSoap calling .NET ASMX service passing null arguments,但他们没有帮助。

1 个答案:

答案 0 :(得分:1)

好吧,经过数小时更改名称空间/方法名称/注释等后,我通过比较soapUI生成的请求和到达我服务器的请求找到了问题。

问题在于属性名称。将它们设置为与jax-ws webservice中的参数名称相同不起作用,除非您使用正确的注释指定这些名称(我在Web上的任何示例中都找不到,奇怪的是)

以下是使其正常运行的正确的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!
    }
}