4

我尝试在 Flutter App 中使用Dio发出一个简单的 HTTP 获取请求,如下所示:

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,但仍然超时。在模拟器上运行的本机应用程序没有同样的问题。我能做些什么来修复它?

4

2 回答 2

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"<…&gt;
于 2018-07-16T09:35:27.483 回答
0

如果没有一个 API 在模拟器中工作,我认为这是由于您的系统代理设置,因为它使用您的机器互联网。请看一看。

于 2018-07-16T09:34:31.083 回答