Flutter应用在iOS模拟器上无法进行HTTP请求

4

我尝试在Flutter应用程序中使用Dio发起一个简单的HTTP get请求:

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

在真实的iOS设备上运行得非常完美,但在iOS模拟器上会超时。

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

我也尝试了其他的数据包,例如http,但仍然超时。在模拟器上运行的本地应用程序没有这个问题。

我该怎么办才能解决这个问题呢?


1
@Ramprasathselvam,正如您所看到的,我正在进行HTTPS请求。不是为了这个原因,谢谢。 - BeBeBerr
你解决了吗? - noveleven
Flutter的Http包无法使用系统代理。 - noveleven
2个回答

0
如果模拟器中没有任何API可用,我认为这是由于您的系统代理设置,因为它使用您机器的互联网。请检查一下。

0

我已经成功使用以下代码在iOS模拟器中使用Dio,如果您想了解如何使用http,请查看食谱,同时尝试连接到像家庭WiFi这样的开放网络并查看...

注意:这是来自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();
          })
        ),
      ),
    );
  }
}

响应

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"<…>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接