2011-10-28 6 views
2

.Net Webサービスを呼び出すサンプルアプリケーションを開発しています。 Eclipseのビルドパスにksoap2-j2me-core-prev-2.1.2.jarを追加しました。"メソッドは引数には適用されません"

私は、メソッドaddProperty介して2つの値を渡しています:「番号1」と10整数として、また、「number2の」と20。これは、コンパイルエラーが発生します。

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

どのようにエラーを解決することができますし、どのようにすることができますがaddPropertyに1つの文字列と1つのint値を渡しますか?私はこれを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-MEに追加されたJava-MEに追加された機能(自動ボクシング)が原因です。

プリミティブをラッピングしてオブジェクトのようにすることができます。これがDouble、Integer、Float、Characterクラスの機能です。 Java SEでは、コンパイラーがObject引数として渡されるプリミティブを見ると、自動的にラップされた「Boxed」バージョンに変換されます。この機能はJava-MEには存在しないため、あなた自身でボクシングを行う必要があります。この場合、その意味は:

rpc.addProperty("number1", new Integer(10)); 
rpc.addProperty("number2", new Integer(20));