【问题标题】:Connection refused when trying to connect local host with my own <keyvaluepair> values尝试使用我自己的 <keyvaluepair> 值连接本地主机时连接被拒绝
【发布时间】:2019-03-08 09:44:19
【问题描述】:

我想连接我的本地主机并使用我的参数获取一个令牌。当我使用此代码在我的设备中连接时,我遇到错误连接被拒绝!

这是我的代码。

class _MyHomePageState extends State<MyHomePage> {

      var formContent = {
        "grant_type": "1",
        "branchcode": "0",
        "password": "1",
        "username": "1",
        "dbname": "1",
        "dbuser": "1",
        "dbpassword": "1",
        "dbtype": "0"
      };

      Future<String> getData() async {
        var postBody= json.encode(formContent);
        var response = await http.post(
            Uri.encodeFull("http://localhost:7070/api/v2/token"),
            body:postBody,
            headers: {"Accept": "application/json"});
        if(response.statusCode == 200){
          return String.fromCharCode(json.decode(response.body));
        }else{
          throw Exception('Failed');
        }
      }

      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new Center(
            child: new RaisedButton(
              child: new Text("Get The Data"),
              onPressed: getData,
            ),
          ),
        );
      }
    }

我无法将我的本地主机与我自己的值连接起来。我有一个错误,比如连接被拒绝,但我不明白为什么?

Unhandled Exception: SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 40109

【问题讨论】:

  • 您知道localhost 是手机/模拟器,而不是您的开发PC?如果您尝试连接的服务在该 IP 上侦听并且您的手机连接到同一网络 (WiFi),则您可以使用 adb 创建重定向或使用 PC 的公共 IP 地址从手机连接。
  • 是的,我知道,那我怎么解决呢?
  • stackoverflow.com/questions/49855754/… 可能会有所帮助,而不是如上所述使用localhost
  • 我无法理解我如何使用这个 adb reverse —list 命令
  • 我不明白我到底在做什么

标签: android ios dart flutter socketexception


【解决方案1】:

给出的错误通常是由于设备/模拟器没有网络连接,或者给定的端点无法访问(即服务器离线,地址不正确)引起的。如果问题是由后者引起的,您可以尝试将根地址从“localhost”替换为“10.0.2.2”。这是因为模拟器拦截了地址并将其转换为“127.0.0.1” - 此处解释了有关此的更多详细信息https://developer.android.com/studio/run/emulator-networking

【讨论】:

    猜你喜欢
    • 2013-07-08
    • 2015-07-30
    • 2017-11-22
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 2014-05-31
    相关资源
    最近更新 更多