2010-07-21 63 views
2

我是新來的android應用程序,我使用Ksoap2來訪問.net web服務和調用沒有參數執行就好。但有了參數,我變空了。 我遵循網站中提供的所有步驟。我嘗試了一切可能,但迄今爲止沒有運氣。我希望 有人能幫助,傳遞參數使用kso​​ap2到.net網絡服務,總是傳遞NULL(空)值

private static final String SOAP_ACTION = "http://www.agilelearning.com/GetProvincelist1"; 
private static final String METHOD_NAME = "GetProvincelist1 "; 
private static final String NAMESPACE = "http://www.agilelearning.com" ; 
private static final String URL = "http://192.168.1.24/Service.asmx"; 



//Method executed when Province is selected 
private OnItemSelectedListener selectListener = new OnItemSelectedListener() 
    { 
     public void onItemSelected(AdapterView parent, View v, int position, long id) 
     { 
      try 
      { 
ArrayList<String> districts=new ArrayList<String>(); 
       int Provinceid=position+1;//parent.getItemAtPosition(position).toString();   
       SoapObject request1 = new SoapObject(NAMESPACE, METHOD_NAME1);        
       request1.addProperty("Provincename","East Province"); 
       SoapSerializationEnvelope envelope1 = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
       envelope1.dotNet = true; 
       envelope1.setOutputSoapObject(request1); 
       HttpTransportSE at = new HttpTransportSE(URL); 
       at.debug = true; 
       at.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
       at.call(SOAP_ACTION1, envelope1);    
       SoapObject rs = (SoapObject)envelope1.getResponse();    int count=rs.getPropertyCount(); 
       for(int i=0;i<count;i++) 
       { 
        districts.add(rs.getProperty(i).toString()); 
       } 
       ArrayAdapter<String> aa;   
       aa = new ArrayAdapter<String>(home1.this, android.R.layout.simple_spinner_item, districts); 
       aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
       sd.setAdapter(aa); 
      } 
      catch(Exception ex) 
      { 
       MessageBox("No Districts found"); 
      } 

     } 
+0

當參數的元素不在正確的xml命名空間中時,會發生這種情況,使用哪個命名空間取決於您的.NET服務的配置方式。 (你可以從WSDL中解決這個問題)。 – superfell 2010-11-03 23:16:56

回答

0

首先,重新格式化您的代碼,以便它更可讀....

private static final String SOAP_ACTION = "http://www.agilelearning.com/GetProvincelist1"; 
private static final String METHOD_NAME = "GetProvincelist1"; 
private static final String NAMESPACE = "http://www.agilelearning.com"; 
private static final String URL = "http://192.168.1.24/Service.asmx"; 

//Method executed when Province is selected 
private OnItemSelectedListener selectListener = new OnItemSelectedListener() { 
    public void onItemSelected(AdapterView parent, View v, int position, long id) { 
     try { 
      ArrayList districts=new ArrayList(); 
      int Provinceid=position+1; 
      //parent.getItemAtPosition(position).toString(); 
      SoapObject request1 = new SoapObject(NAMESPACE, METHOD_NAME); 
      request1.addProperty("Provincename","East Province"); 
      SoapSerializationEnvelope envelope1 = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope1.dotNet = true; 
      envelope1.setOutputSoapObject(request1); 
      HttpTransportSE at = new HttpTransportSE(URL); 
      at.debug = true; 
      at.setXmlVersionTag(""); 
      at.call(SOAP_ACTION, envelope1); 

      SoapObject rs = (SoapObject)envelope1.getResponse(); 
      int count=rs.getPropertyCount(); 
      for(int i=0;i aa; 
       aa = new ArrayAdapter(home1.this, 
             android.R.layout.simple_spinner_item, 
             districts); 
      aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
      sd.setAdapter(aa); 
     } catch(Exception ex) { 
      MessageBox("No Districts found"); 
     } 
    } 
}; 

的問題是這一行: envelope1.dotNet = TRUE;

您可以在SoapSerializationEnvelope.java的第404-407行看到它添加了它們。文檔中顯示「Set this variable to true for compatibility with what seems to be the default encoding for .Net-Services. This feature is an extremely ugly hack. A much better option is to change the configuration of the .Net-Server to standard Soap Serialization!」聽起來像ksoap2只是在某種情況下破解了某些內容。

0

您使用的是什麼版本的Ksoap?我是google代碼上的ksoap2-android項目的項目所有者,並剛剛發佈了一個新版本2.5.2。 .net = true的工作很好,順便說一句。

http://code.google.com/p/ksoap2-android/

你或許應該設定在GETRESPONSE調用調試點,看看你會得到什麼。

如果你得到一個堆棧跟蹤,也很高興看到這一點。

哦,順便說一句。你不想在UI線程上做這樣的調用。您應該將其移動到AsyncTask中完成。

1

我有同樣的問題。在我的情況下,我從android發送的參數名稱與我的Web服務期望的參數名稱不同。

它們必須是相同的。

request.addProperty("DeviceId",uid); --add parameter from android 


    [WebMethod] 
    public LoginResult Login(string DeviceId) ---web service 
    { 
     ....... 
    } 
相關問題