无法从soap响应中获取值

时间:2015-12-09 13:17:07

标签: java soap ksoap2

这是我的代码

SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);
    request.addProperty("MobileNbr","9959077764");
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet=true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
    try {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        androidHttpTransport.debug = true;
        SoapObject response = (SoapObject)envelope.bodyIn;
        System.out.println(response);
        String result = response.getProperty(0);
        System.out.println(result);
    }catch (XmlPullParserException | IOException e) {
        e.printStackTrace();
    }   

这是我得到的回应。

GetCabListResponse{GetCabListResult=<CabList><status>0</status><cab>AP09BS9127</cab><cab>AP09BS9126</cab></CabList>; }

这是从response.getProperty(0);

获得的结果
<CabList><status>0</status><cab>AP09BS9127</cab><cab>AP09BS9126</cab></CabList>

我想要状态和驾驶室的值

1 个答案:

答案 0 :(得分:1)

这是一个从返回的XML中提取数据的正则表达式解决方案:

    String cab ="<CabList><status>0</status><cab>AP09BS9127</cab><cab>AP09BS9126</cab></CabList>";
    Pattern p = Pattern.compile("<status>([^<]+)</status><cab>([^<]+)</cab>");
    Matcher m = p.matcher(cab);
    if(m.find()) {
        System.out.println("Status: " + m.group(1));
        System.out.println("Cab: " + m.group(2));
    }

多个出租车:

    String cab ="<CabList><status>0</status><cab>AP09BS9127</cab><cab>AP09BS9126</cab></CabList>";
    Pattern p = Pattern.compile("<status>([^<]+)</status>((<cab>([^<]+)</cab>)*)");
    Matcher m = p.matcher(cab);
    if(m.find()) {
        System.out.println("Status: " + m.group(1));
        List<String> cablist = Arrays.asList(m.group(2).replace("<cab>", "").replace("</cab>", "#").split("#"));
        System.out.println("Cab: " + cablist);          
    }