简体   繁体   English

Flutter App无法在iOS模拟器上发出HTTP请求

[英]Flutter App failed to make HTTP request on iOS simulator

I tried to make a simple HTTP get request using Dio in a Flutter App like this: 我试图在Flutter应用中使用Dio发出一个简单的HTTP get请求,如下所示:

Dio dio = new Dio();
Response response = await dio.get('https://www.baidu.com');
print(response.data.toString());

It works perfect on a real iOS device, but time out on iOS simulators. 它在真正的iOS设备上完美运行,但在iOS模拟器上超时。

SocketException: OS Error: Operation timed out, errno = 60

I also tried other packets such as http , but still time out. 我也尝试了其他数据包,例如http ,但仍然超时。 Native apps running on the simulator doesn't have the same problem. 在模拟器上运行的本机应用程序没有相同的问题。 What can I do to fix it? 我该如何解决?

If none of the API's are working in simulator, I assume this is due to your systems proxy settings since it uses your machines internet. 如果没有一个API在模拟器中运行,则我认为这是由于您的系统代理设置所致,因为它使用了您的计算机网络。 Please have a look. 请看一看。

I was able to get it working with Dio using the below code in iOS simulator, if you want to see how http work check the cookbook , also try connecting to an open network like home wifi or something and see... 我能够使用iOS模拟器中的以下代码与Dio使用,如果您想查看http如何检查菜谱 ,还可以尝试连接到开放式网络(如家庭wifi或其他设备),然后查看...

Note: This is a piece from iOS/ Android flutter project 注意:这是iOS / Android flutter项目的一部分

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {

  fetchData()async {

    Dio dio = new Dio();
    Response response=await dio.get('https://www.baidu.com');
    print(response.data);
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Fetch Data Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: Text('Fetch Data Example'),
        ),
        body: Center(
          child: new IconButton(icon:new Icon(Icons.all_inclusive) , onPressed: (){
            fetchData();
          })
        ),
      ),
    );
  }
}

RESPONSE 响应

flutter: <!DOCTYPE html>
<!--STATUS OK-->

<html>
<head>

    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta content="always" name="referrer">
    <meta name="theme-color" content="#2932e1">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索" />
    <link rel="icon" sizes="any" mask href="//www.baidu.com/img/baidu_85beaf5496f291521eb75ba38eacbd87.svg">


    <link rel="dns-prefetch" href="//s1.bdstatic.com"<…>

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

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