如何从 Flutter/Dart Soap Web 服务 asmx 调用中获取价值?



我试图了解Flutter和Dart如何使用soap web服务asmx。为此,我创建了非常基本的项目,并使用在线肥皂WebService asmx进行测试。

使用 http://www.dneonline.com/calculator.asmx?op=Add 我成功地在 Flutter 中构建了我的信封。

在测试中,我使用SOAP 1.1部分。

首先,我未能运行该应用程序,但幸运的是,我发现了">内容长度:长度"上的错误。所以我删除了它,一切都很好用。

它是一个基本的计算器(添加)功能。首先,我不知道如何在信封内添加整数,所以我使用静态硬编码值。

在响应正文中,我找到了9行,其中包含肥皂调用中的我的答案。

第二件事是(这是我的问题)我得到响应体,但我不知道如何从体中获取值。

如何从 Flutter/Dart Soap Web 服务 asmx 调用中获取价值?

这是我完整的 Flutter 代码。

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _firstInteger = 5;
int _secondInteger = 4;
var envelope =
"<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Add xmlns="http://tempuri.org/"> <intA>5</intA> <intB>4</intB></Add></soap:Body></soap:Envelope>";
var _testValue = "";
bool _add = true;
@override
void initState() {
// TODO: implement initState
super.initState();
_add = true;
}
Future _getCalculator() async {
http.Response response =
await http.post('http://www.dneonline.com/calculator.asmx',
headers: {
"Content-Type": "text/xml; charset=utf-8",
"SOAPAction": "http://tempuri.org/Add",
"Host": "www.dneonline.com"
},
body: envelope);
setState(() {
_testValue = response.body;
_add = true;
});
print(response.body);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_add == true
? new Text(
"Answer: $_testValue",
style: new TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
color: Colors.red[800]),
)
: new CircularProgressIndicator(),
new RaisedButton(
onPressed: () {
setState(() {
_add = false;
});
_getCalculator();
},
child: new Text("Calculate"),
)
],
)),
);
}
}

这是响应.body 输出:

I/flutter ( 6414): <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AddResponse xmlns="http://tempuri.org/"><AddResult>9</AddResult></AddResponse></soap:Body></soap:Envelope>

完整的工作代码在这里:

从 Flutter 调用 soap Web 服务 asmx 并使用 dart xml 解析它。 :)

希望我们可以得到一些 Dart xml 信封转换器,这样我们就不必手动创建每个信封。

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:xml/xml.dart' as xml;

void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _firstInteger = 5;
int _secondInteger = 4;
var envelope =
"<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <Add xmlns="http://tempuri.org/"> <intA>5</intA> <intB>4</intB></Add></soap:Body></soap:Envelope>";
var _testValue = "";
bool _add = true;
List<dynamic> itemsList = List();
@override
void initState() {
// TODO: implement initState
super.initState();
_add = true;
}
Future _getCalculator() async {
http.Response response =
await http.post('http://www.dneonline.com/calculator.asmx',
headers: {
"Content-Type": "text/xml; charset=utf-8",
"SOAPAction": "http://tempuri.org/Add",
"Host": "www.dneonline.com"
},
body: envelope);
var _response = response.body;
await _parsing(_response);
}

Future _parsing(var _response) async {
var _document = xml.parse(_response);
Iterable<xml.XmlElement> items = _document.findAllElements('AddResponse');
items.map((xml.XmlElement item) {
var _addResult = _getValue(item.findElements("AddResult"));
itemsList.add(_addResult);
}).toList();
print("itemsList: $itemsList");
setState(() {
_testValue = itemsList[0].toString();
_add = true;
});
}

_getValue(Iterable<xml.XmlElement>  items) {
var textValue;
items.map((xml.XmlElement node) {
textValue = node.text;
}).toList();
return textValue;
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
_add == true
? new Text(
"Answer: $_testValue",
style: new TextStyle(
fontSize: 18.0,
fontWeight: FontWeight.bold,
color: Colors.red[800]),
)
: new CircularProgressIndicator(),
new RaisedButton(
onPressed: () {
setState(() {
_add = false;
});
_getCalculator();
},
child: new Text("Calculate"),
)
],
)),
);
}
}

相关内容

  • 没有找到相关文章

最新更新