【问题标题】:How make local http request in flutter?如何在颤动中发出本地http请求?
【发布时间】:2020-06-22 22:09:15
【问题描述】:

我正在制作一个应用程序,并且我已经在本地运行了一个服务器。我可以通过导航器访问网址:

Request URL: http://localhost:4444/categorie?page_size=15&page=1
Request Method: GET
Status Code: 200 OK
Remote Address: 127.0.0.1:4444
Referrer Policy: no-referrer-when-downgrade

预览:

{
    "count": 4,
    "next": null,
    "previous": null,
    "results": [
        {
            "uid": "_b656062d3754",
            "name": "Pinga"
        },
        {
            "uid": "_0c644473c244",
            "name": "Cerveja"
        },
        {
            "uid": "_75df557ccbaa",
            "name": "Vinhos"
        },
        {
            "uid": "_8e32ce3baded",
            "name": "Refrigerantes"
        }
    ]
}

但是当我尝试颤动时,请求永远不会响应:

getCategories() async {

  String url = 'http://localhost:4444/categorie?page_size=15&page=1';

  var response = await http.get(url);

  if (response.statusCode == 200) {
    // If the call to the server was successful, parse the JSON
    return response.body;
  } else {
    // If that call was not successful, throw an error.
    throw Exception('Failed to load post');
  }
}

我正在使用Django 在我的Windows10 中运行服务器。我的颤振应用程序位于Android Studio 中,使用Virtual Machine 作为设备。 我尝试将 ip 更改为 127.0.0.110.0.0.1 但仍然无法正常工作。我是否更改了 Django 的主机或在我的颤振或 VirutalBox 内部启用任何配置?

  • 对其他站点的请求运行良好。问题出在本地。

我的依赖:

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2
  http: any

dev_dependencies:
  flutter_test:
    sdk: flutter

【问题讨论】:

  • @ViniciusMorais 我的回答解决了您的问题吗?如果是,请将其标记为正确。
  • 不抱歉,我不得不更改我的 Django 配置来解决我的问题并从 url 请求更改 IP。

标签: django flutter


【解决方案1】:

PC 上的 localhost 与 Android 模拟器上的不同。要从 Android 模拟器访问您 PC 的 localhost,您需要指向 10.0.2.2

这意味着您需要将代码更改为:

String url = 'http://10.0.2.2:4444/categorie?page_size=15&page=1';

【讨论】:

    【解决方案2】:

    在依赖项下的pubspec文件中使用这个:

    http: ^0.12.0+2
    

    此方法用于 Http 请求

    getCategories() async {
        final response = await http.get("http://localhost:4444/categorie?page_size=15&page=1",
    
          headers: {
            'content-type': 'application/json',
          },
        );
    
        if (response.statusCode == 200) {
          final result = json.decode(response.body);
    
          var list = result['results'] as List;
        //  make a model class
          List<Categories> categories_= list.map((i) => Categories.fromJson(i)).toList();
          setState(() => {
          // update value
          });
        } else {
     setState(() => {
           // Show error
          });
    
        }
    
      }
    

    模型类:

    class Categories{
      String uid;
      String name;
    
      Categories({this.uid, this.name});
    
      Categories.fromJson(Map<String, dynamic> json) {
        uid = json['uid'];
        name = json['name'];
      }
    
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['uid'] = this.uid;
        data['name'] = this.name;
        return data;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 2018-12-24
      • 2018-09-22
      • 1970-01-01
      • 2021-10-26
      • 1970-01-01
      相关资源
      最近更新 更多