很多人運行flutter項目時,出現(xiàn)如下錯誤
Error connecting to the service protocol: HttpException: Connection closed before full header was received, uri = http://127.0.0.1:54627/p1NdakJReqU=/ws
這個問題目前發(fā)現(xiàn)在安卓模擬器上出現(xiàn)頻率較多亡容,之前為了簡單方便,都建議使用真機(jī)測試冤今。
今天有時間尋找研究一下這個問題闺兢,多謝網(wǎng)友【itskamui】(網(wǎng)名)的協(xié)作一起研究。
情況1:
?
可以看到日志里面有
No connected devices
戏罢,判斷應(yīng)該是模擬器沒有鏈接成功屋谭,可以嘗試各種adb devices -l
或者
flutter devices
查看是不是能看到模擬器,如果看不到龟糕,重啟模擬器桐磁,再試。
情況2:
通過正常的flutter run運行得到簡單日志
Launching lib/main.dart on Android SDK built for x86 in debug mode...
Initializing gradle...
Resolving dependencies...
Running Gradle task 'assembleDebug'...
Built build/app/outputs/apk/debug/app-debug.apk.
Installing build/app/outputs/apk/app.apk...
Error connecting to the service protocol: HttpException: Connection closed before full header was received, uri = http://127.0.0.1:54627/p1NdakJReqU=/ws
使用flutter run -v可以輸出更完整的日志
....
[ +1 ms] Stopping app 'app.apk' on Android SDK built for x86.
[ ] executing: /Users/wei/Library/Android/sdk/platform-tools/adb -s emulator-5554 shell am force-stop pt.tribeiro.flutter_pdf_viewer_example
[ +89 ms] executing: /Users/wei/Library/Android/sdk/platform-tools/adb -s emulator-5554 shell pm list packages pt.tribeiro.flutter_pdf_viewer_example
[ +31 ms] package:pt.tribeiro.flutter_pdf_viewer_example
[ +3 ms] executing: /Users/wei/Library/Android/sdk/platform-tools/adb -s emulator-5554 shell cat
/data/local/tmp/sky.pt.tribeiro.flutter_pdf_viewer_example.sha1
[ +26 ms] 01742bfabba2b904071ee66da7111e5f6bd5e7f2
[ ] Latest build already installed.
[ ] Android SDK built for x86 startApp
[ +1 ms] executing: /Users/wei/Library/Android/sdk/platform-tools/adb -s emulator-5554 shell am start -a android.intent.action.RUN -f 0x20000000
--ez enable-background-compilation true --ez enable-dart-profiling true --ez enable-checked-mode true --ez verify-entry-points true
pt.tribeiro.flutter_pdf_viewer_example/pt.tribeiro.flutter_plugin_pdf_viewer_example.MainActivity
[ +65 ms] Starting: Intent { act=android.intent.action.RUN flg=0x20000000
cmp=pt.tribeiro.flutter_pdf_viewer_example/pt.tribeiro.flutter_plugin_pdf_viewer_example.MainActivity (has extras) }
[ +1 ms] Waiting for observatory port to be available...
[ +939 ms] Observatory URL on device: http://127.0.0.1:41575/z9Mve5wOFL8=/
[ +1 ms] executing: /Users/wei/Library/Android/sdk/platform-tools/adb -s emulator-5554 forward tcp:0 tcp:41575
[ +8 ms] 53415
[ ] Forwarded host port 53415 to device port 41575 for Observatory
[ +7 ms] Connecting to service protocol: http://127.0.0.1:53415/z9Mve5wOFL8=/
[ +22 ms] Error connecting to the service protocol: HttpException: Connection closed before full header was received, uri = http://127.0.0.1:53415/z9Mve5wOFL8=/ws
[ +3 ms] "flutter run" took 9,137ms.
[ ] "flutter run" took 9,137ms.
....
Dart Observatory (語句級的單步調(diào)試和分析器) 是調(diào)試工具也要靠這個工具啟動的服務(wù)來實現(xiàn)flutter熱加載讲岁,有興趣的可以查看文檔https://dart-lang.github.io/observatory/研究一下
所以一開始以為是端口問題我擂,使用參數(shù)--observatory-port=
指定了端口衬以,發(fā)現(xiàn)是無效的。
flutter sdk 的issues 上有好多類似問題校摩。
https://github.com/flutter/flutter/issues/6724
https://github.com/flutter/flutter/issues/13747
有說需要用PowerShell運行的看峻,經(jīng)過嘗試無效。
有說需要管理員身份運行的衙吩,嘗試無效互妓。
后來發(fā)現(xiàn)模擬器的系統(tǒng)鏡像版本是Android 10或者是Android Q或者是Android 9.+(api 29)
然后嘗試下載使用低版本的系統(tǒng),重新創(chuàng)建一個模擬器坤塞,問題就解決了冯勉。
猜測應(yīng)該是Android Q目前還是beta版本,所以都會出現(xiàn)這個問題摹芙,目前在Android Q下還是沒有辦法能解決這個問題珠闰。
還有不排除設(shè)置了代理的可能性。
所以目前解決這個問題的辦法是:
1瘫辩、使用低版本的系統(tǒng)鏡像
2伏嗜、使用真機(jī)吧、真機(jī)吧伐厌、真機(jī)吧