2014-12-03 49 views
0

我想用從Web服務的XML輸出的經緯度信息的Android應用程序。不完整的XML而KSOAP對象轉換爲XML

http://www.webservicex.net/airport.asmx?op=getAirportInformationByAirportCode

當檢查在Web瀏覽器的輸出,我得到如下: 此XML文件沒有出現有任何相關的樣式信息。文檔樹如下圖所示。

<string xmlns="http://www.webserviceX.NET"> 
<NewDataSet> <Table> <AirportCode>MSP</AirportCode> <CityOrAirportName>MINNEAPOLIS INTL</CityOrAirportName> <Country>United States</Country> <CountryAbbrviation>US</CountryAbbrviation> <CountryCode>63</CountryCode> <GMTOffset>6</GMTOffset> <RunwayLengthFeet>10000</RunwayLengthFeet> <RunwayElevationFeet>841</RunwayElevationFeet> <LatitudeDegree>44</LatitudeDegree> <LatitudeMinute>54</LatitudeMinute> <LatitudeSecond>0</LatitudeSecond> <LatitudeNpeerS>N</LatitudeNpeerS> <LongitudeDegree>93</LongitudeDegree> <LongitudeMinute>13</LongitudeMinute> <LongitudeSeconds>0</LongitudeSeconds> <LongitudeEperW>W</LongitudeEperW> </Table> <Table> <AirportCode>MSP</AirportCode> <CityOrAirportName>MINNEAPOLIS INTL</CityOrAirportName> <Country>United States</Country> <CountryAbbrviation>US</CountryAbbrviation> <CountryCode>63</CountryCode> <GMTOffset>6</GMTOffset> <RunwayLengthFeet>10000</RunwayLengthFeet> <RunwayElevationFeet>841</RunwayElevationFeet> <LatitudeDegree>44</LatitudeDegree> <LatitudeMinute>54</LatitudeMinute> <LatitudeSecond>0</LatitudeSecond> <LatitudeNpeerS>N</LatitudeNpeerS> <LongitudeDegree>93</LongitudeDegree> <LongitudeMinute>13</LongitudeMinute> <LongitudeSeconds>0</LongitudeSeconds> <LongitudeEperW>W</LongitudeEperW> </Table> </NewDataSet> 
</string> 

我得到這個在下面的代碼SOAP對象的形式:

private static String SOAP_ACTION = "http://www.webserviceX.NET/getAirportInformationByAirportCode"; 
private static String NAMESPACE = "http://www.webserviceX.NET"; 
private static String METHOD_NAME = "getAirportInformationByAirportCode"; 
private static String URL = "http://www.webservicex.NET/airport.asmx?WSDL"; 
public String source_textview, destination_textview; 

//Initialize soap request + add parameters 
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);  

//Use this to add parameters 
request.addProperty("airportCode",airportCode); 

//Declare the version of the SOAP request 
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

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

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

androidHttpTransport.call(SOAP_ACTION, envelope); 

try { 
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

    androidHttpTransport.debug = true; 
    //androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
    //this is the actual part that will call the webservice 
    androidHttpTransport.call(SOAP_ACTION, envelope); 

    String result=androidHttpTransport.responseDump; 
    Log.d("xml:", "is:" + result);  

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

我得到的XML如下:

D/xml:(4736): is:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getAirportInformationByAirportCodeResponse xmlns="http://www.webserviceX.NET"><getAirportInformationByAirportCodeResult>&lt;NewDataSet /&gt;</getAirportInformationByAirportCodeResult></getAirportInformationByAirportCodeResponse></soap:Body></soap:Envelope> 
+0

不要擔心有關'style information'的警告信息 - 這只是您的瀏覽器。查看源代碼,或使用開發工具,提琴手等查看實際的XML。但請注意,webservice本身會返回一個XmlEncoded字符串 - 在解析lats和long之前,您需要對其進行解碼。 – StuartLC 2014-12-03 19:15:08

+0

我不關心的警告。我想使用我提到的Soap對象來提取經緯度信息 – qoo 2014-12-03 19:23:34

回答

0

您輸入upthere的代碼不編譯,所以它不包括你所包含的代碼。除了這一點,你向我們展示的響應是情況時有空參數機場代碼。我嘗試清理你的代碼(包含在下面),它的工作原理。真的不知道你以前做錯了什麼。

private String SOAP_ACTION = "http://www.webserviceX.NET/getAirportInformationByAirportCode"; 
private String NAMESPACE = "http://www.webserviceX.NET"; 
private String METHOD_NAME = "getAirportInformationByAirportCode"; 
private String URL = "http://www.webservicex.NET/airport.asmx?WSDL"; 

@Override 
protected String doInBackground(String... params) { 

    //Initialize soap request + add parameters 
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);  

    //Use this to add parameters 
    request.addProperty("airportCode","MSP"); 

    //Declare the version of the SOAP request 
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

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

    try { 
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

     androidHttpTransport.debug = true; 
     //androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); 
     //this is the actual part that will call the webservice 
     androidHttpTransport.call(SOAP_ACTION, envelope); 

     String result=androidHttpTransport.responseDump; 

    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    }   
    return "aaa"; 
} 

現在你必須解析XML是在「getAirportInformationByAirportCodeResult」標記爲文本。