2015-07-10 93 views
2

這是我的WSDL文件視圖。如何在android中通過soap請求

<s:element name="ManageUser"> 
<s:complexType> 
<s:sequence> 
<s:element minOccurs="1" maxOccurs="1" name="Scenario" type="tns:ManageUserScenario"/> 
<s:element minOccurs="0" maxOccurs="1" name="Language" type="s:string"/> 
<s:element minOccurs="0" maxOccurs="1" name="Userguid" type="s:string"/> 
<s:element minOccurs="0" maxOccurs="1" name="Username" type="s:string"/> 
<s:element minOccurs="0" maxOccurs="1" name="Email" type="s:string"/> 
<s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string"/> 
<s:element minOccurs="0" maxOccurs="1" name="Firstname" type="s:string"/> 
<s:element minOccurs="0" maxOccurs="1" name="Lastname" type="s:string"/> 
<s:element minOccurs="0" maxOccurs="1" name="Address" type="s:string"/> 
<s:element minOccurs="0" maxOccurs="1" name="District" type="s:string"/> 
<s:element minOccurs="0" maxOccurs="1" name="Province" type="s:string"/> 
<s:element minOccurs="0" maxOccurs="1" name="Country" type="s:string"/> 
<s:element minOccurs="0" maxOccurs="1" name="Postcode" type="s:string"/> 
<s:element minOccurs="0" maxOccurs="1" name="Phone" type="s:string"/> 
<s:element minOccurs="1" maxOccurs="1" name="Newsletter" type="tns:SubscribeNewsletter"/> 
</s:sequence> 
</s:complexType> 
</s:element> 
<s:simpleType name="ManageUserScenario"> 
<s:restriction base="s:string"> 
<s:enumeration value="Create"/> 
<s:enumeration value="Update"/> 
</s:restriction> 
</s:simpleType> 
<s:simpleType name="SubscribeNewsletter"> 
<s:restriction base="s:string"> 
<s:enumeration value="X"/> 
<s:enumeration value="N"/> 
</s:restriction> 
</s:simpleType> 

我該如何設置請求。這是我的代碼,我沒有得到任何迴應,但警告。請告訴我我錯在哪裏。我的代碼:

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

       request.addProperty("Scenario", "Create"); 
       request.addProperty("Language", "en"); 
       request.addProperty("Username", "abc"); 
       request.addProperty("Email", "[email protected]"); 
       request.addProperty("Password", "abc"); 
       request.addProperty("Firstname", "kumar"); 
       request.addProperty("Lastname", "aryan"); 
       request.addProperty("Userguid", ""); 
       request.addProperty("Phone", "0000000000"); 
       request.addProperty("Address", ""); 
       request.addProperty("District", ""); 
       request.addProperty("Province", ""); 
       request.addProperty("Country", "India"); 
       request.addProperty("Postcode", ""); 
       request.addProperty("Newsletter", "X"); 

       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
         SOAP_VERSION); 

       envelope.setOutputSoapObject(request); 
       envelope.dotNet = true; 

       HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
       Log.e("Try Block 1", envelope + " - " + request); 
       androidHttpTransport.call(SOAP_ACTION, envelope); 

       SoapObject result1 = (SoapObject) envelope.bodyIn; 

而且我得到這個日誌:

W/System.err(32420): org.xmlpull.v1.XmlPullParserException: unexpected type (position:TEXT To register an [email protected]:166 in [email protected]) 
: at org.kxml2.io.KXmlParser.nextTag(KXmlParser.java:2045) 
: at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:275) 
: at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389) 
: at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273) 
: at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389) 
: at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273) 
: at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389) 
: at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273) 
: at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389) 
: at org.ksoap2.serialization.SoapSerializationEnvelope.readUnknown(SoapSerializationEnvelope.java:273) 
: at org.ksoap2.serialization.SoapSerializationEnvelope.read(SoapSerializationEnvelope.java:389) 
: at org.ksoap2.serialization.SoapSerializationEnvelope.parseBody(SoapSerializationEnvelope.java:151) 
: at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:140) 
: at org.ksoap2.transport.Transport.parseResponse(Transport.java:118) 
: at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:253) 
: at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116) 
: at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111) 
: at com.chilindo.SignUpActivity$AsyncTaskRunner.doInBackground(SignUpActivity.java:193) 

任何幫助表示讚賞,謝謝。

+0

http://stackoverflow.com/a/29444607/4447803儘量LIK這一點,在這個環節上我已經使用SOAP請求僅 – Mano

+0

您可以查看[此鏈接](http://www.ibm.com /developerworks/library/x-soapcl/listing1.html) – Pankaj

+0

感謝您的幫助。讓我檢查一下這些鏈接是否適用於我。 –

回答

0

嗨,這是使用wsdl api的複雜方式。

HttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(NAMESPACE + "users.asmx"); 

       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
         SoapEnvelope.VER11); 
       envelope.encodingStyle = SoapEnvelope.ENC; 

       String bodyOut = "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"; 
       bodyOut += "<soap:Body><" + METHOD + " xmlns=\"" 
         + NAMESPACE + "\">"; 
       bodyOut += "<Scenario>" + "Create" 
         + "</Scenario><Language>" 
         + R.string.language 
         + "</Language>" 
         + "<Userguid>" 
         + "" 
         + "</Userguid><Username>" 
         + str_username 
         + "</Username>" 
         + "<Email>" 
         + str_email 
         + "</Email> <Password>" 
         + str_password 
         + "</Password><Firstname>" 
         + str_first_name 
         + "</Firstname>" 
         + " <Lastname>" 
         + str_last_name 
         + "</Lastname><Address>" 
         + "" 
         + "</Address><District>" 
         + "" 
         + "</District>" 
         + "<Province>" 
         + "" 
         + "</Province><Country>" 
         + str_country 
         + "</Country><Postcode>" 
         + "" 
         + "</Postcode>" 
         + "<Phone>" 
         + str_phone 
         + "</Phone><Newsletter>" 
         + news_letter 
         + "</Newsletter>"; 
       bodyOut += "</" + METHOD + "></soap:Body></soap:Envelope>"; 

       String xml2 = bodyOut; 
       // System.out.println("Body " + xml2); 
       StringEntity se = new StringEntity(xml2, HTTP.UTF_8); 
       se.setContentType("text/xml"); 
       httpPost.addHeader(SOAP_ACTION, NAMESPACE + "users.asmx"); 
       httpPost.setEntity(se); 

       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity resEntity = httpResponse.getEntity(); 
       String xml = EntityUtils.toString(resEntity); 
+0

謝謝尼克斯......它爲我工作。你是我生命中的救星 –

0

這就是您如何使用SOAP API發出請求,然後獲得響應。以下是通過SOAP獲取數據的最簡單格式。希望它有幫助...

public class WebService 
{ 

public static SoapObject getSoapResponse(String email, String password, String Key) 
{ 
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    PropertyInfo EmailPI = new PropertyInfo(); 
    EmailPI.setName("email"); 
    EmailPI.setValue(email); 
    EmailPI.setType(String.class); 
    request.addProperty(EmailPI); 

    PropertyInfo passwordApi = new PropertyInfo(); 
    passwordApi.setName("password"); 
    passwordApi.setValue(password); 
    passwordApi.setType(String.class); 
    request.addProperty(passwordApi); 

    PropertyInfo apiKeyParam=new PropertyInfo(); 
    apiKeyParam.setName("Key"); 
    apiKeyParam.setType(String.class); 
    apiKeyParam.setValue(apiKey); 
    request.addProperty(apiKeyParam); 

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

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
    SoapObject response = null; 
    try { 
     androidHttpTransport.call(SOAP_ACTION, envelope); 
     response = (SoapObject)envelope.getResponse(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
    return response; 
} 
} 
+0

感謝您的幫助,但它也不適合我。有沒有其他辦法可以這樣做? –

+0

@UmeshKumar你面臨的錯誤是什麼? – Umair

+0

我已更新我遇到的錯誤。請檢查它。 –