[英]How to get value from Flutter/Dart Soap web services asmx call?
我試圖了解 Flutter 和 Dart 如何使用 soap web 服務 asmx。 為此,我創建了非常基本的項目並使用在線 soap WebService asmx 進行測試。
使用http://www.dneonline.com/calculator.asmx?op=Add我成功地在 Flutter 中構建了我的信封。
在測試中,我使用SOAP 1.1部分。
首先,我未能運行該應用程序,但幸運的是,我發現了“ Content-Length: length ”的錯誤。 所以我將其刪除並且一切正常。
它是一個基本的計算器(添加)功能。 首先我不知道如何在信封內添加整數,所以我使用靜態硬編碼值。
在響應正文中,我找到了< AddResult > 9 </ AddResult >行,其中包含我從 soap 調用中得到的答案。
第二件事是(這是我的問題)我得到了響應主體,但我不知道如何從主體中獲取價值。
如何從 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"),
)
],
)),
);
}
}
這是 response.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"),
)
],
)),
);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.