这是我的代码
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>
我想要状态和出租车的值
最佳答案
这是一个正则表达式解决方案,用于从返回的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);
}
关于java - 无法从 SOAP react 中获取值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34179820/