2012-12-26 207 views
0

如何才能使用WebService?這是我的代碼:Web服務Android肥皂

WebServiceParams ws = new WebServiceParams(context); 

    SoapObject request = new SoapObject(ws.getWSNameSpace(), ws.getWSHelloMethod()); 

    SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    soapSerializationEnvelope.setOutputSoapObject(request); 

    request.addProperty("name", "Charly"); 

    try { 
     HttpTransportSE httpTransportSE = new HttpTransportSE(ws.getWSUrl()); 
     httpTransportSE.debug = true; 

     httpTransportSE.call(ws.getWSNameSpace() + ws.getWSLoginMethod(), soapSerializationEnvelope); 

     String xmlResult = soapSerializationEnvelope.getResponse().toString(); 

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

而且我得到的SOAPFault的faultcode SoapClient的faultstring解組錯誤意想不到的元素URI:「」本地「名」預期的元素是< {}請求>

的主要部分webservice是:

<request> 
     <name>?</name> 
</request> 

我該如何寫請求標記並在名稱內部發送它?

回答

1

我不知道你是否在使用庫。 我能夠使用Ksoap2達到此目的。

在這裏你有一個例子。希望能幫助到你。

public static int getTotalRows(){ 
    SoapObject request = new SoapObject(Variables.WEBSERVICE_NAMESPACE, Variables.METHOD_GETDEALS_TOTAL); 
    SoapSerializationEnvelope env = new  SoapSerializationEnvelope(SoapEnvelope.VER11); 
    env.dotNet = true; 
    env.setOutputSoapObject(request); 
    HttpTransportSE androidHttpTransport = new  HttpTransportSE(Variables.WEBSERVICE_URL); 
    int totalRows = 0; 

    try { 
     androidHttpTransport.call(Variables.SOAP_ACTION_GETDEALS_TOTALROWS, env); 
     SoapPrimitive response = (SoapPrimitive)env.getResponse(); 
     totalRows = Integer.valueOf(response.toString()); 
     Log.i(Variables.TAG, response.toString()); 
     return totalRows; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return totalRows; 
    } catch (XmlPullParserException e) { 
     e.printStackTrace(); 
     return totalRows; 
    } catch (NumberFormatException e) { 
     e.printStackTrace(); 
     return totalRows; 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
     return totalRows; 
    } 
} 
+0

這裏真正的問題是如何通過webservice發送POJO? – user1893074

+0

您可以嘗試使用Gson將pojo序列化爲JSON。 – blindado