无法使用 Flutter 调用 Localhost,随机端口被分配给 HTTP GET 调用
Unable to make calls to Localhost using Flutter, random port being assigned to HTTP GET call
我正在尝试构建一个 Flutter 应用程序,我需要使用 dart http library 进行 HTTP 调用。所以这是我用来打电话的 fu 的片段,
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
Future<List> getData() async {
List data = new List();
var httpClient = new HttpClient();
var request = await httpClient.get("localhost", 5000, '/search?query=hello');
var response = await request.close();
if (response.statusCode == HttpStatus.OK) {
var jsonString = await response.transform(utf8.decoder).join();
data = json.decode(jsonString);
print(data);
return data;
} else {
return data;
}
}
现在从我的代码调用 getData()
,这是我得到的错误
E/flutter (30949): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter (30949): SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 46999
E/flutter (30949): #0 _HomePageState.getData (file:///home/bholagabbar/AndroidStudioProjects/fnd_modile_client/lib/main.dart:75:19)
E/flutter (30949): <asynchronous suspension>
E/flutter (30949): #1 _HomePageState.build.<anonymous closure> (file:///home/bholagabbar/AndroidStudioProjects/fnd_modile_client/lib/main.dart:56:15)
E/flutter (30949): #2 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:478:14)
E/flutter (30949): #3 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:530:30)
E/flutter (30949): #4 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter (30949): #5 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161:9)
E/flutter (30949): #6 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:94:7)
E/flutter (30949): #7 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
E/flutter (30949): #8 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
E/flutter (30949): #9 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
E/flutter (30949): #10 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:143:19)
E/flutter (30949): #11 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
E/flutter (30949): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
E/flutter (30949): #13 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
E/flutter (30949): #14 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
E/flutter (30949): #15 _invoke1 (dart:ui/hooks.dart:134:13)
E/flutter (30949): #16 _dispatchPointerDataPacket (dart:ui/hooks.dart:91:5)
我的 API 已启动并且 运行 正确,如您在错误日志中所见,请求似乎没有通过。这里的问题到底是什么?
郑重声明,我已经尝试了 get
、getUrl
的所有排列,甚至尝试用 10.0.2.2
替换 localhost
和 127.0.0.1
,如。我直接在 phone 上测试这个 (One Plus 3)
应该这样做
adb reverse tcp:5000 tcp:5000
https://developer.android.com/studio/command-line/adb.html
或使用adb reverse --list
查看设置了哪些重定向。
Günter 的回答是正确的,但您需要记住一件事,那就是不要忘记更改端口号
例如,如果您的端口是 3002,则使用:
adb reverse tcp:3002 tcp:3002
请记住,上述命令会将您的 phone 端口 3002 重定向到您计算机的端口 3002。
如果您想将 phone 的端口 3000 重定向到计算机的端口 3002,则命令如下所示
adb reverse tcp:3000 tcp:3002
更一般的答案是:
adb reverse tcp:<Your Phone's Port No.> tcp:<Your Computer's Port No>
我正在尝试构建一个 Flutter 应用程序,我需要使用 dart http library 进行 HTTP 调用。所以这是我用来打电话的 fu 的片段,
import 'package:flutter/material.dart';
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
Future<List> getData() async {
List data = new List();
var httpClient = new HttpClient();
var request = await httpClient.get("localhost", 5000, '/search?query=hello');
var response = await request.close();
if (response.statusCode == HttpStatus.OK) {
var jsonString = await response.transform(utf8.decoder).join();
data = json.decode(jsonString);
print(data);
return data;
} else {
return data;
}
}
现在从我的代码调用 getData()
,这是我得到的错误
E/flutter (30949): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter (30949): SocketException: OS Error: Connection refused, errno = 111, address = localhost, port = 46999
E/flutter (30949): #0 _HomePageState.getData (file:///home/bholagabbar/AndroidStudioProjects/fnd_modile_client/lib/main.dart:75:19)
E/flutter (30949): <asynchronous suspension>
E/flutter (30949): #1 _HomePageState.build.<anonymous closure> (file:///home/bholagabbar/AndroidStudioProjects/fnd_modile_client/lib/main.dart:56:15)
E/flutter (30949): #2 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:478:14)
E/flutter (30949): #3 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:530:30)
E/flutter (30949): #4 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter (30949): #5 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161:9)
E/flutter (30949): #6 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:94:7)
E/flutter (30949): #7 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
E/flutter (30949): #8 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
E/flutter (30949): #9 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
E/flutter (30949): #10 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:143:19)
E/flutter (30949): #11 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
E/flutter (30949): #12 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
E/flutter (30949): #13 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
E/flutter (30949): #14 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
E/flutter (30949): #15 _invoke1 (dart:ui/hooks.dart:134:13)
E/flutter (30949): #16 _dispatchPointerDataPacket (dart:ui/hooks.dart:91:5)
我的 API 已启动并且 运行 正确,如您在错误日志中所见,请求似乎没有通过。这里的问题到底是什么?
郑重声明,我已经尝试了 get
、getUrl
的所有排列,甚至尝试用 10.0.2.2
替换 localhost
和 127.0.0.1
,如
应该这样做
adb reverse tcp:5000 tcp:5000
https://developer.android.com/studio/command-line/adb.html
或使用adb reverse --list
查看设置了哪些重定向。
Günter 的回答是正确的,但您需要记住一件事,那就是不要忘记更改端口号 例如,如果您的端口是 3002,则使用:
adb reverse tcp:3002 tcp:3002
请记住,上述命令会将您的 phone 端口 3002 重定向到您计算机的端口 3002。
如果您想将 phone 的端口 3000 重定向到计算机的端口 3002,则命令如下所示
adb reverse tcp:3000 tcp:3002
更一般的答案是:
adb reverse tcp:<Your Phone's Port No.> tcp:<Your Computer's Port No>