2011-09-23 135 views
0

我在訪問Web服務時遇到了一些問題。獲取ClassCastException錯誤。考慮一個場景,我試圖訪問一個Web服務的方法,Web服務應該返回兩個字符串(比如說String1和String2)。而且,我必須提供或傳遞兩個參數(可以說參數1和參數2,其中參數1應該是整數和參數2應該是字符串)這裏是我的代碼Android:肥皂原始錯誤

public class MyWebService extends Activity { 

private static final String SOAP_ACTION ="http://www.mywebsite.com/myMethod"; 
private static final String METHOD_NAME = "MyMethod"; 
private static final String NAMESPACE = "http://www.myNamespace/"; 
    private static final String URL = "http://mysession.com/myservice.asmx?WSDL"; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

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

     PropertyInfo pi = new PropertyInfo(); 
     pi.setName("Parameter 1"); 
     pi.setValue(1); 
     pi.setType(pi.INTEGER_CLASS); 
     request.addProperty(pi); 

     PropertyInfo pi2 = new PropertyInfo(); 
     pi2.setName("Parameter 2"); 
     pi2.setValue("Any string"); 
     pi2.setType(pi2.STRING_CLASS); 
     request.addProperty(pi2); 

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

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


     String string1=result.getProperty(0).toString(); 
     String string2=result.getProperty(1).toString(); 


    } catch (Exception e) { 
     e.printStackTrace();  

    }   
} 

}

這是除了我得到 java.lang.ClassCastException:org.ksoap2.serialization.SoapPrimitive

誰能告訴我,如果我錯了,在這裏做什麼.. 感謝

回答

1

試試這個,

SoapPrimitive result= (SoapPrimitive)envelope.getResponse(); 

OR

Object result= (Object)envelope.getResponse(); 

,而不是

SoapObject result=(SoapObject)envelope.getResponse(); 
-1

試試這個:

SoapObject request = new SoapObject(SOAP_NAMESPACE,SOAP_METHOD); 
request.addProperty("name", abcd); 
request.addProperty("age", 30); 
+0

作爲一個大師,你能解釋多一點有什麼不好,爲什麼你認爲他應該嘗試這個? –