如何获取SOAP Object元素?

时间:2012-10-03 20:06:30

标签: android soap

我正在尝试创建一个简单的Android应用程序来从Web服务服务器获取数据。我创建了一个包含假天气数据的数据库,并配置了Web服务当我从Android应用程序运行此代码时:

SoapObject results = (SoapObject) envelope.bodyIn;
result = results.getProperty("getAlertsResult").toString();

结果字符串如下。如何从soap对象获取单个警报消息,名称和优先级?谢谢。

anyType的{架构= {anyType的元素= anyType的{的complexType = {anyType的选择= {anyType的元素= anyType的{的complexType = {anyType的序列= {anyType的元素= anyType的{};元素= anyType的{};元素= anyType的{};元素= anyType的{};元素= anyType的{};元素= anyType的{};元素= anyType的{};元素= anyType的{}; }; }; }; }; }; }; };的DiffGram = anyType的{NewDataSet = anyType的{表= anyType的{ALERT_ID = 1; ALERT_TYPE =天气; ALERT_NAME =该地区严重风暴; ALERT_MSG =这是一个非常危险的情况,有TORNADO喜欢风速的预期。 ALERT_PRIORITY = HIGH; ALERT_ENABLED = 1; ALERT_USER =管理员; ALERT_DATETIME = 2012-07-24T00:00:00-04:00; };表= anyType的 {ALERT_ID = 2; ALERT_TYPE =天气; ALERT_NAME =洪水警告; ALERT_MSG =国家天气服务 田纳西州的MORRISTOWN发生了洪水; ALERT_PRIORITY =正常; ALERT_ENABLED = 1; ALERT_USER =管理员; ALERT_DATETIME = 2012-07-24T00:00:00-04:00; };表= anyType的{ALERT_ID = 3; ALERT_TYPE =地震; ALERT_NAME = 7.1地区的震动; ALERT_MSG =美国东部时间下午2:30在麦德龙地区登记的强烈地震,7.1 RS; ALERT_PRIORITY = HIGH; ALERT_ENABLED = 1; ALERT_USER =管理员; ALERT_DATETIME = 2012-07-24T00:00:00-04:00; }; }; }; }

2 个答案:

答案 0 :(得分:2)

以下是获取单个SoapObject属性的代码。

SoapObject resultOne = (SoapObject) results.getProperty(0);
SoapObject resultTwo = (SoapObject) resultOne.getProperty(1);
SoapObject allAlerts = (SoapObject) resultTwo.getProperty(0);
SoapObject alertOne = (SoapObject) allAlerts.getProperty(0);
SoapPrimitive alertPriority = (SoapPrimitive) alertOne.getProperty(4);

答案 1 :(得分:0)

你可以做的是制作一个php查询,它将检索数据并将其放入json格式。 json是一种非常好的共享跨平台数据的格式。 像这样

<?php

    $response = array();

    require_once __DIR__ . '/db_connect.php';

    $db = new DB_CONNECT();

    $result = mysql_query("SELECT * FROM table_name") or die(mysql_error());

    if (mysql_num_rows($result) > 0) {

        $response["forecasts"] = array();

        while ($row = mysql_fetch_array($result)) {


           //here you put all the rows from the column that you have retrieved 
            $product = array();
            $product["column1"] = $row["column1"];
            $product["column2"] = $row["column2"];

            array_push($response["forecasts"], $product);
        }

        $response["success"] = 1;

        echo json_encode($response);
    } else {

        $response["success"] = 0;
        $response["message"] = "No games found";

        echo json_encode($response);
    }
    ?>

进程json数据你可以使用gson库,它非常适合解析json到对象

http://bobbyprabowo.wordpress.com/2010/11/25/android-json-processing-using-gson-and-display-in-on-a-listview/