2012-04-11 3 views
2

Windows AzureでホストされているVB.NET WS(Webサービス)にパラメータを送信しようとしています。単純なこんにちは世界(パラメータなし)をうまく動作させるが、何らかの理由でWebサービス側のパラメータがnullになっていますか?呼び出されるAndroidクライアントからWindows Azure上でホストされているVB.NET WSへのパラメータの受け渡し

Webサービス機能(IService1.vb):

<OperationContract()> 
Function GetAddition(ByVal number1 As Integer, ByVal number2 As Integer) As String 

機能の実装(Service1.svc.vb):

Function GetAddition(ByVal number1 As Integer, ByVal number2 As Integer) As String Implements IService1.GetAddition 
    Return number1 + number2 
End Function 

Androidのクライアントコード:

import java.util.ArrayList; 

import android.app.*; 
import android.os.*; 
import android.widget.TextView; 

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.PropertyInfo; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 
import org.xmlpull.v1.XmlPullParserException; 

public class Test2Activity extends Activity { 
TextView tv; 

private static final String SOAP_ACTION = "http://tempuri.org/IService1/";  
private static final String NAMESPACE = "http://tempuri.org/";  
private static final String URL = "http://a42c90a9e3e74fffa7b0093001f51de8.cloudapp.net/Service1.svc";  

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    System.setProperty("http.keepAlive", "false"); 
    tv=(TextView)findViewById(R.id.textView1);    

    ArrayList<Object> tmpParam = new ArrayList<Object>(); 
    tmpParam.add(1); 
    tmpParam.add(4); 

    String result = call("GetAddition", tmpParam, new String[]{"number1","number2"}); 
    //String result = call("GetHello", new ArrayList<Object>(), new String[]{""}); //works 

    tv.setText(result); 


    } 

public String call(String methodName, ArrayList<Object> arrParameterValues, String arrParameterNames[]) 
{ 
    try { 

     SoapObject request = new SoapObject(NAMESPACE, methodName); 

     for (int k = 0;k < arrParameterValues.size() ; k++){ 
      PropertyInfo tmp = new PropertyInfo(); 
      tmp.setName(arrParameterNames[k]); 
      tmp.setValue(arrParameterValues.get(k)); 
      tmp.setType(int.class); //hard coded for now 
      request.addProperty(tmp); 
     } 
     //tried 
     /* 
     request.addProperty("number1",1); 
     request.addProperty("number2",1); 
     */ 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

     envelope.setOutputSoapObject(request); 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
     androidHttpTransport.call(SOAP_ACTION.concat(methodName) , envelope); 

     //If an XmlPullParserException occurs, retry once in order to workaround an Android emulator bug 
     try { 
      androidHttpTransport.call(SOAP_ACTION.concat(methodName), envelope); 
     } 
     catch(XmlPullParserException ex) { 
      ex.printStackTrace(); 

      androidHttpTransport.call(SOAP_ACTION.concat(methodName), envelope); 
     } 

     Object result = envelope.getResponse();     
     return result.toString(); 
    } 
    catch(Exception ex) { 
     return ex.toString(); 
    } 
} 
}//end of class  

返される値は0です。他の文字列パラメータ化された関数では、文字列はクライアント上で受信されません。

私はkohap2-android-assembly-2.6.2-jar-with-dependenciesをAndroid 2.1で使用しており、Eclipse 3.7.2で開発中です。 WebサービスはVisual Studio 2010で構築されました

あなたが思うかもしれません:「なぜVBではなくC#?」私はすでにVBでいくつかの経験を持っているためである:)

おかげ

答えて

0

は、あなたは、次の質問のようにメソッドを使用してパラメータを渡すことができます。

How to call a WCF service using ksoap2 on android?

同様:

try { 
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    request.addProperty("Name", "Qing"); 

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( 
      SoapEnvelope.VER11); 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request); 


    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
    androidHttpTransport.call(SOAP_ACTION, envelope); 
    SoapPrimitive result = (SoapPrimitive)envelope.getResponse(); 

    //to get the data 
    String resultData = result.toString(); 
    // 0 is the first object of data 


    sb.append(resultData + "\n"); 
} catch (Exception e) { 
    sb.append("Error:\n" + e.getMessage() + "\n"); 
} 
+0

それは動作します!おかげでトム、なぜあなたは教えてくれますか?私の推測は 'envelope.dotnet = true;'です。他の投稿では、これは醜いハックなので避けなければならないと彼らは言っていますか? – Luke

+0

私はあなたが正しいと確信しています。私はそれが問題の原因だと思っていますが、確かに知るためにはそれをさらに調べなければなりません。 – Tom

関連する問題