2011-10-28 66 views
2

我正在開發一個示例應用程序來調用.Net Web服務。我已將ksoap2-j2me-core-prev-2.1.2.jar添加到Eclipse中的構建路徑中。「該方法不適用於參數」構建BlackBerry應用程序

我通過方法addProperty傳遞了兩個值:「數字1」和10整數,也是「數字2」和20這將導致一個編譯器錯誤:

The method addProperty(String, Object) in the type SoapObject is not applicable for the arguments (String, int)

我怎樣才能解決錯誤,以及如何能我將一個字符串和一個int值傳遞給addProperty?我在Android中以相同的方式完成了這項工作,並且在那裏工作得很好。

String serviceUrl = "URL to webservice"; 
    String serviceNameSpace = "namespace of web service"; 
    String soapAction = "URL to method name"; 
    String methodName = "Name of method"; 
    SoapObject rpc = new SoapObject(serviceNameSpace, methodName); 

    //compiler error here 
    rpc.addProperty("number1", 10); 
    rpc.addProperty("number2", 20); 

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.bodyOut = rpc; 
    envelope.dotNet = true;//IF you are accessing .net based web service this should be true 
    envelope.encodingStyle = SoapSerializationEnvelope.ENC; 
    HttpTransport ht = new HttpTransport(serviceUrl); 
    ht.debug = true; 
    ht.setXmlVersionTag(""); 
    String result = null; 
    try 
    { 
    ht.call(soapAction, envelope); 
    result = (String) (envelope.getResult()); 
    } 
    catch(org.xmlpull.v1.XmlPullParserException ex2){ 
    } 
    catch(Exception ex){ 
    String bah = ex.toString(); 
    } 
    return result; 

回答

3

您需要注意的是,BlackBerry開發是使用Java-ME完成的,而Android開發使用Java-SE完成。在Java中,基元不是對象。基元是像double,int,float,char這樣的值。

即使在Android中,您也無法傳遞對象所在的基元。您的代碼在Android中工作的原因是因爲添加到Java-SE的功能不在Java-ME中,稱爲自動裝箱。

通過包裝它們,可以使基元變得像對象一樣。這就是Double,Integer,Float和Character類所做的。在Java SE中,當編譯器看到一個作爲Object參數傳遞的原語時,它會自動轉換爲包裝或「盒裝」版本。這個特性在Java-ME中不存在,所以你必須自己做拳擊。這意味着:

rpc.addProperty("number1", new Integer(10)); 
rpc.addProperty("number2", new Integer(20)); 
相關問題