如何从Flutter / Dart Soap网络服务asmx电话获得价值?

问题描述 投票:0回答:1

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

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

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

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

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

在回复正文中,我找到了<AddResult> 9 </ AddResult>行,其中包含来自soap调用的答案。

第二件事是(这是我的问题)我得到了回复的身体,但我不知道如何从身体获得价值。

如何从Flutter / Dart Soap网络服务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>
web-services soap dart flutter asmx
1个回答
0
投票

完整的工作代码在这里:

从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"),
          )
        ],
      )),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.