如何在Flutter中发送或接收xml文件? [英] How to send or receive xml file in Flutter?

查看:140
本文介绍了如何在Flutter中发送或接收xml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用Flutter发送和接收JSON字符串数据.但是我找不到任何有关如何使用Flutter发送和接收xml文件的信息.

I can send and receive JSON, string data with Flutter. But I cannot find any information how to send and receive xml file with Flutter.

我正在寻找好的文档和基本的实际操作示例.有什么帮助吗?

I am looking good documentation and basic hands-on-example. Any help please?

推荐答案

感谢@GünterZöchbauer.我想在Flutter中建立xml,并能够发布并获得响应.这是代码:

Thanks to @GünterZöchbauer. I mange to build xml in Flutter and be able to post and get the response. Here is the code:

依赖项:

// Add pubspec.yaml:   xml: "^3.2.1"
import 'package:xml/xml.dart' as xml;
import 'dart:io';

内置XML:

// TODO: BUILD XML FILE
Future<HttpClientResponse> _sendOTP() async {
  var builder = new xml.XmlBuilder();
  builder.processing('xml', 'version="1.0" encoding="iso-8859-9"');
  builder.element('MainmsgBody', nest: () {
    builder.element('UserName', nest: "xxxxxxxx");
    builder.element('PassWord', nest: "yyyyyyyy");
    builder.element('Action', nest: 5);
    builder.element('Mesgbody', nest: "I am Fluttering with Dart");
    builder.element('Numbers', nest: 5);
  });
  var bookshelfXml = builder.build();
  String _uriMsj = bookshelfXml.toString();
  print("_uriMsj: $_uriMsj");

  String _uri = "https://*******.******.com/http****";
  var _responseOtp = postOTP(_uri, _uriMsj);
  print("_responseOtp: $_responseOtp");
}


**POST XML:**
 // TODO: POST XML FILE
Future<String> postOTP(String _uri, String _message) async {
  HttpClient client = new HttpClient();
  HttpClientRequest request = await client.postUrl(Uri.parse(_uri));
  request.write(_message);
  HttpClientResponse response = await request.close();
  StringBuffer _buffer = new StringBuffer();
  await for(String a in await response.transform(utf8.decoder)) {
    _buffer.write(a);
  }
  print("_buffer.toString: ${_buffer.toString()}");
  return _buffer.toString();
}

这篇关于如何在Flutter中发送或接收xml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆