2015-12-22 79 views
1

我正在研究一個利用我的Web服務獲取數據並顯示它的android應用程序。我的SOAP響應有點複雜,看起來像這樣。解析SOAP響應中的非法屬性錯誤

<?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> 
<GetSessionDetailsResponse xmlns="https://viberservices.com/gcdev/"> 
    <GetSessionDetailsResult> 
    <ModuleFlags>string</ModuleFlags> 
    <SessionID>string</SessionID> 
    <UserInfo> 
     <Fullname>string</Fullname> 
     <Language>long</Language> 
    </UserInfo> 
    <Locations> 
     <LocationDetails> 
     <LocationID>long</LocationID> 
     <LocationName>string</LocationName> 
     <PhotoURL>string</PhotoURL> 
     </LocationDetails> 
     <LocationDetails> 
     <LocationID>long</LocationID> 
     <LocationName>string</LocationName> 
     <PhotoURL>string</PhotoURL> 
     </LocationDetails> 
    </Locations> 
    </GetSessionDetailsResult> 
</GetSessionDetailsResponse> 

現在我想從位置部分的位置ID和地點名稱的數據。我正在使用下面的代碼。

SoapObject res=(SoapObject)envelope.bodyIn; 
SoapObject t=(SoapObject)res.getProperty("Locations"); 
for(int i=0; i<t.getPropertyCount(); i++){ 
    SoapObject locinfo=(SoapObject)t.getProperty(i); 
    String lid= locinfo.getProperty("LocationID").toString(); 
    String lname= locinfo.getProperty("LocationName").toString(); 
} 
//Code to display lid and lname 

但我得到一個運行時異常說了java.lang.RuntimeException:非法財物:位置

回答

0

我設法通過使用下面的代碼來解決這個問題:

SoapObject res=(SoapObject)envelope.bodyIn; 
SoapObject root=(SoapObject)((SoapObject)res.getProperty("GetSessionDetailsResult")).getProperty("Locations"); 
for(int i=0; i<root.getPropertyCount(); i++){ 
    SoapObject t=(SoapObject)root.getProperty(i); 
    String lid= t.getProperty("LocationID").toString(); 
    String lname= t.getProperty("LocationName").toString(); 
} 
1

的Android SAX解析器:

SAXXMLParser:

public class SAXXMLParser { 

public static List<Location> parse(String input) { 
    List<Location> locations = null; 

    try { 
     // create a XMLReader from SAXParser 
     XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser() 
       .getXMLReader(); 
     // create a SAXXMLHandler 
     SAXXMLHandler saxHandler = new SAXXMLHandler(); 
     // store handler in XMLReader 
     xmlReader.setContentHandler(saxHandler); 
     // the process starts 
     xmlReader.parse(input); 
     // get the `Locations list` 
     locations = saxHandler.getEmployees(); 

    } catch (Exception ex) { 
     Log.d("XML", "SAXXMLParser: parse() failed"); 
    } 

    // return location list 
    return locations; 
} 

} 

SAXXMLHandler:

import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 

import java.util.ArrayList; 
import java.util.List; 


public class SAXXMLHandler extends DefaultHandler { 


private List<Location> locations; 
private String tempVal; 
// to maintain context 
private Location location; 

public SAXXMLHandlerTest() { 
    locations = new ArrayList<Location>(); 
} 

public List<Location> getEmployees() { 
    return locations; 
} 

// Event Handlers 
public void startElement(String uri, String localName, String qName, 
         Attributes attributes) throws SAXException { 
    // reset 
    tempVal = ""; 
    if (qName.equalsIgnoreCase("LocationDetails")) { 
     // create a new instance of location 
     location = new Location(); 
    } 
} 

public void characters(char[] ch, int start, int length) 
     throws SAXException { 
    tempVal = new String(ch, start, length); 
} 

public void endElement(String uri, String localName, String qName) 
     throws SAXException { 
    if (qName.equalsIgnoreCase("LocationDetails")) { 
     // add it to the list 
     locations.add(location); 
    } else if (qName.equalsIgnoreCase("LocationID")) { 
     location.setLocationID(Long.parseLong(tempVal)); 
    } else if (qName.equalsIgnoreCase("LocationName")) { 
     location.setLocationName(tempVal); 
    } else if (qName.equalsIgnoreCase("PhotoURL")) { 
     location.setPhotoURL(tempVal); 
    } 
} 

} 

模型類:

public class Location { 


private long LocationID; 
private String LocationName; 
private String PhotoURL; 

public long getLocationID() { 
    return LocationID; 
} 

public void setLocationID(long locationID) { 
    LocationID = locationID; 
} 

public String getLocationName() { 
    return LocationName; 
} 

public void setLocationName(String locationName) { 
    LocationName = locationName; 
} 

public String getPhotoURL() { 
    return PhotoURL; 
} 

public void setPhotoURL(String photoURL) { 
    PhotoURL = photoURL; 
} 

} 

使用此代碼在您的活動/片段來分析數據

List<Location> locations = SAXXMLParsertest.parse("Your xml data"); 

    if (locations != null && locations.size() > 0) { 

     for (int i = 0; i < locations.size(); i++) { 

      Log.e("Location name", " " + locations.get(i).getLocationName()); 
      Log.e("Location Id", " " + locations.get(i).getLocationID()); 
      Log.e("Photo Uri", " " + locations.get(i).getPhotoURL()); 

     } 
    }