如何解析ksoap数组响应

时间:2013-04-26 02:21:26

标签: android android-ksoap2

我有一个Web服务,它会抛出一组数据。我正在使用Ksoap从我的网络服务获得响应,即

  

anyType {NewDataSet = anyType {Table = anyType {couponname = coupon name;   couponimage =图像; COUPONCODE =代码; coupondescription =描述;   couponstartdate =开始日期; couponenddate =结束日期; ID = 1; };   表= anyType {couponname = coupon name1; couponimage =图像;   COUPONCODE =代码1; coupondescription =内容描述;   couponstartdate =开始日期1; couponenddate =结束日期1; ID = 2; };   表= anyType {couponname = coupon name2; couponimage =图像;   COUPONCODE =码2; coupondescription =内容描述;   couponstartdate =开始日期2; couponenddate = end date2; ID = 3; }; }; }

任何人都可以提供教程或建议如何解析此回复吗? 任何想法都将受到高度赞赏。

1 个答案:

答案 0 :(得分:2)

是的,你可以这样做:

如果您的回答与以下相似:

anyType{
    StatusSetting=anyType{Id=1; Name=Til afskrivning; LocationId=1; Editable=true; Default=true; Transcribed=false; }; 
    StatusSetting=anyType{Id=2; Name=Afskrevet; LocationId=1; Editable=false; Default=false; Transcribed=true; }; 
    ...
}

然后你必须这样做:

SoapObject countryDetails = (SoapObject)envelope.getResponse();
System.out.println(countryDetails.toString());

ArrayList list = new ArrayList(countryDetails.getPropertyCount());
lv_arr = new String[countryDetails.getPropertyCount()];
for (int i = 0; i < countryDetails.getPropertyCount(); i++) {
    object property = countryDetails.getProperty(i);
    if (property instanceof SoapObject) {
        SoapObject countryObj = (SoapObject) property;
        String countryName = countryObj.getProperty("countryName").toString();
        list.add(countryName );
    }
}

如果你能通过这个例子理解,请告诉我;否则,我会向你发送你的回复数据的解析代码。

或其他:

只需添加:

即可从soap对象获取XML响应
androidHttpTransport.debug = true;
在呼叫之前

androidHttpTransport.call(SOAP_ACTION, envelope);

并添加:

String xml = androidHttpTransport.responseDump;
电话结束后

您将获得XML输出的字符串XML。所以你可以使用任何(DOM,xmlpull或SAX)解析器来解析它。

您可以按照以下代码来完成任务:

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// Add the input required by web service
request.addProperty("city","chennai");
request.addProperty("key","10000");

SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);

// Make the soap call.
androidHttpTransport.call(SOAP_ACTION, envelope);

// Get the SoapResult from the envelope body.
resultRequestSOAP = (SoapObject) envelope.bodyIn;


System.out.println("********Response : "+resultRequestSOAP.toString());

SoapObject root = (SoapObject) resultRequestSOAP.getProperty(0);
SoapObject s_deals = (SoapObject) root.getProperty("FOO_DEALS");

StringBuilder stringBuilder = new StringBuilder();

System.out.println("********Count : "+ s_deals.getPropertyCount());

for (int i = 0; i < s_deals.getPropertyCount(); i++) 
{
    Object property = s_deals.getProperty(i);
    if (property instanceof SoapObject)
    {
        SoapObject category_list = (SoapObject) property;
        String CATEGORY = category_list.getProperty("CATEGORY").toString();
        String CATEGORY_URL = category_list.getProperty("CATEGORY_URL").toString();
        String CATEGORY_ICON = category_list.getProperty("CATEGORY_ICON").toString();
        String CATEGORY_COUNT = category_list.getProperty("CATEGORY_COUNT").toString();
        String SUPERTAG = category_list.getProperty("SUPERTAG").toString();
        String TYPE = category_list.getProperty("TYPE").toString();
        stringBuilder.append
        (
             "Row value of: " +(i+1)+"\n"+
             "Category: "+CATEGORY+"\n"+
             "Category URL: "+CATEGORY_URL+"\n"+
             "Category_Icon: "+CATEGORY_ICON+"\n"+
             "Category_Count: "+CATEGORY_COUNT+"\n"+
             "SuperTag: "+SUPERTAG+"\n"+
             "Type: "+TYPE+"\n"+
             "******************************"
        );                   
        stringBuilder.append("\n");
    }
}