报错:
flutter: Connection refused, errno = 111, address = 127.0.0.1, port = 38870
解决方法:
原因原来是127.0.0.1 和localhost 指向的是虚拟机的地址,并非电脑本地。
https://stackoverflow.com/questions/6760585/accessing-localhostport-from-android-emulator
所以我们只需要修改接口的127.0.0.1或localhost就行了;在Android操作系统中,将本地电脑映射为10.0.2.2;亦或使用电脑ip地址。
I am using Windows 10 as my development platform, accessing 10.0.2.2:port in my emulator is not working as expected, and the same result for other solutions in this question as well.
After several hours of digging, I found that if you add -writable-system argument to the emulator startup command, things will just work.
You have to start an emulator via command line like below:
emulator.exe -avd <emulator_name> -writable-system
Then in your emulator, you can access your API service running on host machine, using LAN IP address and binding port:
http://192.168.1.2:<port>
Hope this helps you out.
About start emulator from command line: https://developer.android.com/studio/run/emulator-commandline.
I had the same issue when I was trying to connect to my IIS .NET Webservice from the Android emulator.
- install
npm install -g iisexpress-proxy
iisexpress-proxy 53990 to 9000
to proxy IIS express port to 9000 and access port 9000 from emulator like"http://10.0.2.2:9000"
the reason seems to be by default, IIS Express doesn’t allow connections from network https://forums.asp.net/t/2125232.aspx?Bad+Request+Invalid+Hostname+when+accessing+localhost+Web+API+or+Web+App+from+across+LAN