簡體   English   中英

Flutter App無法在iOS模擬器上發出HTTP請求

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

我試圖在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 ,但仍然超時。 在模擬器上運行的本機應用程序沒有相同的問題。 我該如何解決?

如果沒有一個API在模擬器中運行,則我認為這是由於您的系統代理設置所致,因為它使用了您的計算機網絡。 請看一看。

我能夠使用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"<…>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM