简体   繁体   English

如何在颤振/飞镖中发出 HTTP GET 请求

[英]How to make HTTP GET request in flutter/dart

I am trying to make this GET request in my app but I am not sure how to go about it as I'm fairly new to flutter/dart.我正在尝试在我的应用程序中发出这个 GET 请求,但我不确定如何去做,因为我对颤振/飞镖还很陌生。

I have successfully made a POST request to log in and now I am trying to make this GET request to display the messages in this message board in my app.我已成功发出 POST 请求以登录,现在我正在尝试发出此 GET 请求以在我的应用程序的此留言板中显示消息。 That can come later as I am trying to complete the GET request first.这可能会在我尝试先完成 GET 请求时出现。

Preferably the GET request should be in a method which I can use in a button; GET 请求最好采用我可以在按钮中使用的方法; each will be in messageBoard_Page.dart每个都在 messageBoard_Page.dart

This is the request URL that I am trying to reach.这是我要访问的请求 URL。

请求网址

and this is the Request Header这是请求标头

请求标头

for reference this is the response that I am getting from my Login POST method供参考,这是我从登录 POST 方法中得到的响应

{
   "RESPONSE":{
      "login_request_result":{
         "user_must_change_password":"0"
      },
      "BASEL_RESPONSE":{
         "UserDate":"0",
         "UserTime":"0",
         "UserName":"Administrator",
         "module_config_1":"0",
         "module_config_2":"0",
         "ErrEntity":{
            "MessageID":"0",
            "last_req_id":"50029",
            "table_id":"0",
            "key_id_list":"536871",
            "operation_id":"0"
         },
         "is_csv":"0",
         "VersionName":"DYMA @ 6.1.24.0, ORG @ 2017.3.22.15.0.41, GRC @ 2017.3.22.15.0.55, LDC @ 2017.3.22.15.1.8, DYMA_XML @ 2017.3.22.15.0.30, NAS @ 2017.3.22.15.1.22 - Config: 0 - Node: OPRISK_DATACOLLECTOR",
         "ExpiryDate":"31/01/2030",
         "count_key":"0",
         "id_Us":"1",
         "is_popup":"0",
         "tot_messages":"0",
         "my_messages":"0",
         "product":"0"
      },
      "RESPONSE_HEADER":{
         "SessionID":"VtVQIdERO-206868772kpwAXF0001",
         "NomeRichiesta":"LOGIN_REQUEST",
         "ltimeStart":"22262791",
         "ltimeStop":"22262813",
         "ldate_null":"19900101",
         "product":"1",
         "server_name":"OPRISK_DATACOLLECTOR",
         "cell_context_id":"537945",
         "operation_key":"1000000",
         "operation_sub_num":"-1"
      }
   }
}

and this is my Login POST Method in login_Page.dart这是我在 login_Page.dart 中的登录 POST 方法

void sendLogin() async {
    var map = <String, dynamic>{
      "UserName": _usernameController.text,
      "Password": _passwordController.text,
    };

    var res = await http.post(
      Uri.parse("http://192.168.1.8:8080/HongLeong/LOGIN_REQUEST.do"),
      body: map,
    );

    final data = jsonDecode(res.body);


    final String userSessionID = (data as Map)['RESPONSE']['RESPONSE_HEADER']['SessionID'];

    print(res.statusCode);
    print(res.body);


    await WriteCache.setString(key: "cache", value: userSessionID);




    if ((data as Map)['RESPONSE']['login_request_result'] == null) {
      showDialog(
        context: context,
        builder: (ctx) => AlertDialog(
          title: const Center(child: Text('Invalid Username/Password')),
          actions: <Widget>[
            Center(
              child: TextButton(
                onPressed: () {
                  _usernameController.clear();
                  _passwordController.clear();
                  Navigator.of(ctx).pop();
                },
                child: const Text('Ok'),
              ),
            ),
          ],
        ),
      );
    } else {
      Navigator.push(context,
          MaterialPageRoute(builder: (context) =>  const DashboardPage()));
    }
  }

please ignore the messiness as Im trying to achieve the GET method as of now.请忽略混乱,因为我现在正试图实现 GET 方法。

Any help is appreciated.任何帮助表示赞赏。 Thank You.谢谢你。

ps.附言。 if the images are too small, I am not sure how else to show the request URL and header, sorry.如果图像太小,我不确定如何显示请求 URL 和标头,抱歉。

To make a get request its very much similar to the post you created.要发出获取请求,它与您创建的帖子非常相似。 Just remove the body and change the request type只需删除正文并更改请求类型

var res = await http.get(
      Uri.parse("The url for getting the respose through get method here")
    );

Rest everything stays the same.休息一切都保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM