2012-03-27 75 views
0

調用web方法後,將返回一個XML字符串作爲響應。 了android端代碼是這樣的:如何解析android開發中從web服務返回的ksoap2響應?

SoapPrimitive response = (SoapPrimitive) ws.envelope.getResponse(); 

和XML是這樣的:

<string xmlns="http://tempuri.org/"> 
[{"ID":"[email protected]","float_latitude":22.338,"float_longitude":114.169},   {"ID":"[email protected]","float_latitude":22.33974,"float_longitude":114.169456}, {"ID":"[email protected]","float_latitude":22.3384857,"float_longitude":114.1691}, {"ID":"[email protected]","float_latitude":50,"float_longitude":100}] 
</string> 

我想

  1. 的id號返回
  2. 保存LAT並長期以ID爲本地變量

回答

2

O男人。首先你的迴應不是XML,而是JSON。我解析JSON的方式是首先構建一個通用對象。

public class yourObject{ 

    private String id; 
    private double lat; 
    private double lng; 
} 

從那裏建立一個集合數組列表應該沒問題。

ArrayList<yourObject> objects = new ArrayList<yourObject>(); 

之後,對JSON進行一些研究,這裏是一些入門代碼。

JSONArray myAwarry = new JSONArray(response); 

傳遞從服務器返回的字符串例如響應。

迭代通過數組

for(int i = 0; i < myAwarry.length(); i++){ 
JSONObject o = myAwarry.get(i); 
yourObject obj = new yourObject(); 
obj.setId(o.getString("ID")); 
obj.setlat(o.getDouble("float_longitude")); 
obj.setlng(o.getDouble("float_latitude")); 
//add the obj to the collections of objects 
    objects.add(obj); 

}

希望這有助於

+0

它完美對我的工作!我已經搜索瞭如何解析ksoap2 xml很多次,並找不到解決方案因爲我搞砸了XML和JSON。非常感謝您 – Chleung49 2012-03-27 23:23:25

+0

沒問題我很高興能夠提供幫助。 – JakeWilson801 2012-03-28 00:10:25

相關問題