2015-06-08 105 views
1

我正在使用ksoap2-android庫從Android應用程序調用java web服務。使用kso​​ap2從Android調用java web服務:參數全爲空

問題是,雖然我請求的方法實際上是正確調用的,但它的所有參數(都是簡單字符串)始終是null

這裏是我的代碼:

JAX-WS Web服務:

@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

好了,經過幾小時的命名空間/方法名稱/註釋等的改變,我發現通過比較由soapUI生成的請求和到達我的服務器的請求的問題。

問題在於屬性名稱。將它們設置爲與jax-ws webservice中的參數名稱相同是行不通的,除非您使用正確的註釋指定了這些名稱(我在網上的任何示例中都沒有找到這種名稱,足夠奇怪)

這裏是正確的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! 
    } 
} 
相關問題