【问题标题】:Is there any good way do make a POST request in Flutter, to a JSON?有什么好的方法可以在 Flutter 中向 JSON 发出 POST 请求吗?
【发布时间】:2019-09-18 09:58:00
【问题描述】:

所以我有一个 Flutter 应用程序,它以 JSON 格式从 api 获取数据并在 listview.builder 中显示。我现在想发出 POST 请求并更改 JSON 中的数据。如果您知道如何执行此操作,请添加对此的回复:)

JSON:

[
{
    "id": 29,
    "description": "Berlinmurens Fall",
    "state": "started",
    "assigned_users": [
        {
            "username": "hugo",
            "fullname": "Hugo Johnsson"
        }
    ],
    "subtasks": []
},
{
    "id": 30,
    "description": "Kubakrisen + Konsekvenser",
    "state": "started",
    "assigned_users": [
        {
            "username": "studentone",
            "fullname": "Student One"
        }
    ],
    "subtasks": []
},
{
    "id": 31,
    "description": "Mutual Assured Destruction (MAD)",
    "state": "new",
    "assigned_users": [
        {
            "username": "hugo",
            "fullname": "Hugo Johnsson"
        },
        {
            "username": "studentone",
            "fullname": "Student One"
        },
        {
            "username": "studenttwo",
            "fullname": "Student Two"
        }
    ],
    "subtasks": [
        {
            "id": 3,
            "description": "Vad betyder MAD?"
        },
        {
            "id": 4,
            "description": "Vad har MAD att göra med Kalla Kriget?"
        },
        {
            "id": 5,
            "description": "Vem vann tillslut för att de fick kraften över MAD?"
        }
    ]
},
{
    "id": 32,
    "description": "Hur vann USA?",
    "state": "new",
    "assigned_users": [
        {
            "username": "hugo",
            "fullname": "Hugo Johnsson"
        },
        {
            "username": "studentone",
            "fullname": "Student One"
        }
    ],
    "subtasks": []
},
{
    "id": 33,
    "description": "Stilig Google Presentation",
    "state": "done",
    "assigned_users": [
        {
            "username": "hugo",
            "fullname": "Hugo Johnsson"
        },
        {
            "username": "studentone",
            "fullname": "Student One"
        },
        {
            "username": "studenttwo",
            "fullname": "Student Two"
        }
    ],
    "subtasks": []
},
{
    "id": 34,
    "description": "Vem var John F Kennedy?",
    "state": "done",
    "assigned_users": [
        {
            "username": "studentone",
            "fullname": "Student One"
        }
    ],
    "subtasks": []
}
]

我想改变“状态”。通过 POST 请求。有人可以告诉我怎么做吗?

我应该这样做吗?

_postreq() async {

var url ="--ADRESS--";

var response = await http.post(
    url,
    headers:{ "Accept": "application/json" } ,
    body: { "state": 1}, //key value
    encoding: Encoding.getByName("utf-8")
);

print("post");

return response;
}

带有单个任务的 JSON/29

GET /api/task/29/
HTTP 200 OK
Allow: GET, POST, PUT, DELETE, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
"id": 29,
"description": "Berlinmurens Fall",
"state": "started",
"assigned_users": [
    {
        "username": "hugo",
        "fullname": "Hugo Johnsson"
    }
],
"subtasks": []
}

和用户界面

_postreq() async {

var url ="http://studieplaneraren.pythonanywhere.com/api/task/29/";

var response = await http.post(
    url,
    headers:{ "Accept": "application/json" } ,
    body: { "state": "done",}, //key value
    encoding: Encoding.getByName("utf-8")
);

print("post");

return response;
}

后端代码

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="Mouldifi - A fully responsive, HTML5 based admin theme">
<meta name="keywords" content="Responsive, HTML5, admin theme, business, professional, jQuery, web design, CSS3, sass">
<title>{{projekt.titel}}</title>

【问题讨论】:

    标签: json post flutter request http-post


    【解决方案1】:

    试试这个:

    var url ="http://192.168.1.10/update.php";
    
    //Metodo post
    var response = await http.post(
        url,
        headers:{ "Accept": "application/json" } ,
        body: { "state": 1}, //key value
        encoding: Encoding.getByName("utf-8")
    );
    

    【讨论】:

    • 评论不适用于扩展讨论或调试会话;这个对话是moved to chat。请确保edit 将答案纳入讨论中的任何相关信息。
    猜你喜欢
    • 2019-08-27
    • 1970-01-01
    • 2021-06-26
    • 2014-04-05
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    相关资源
    最近更新 更多