【问题标题】:Flutter: How to send response in raw body data?Flutter:如何在原始身体数据中发送响应?
【发布时间】:2020-01-19 10:44:57
【问题描述】:

This is ScreenShot of Response of raw body data

正文原始数据:

{"getOpenQuiz":[{"Id":1,"openquizInstruction":"Copy the following word","openquizWord":"Important","possible_answers":["Important","Importnat","import"],"quizstatus":1,"userAnswer":"important"},{"Id":2,"openquizInstruction":"test","openquizWord":"test","possible_answers":["test","testty","adsf","dfgdfg"],"quizstatus":0,"userAnswer":"tttt"},{"Id":3,"openquizInstruction":"dfgdfg","openquizWord":"dsfgsd","possible_answers":["fgsdf","gsdfg","dfgrertert","sdfgsdfg"],"quizstatus":0,"userAnswer":"dsfgsd"},{"Id":4,"openquizInstruction":"Copy the following word","openquizWord":"Important","possible_answers":["Important","Importat","Importnt"],"quizstatus":0,"userAnswer":"importnat"},{"Id":5,"openquizInstruction":"Copy the word","openquizWord":"color","possible_answers":["colour","color"],"quizstatus":0,"userAnswer":"coluor"}]}

【问题讨论】:

  • 你想get那个json还是post那个json。
  • @aligator 我想发布该 json 原始数据以获取 API 的响应

标签: api flutter xmlhttprequest response flutter-test


【解决方案1】:

尝试使用HTTP plugin,REST 很简单。

如何做的例子:

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Center(
          child: FlatButton(
            child: Text('post'),
            onPressed: (){
              post('https://www.example.com/api',
                body: jsonEncode({ // body of post
                  'title' : 'test title'
                })
              ).then((response){
                if(response.statusCode==200){
                  String body = response.body; // body of response
                  Map<String,dynamic> decodedJson = jsonDecode(body);
                  print(decodedJson);
                }
              });
            },
          ),
        ),
      ),
    );
  }
}

【讨论】:

  • 此代码不起作用,我想在正文中传递响应然后我得到响应,该响应必须显示在结果页面中。
猜你喜欢
  • 2017-08-20
  • 2021-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-26
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多