简体   繁体   中英

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.

The problem is that while the method I request is actually called correctly, all its parameters (which are all simple strings) are always null !

Here's my code:

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 code:

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.

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.

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.

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)

Here's the correct web service code to make it work:

@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!
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM