gpt4 book ai didi

java - 从字符串中的 XML 获取数据 - Android

转载 作者:行者123 更新时间:2023-12-01 11:52:26 26 4
gpt4 key购买 nike

我正在使用 SOAP 服务来获取门票。我正在发送用户和通行证,并且我正在字符串中获取 xml。为此,我使用 ksoap2。

@Override
protected String doInBackground(String... params) {

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty(USER, params[0]);
request.addProperty(PASS, params[1]);

SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.bodyOut = request;
soapEnvelope.setOutputSoapObject(request);

HttpTransportSE HttpTransport = new HttpTransportSE(URL);
try {
HttpTransport.call(SOAP_ACTION, soapEnvelope);
return soapEnvelope.getResponse().toString();
} catch (IOException | XmlPullParserException e) {
e.printStackTrace();
return null;
}
}

@Override
protected void onPostExecute(String XML) {
super.onPostExecute(XML);
if (result != null) {
// Here I need to get data from XML

}
}

我的 XML 字符串如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<resp err="0">
<ticket>1234567989</ticket>
</resp>

所以我需要获取错误号和票号。

最佳答案

不要将响应转换为字符串并返回它,而是使用 getProperty() 方法获取所需的属性并将其转换为字符串。我是这样做的:

    String ticket;

public void getSoap() {

SoapObject request = new SoapObject(NAMESPACE, METHODNAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.implicitTypes = false;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransportSe = new HttpTransportSE(URL);
httpTransportSe.debug = true;
SoapObject response = null;

try {
httpTransportSe.call(SOAPACTION, envelope);
response = (SoapObject) envelope.getResponse();
ticket = response.getProperty("ticket").toString();

} catch (Exception e) {
e.printStackTrace();
}

}

从属性获取“err”元素可能需要更多研究,但您可以在获取ticket的地方尝试此操作:

String err = response.getAttribute("err").toString;

希望这对您有所帮助,祝您编码愉快!

关于java - 从字符串中的 XML 获取数据 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28699669/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com