android - 解析 SOAP 响应时出现非法属性错误

标签 android web-services soap ksoap2

我正在开发一个 Android 应用程序,它利用我的 Web 服务来获取数据并显示它。我的 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>

现在我想从 Location 部分获取 Location ID 和 Location Name 数据。我正在使用以下代码。

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: illegal property: Locations

最佳答案

Android Sax 解析器:

SAXML 解析器:

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;
}

}

SAXML 处理程序:

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;
}

}

在您的 Activity/fragment 中使用此代码来解析数据

  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());

        }
    }

关于android - 解析 SOAP 响应时出现非法属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34409919/

相关文章:

android - 折叠工具栏布局展开点击事件而不是滚动

android - 尝试从特定发件人处获取短信

php - 为网站中的不同页面建立单独的数据库是一个好习惯吗?

web-services - Mechanical Turk 文件上传

c# - xdoc 查询的 Select 语句

Tomcat 7 和 Axis 2 的 Java 套接字超时异常

java - SQLite 数据库错误 [致命异常错误]

android - 消失前在复选框中显示勾号

ruby - 带有 WSDL 的 ruby​​ 中的 SOAP 服务器

java - 配置构建路径有效,但在 pom 中添加相同的依赖项不起作用