2012-06-07 110 views
0

我需要在我的android應用程序中訪問.NET webservice。我用ksoap2庫做了它。我建立了連接和一切,但我無法從服務中獲取數據。該服務應該發回一組值。我如何捕獲這些價值?使用kso​​ap2從Web服務獲取多個值Android

這是我的java代碼來訪問網絡服務。

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
    SoapSerializationEnvelope envelope = 
     new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request); 


    AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL); 

    try { 
     androidHttpTransport.call(SOAP_ACTION, envelope); 

     //code to get back the values here. This is my doubt. 
        //What code do I write here to get the values from the service ? 

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

這是從服務響應的格式。

HTTP/1.1 200 OK 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 

<?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/"> 
    <soap:Body> 
    <GetControlResponse xmlns="http://tempuri.org/"> 
     <GetControlResult> 
    <Id>int</Id> 
    <Belt>boolean</Belt> 
    <Lighting>boolean</Lighting> 
    <AutoSpeed>boolean</AutoSpeed> 
    <ManualSpeed>short</ManualSpeed> 
    <Projector>boolean</Projector> 
    <ProjecterPattern>short</ProjecterPattern> 
    </GetControlResult> 
</GetControlResponse> 
</soap:Body> 
</soap:Envelope> 

回答

2

檢查this一。它是一個很好的教程,用於訪問android中的ksoap2 web服務。

+0

本教程使用僅返回一個值的服務。我需要從我的服務中獲得多個值。我不能做response.toString(),因爲我的迴應將包含各種值。 – Swayam

0

您可以通過使用此代碼完成這項工作:

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
envelope.dotNet = true; 
envelope.setOutputSoapObject(request); 
System.out.println(request); 
envelope.encodingStyle = SoapSerializationEnvelope.XSD; 
HttpTransportSE httpTransportSE = new HttpTransportSE(URL); 
httpTransportSE.debug = true; 
try { 
    httpTransportSE.call(SOAP_ACTION, envelope); 

} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (XmlPullParserException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

String ss = httpTransportSE.requestDump; 
Log.d("Result", ss); 
System.out.println(ss); 
相關問題