2012-10-03 103 views
0

我正在嘗試創建一個簡單的Android應用程序以從Web服務服務器獲取數據。我已創建了假冒的氣象數據和配置的Web服務數據庫。當我從Android應用程序運行這段代碼:如何獲取SOAP對象元素?

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

結果字符串如下。如何從soap對象中獲得單個警報消息,名稱和優先級?謝謝。 anyType {element = anyType {complexType = anyType {choice = anyType {element = anyType {complexType = anyType {sequence = anyType {element = anyType {};}} anyType {any =元素= anyType的{};元素= anyType的{};元素= anyType的{};元素= anyType的{};元素= anyType的{};元素= anyType的{};元素= anyType的{}; }; }; }; }; }; }; };的DiffGram = anyType的{NewDataSet = anyType的{表= anyType的{ALERT_ID = 1; ALERT_TYPE =天氣; ALERT_NAME =該地區嚴重風暴; ALERT_MSG =這是一種極危險的情況,會有龍捲風般的風速。 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 = FLOOD WARNING; 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

檸好這裏是代碼即可獲得單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); 
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數據u可以使用GSON庫至極是在解析JSON對象

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