#React React-Native存取URL報錯TypeError: Network request failed[已解決]
長庚大學
我在寫一個簡單的登入功能,在使用API 的時候出現TypeError: Network request failed 的exception。
我問過chatGPT跟 搜尋過網絡,大部分的資訊都指向需要在AndroidManifest.xml 新增android: networkSecuituConfiguration,也找到相關問題串:可是我的android/app/src/main 根本沒有xml這個資料夾,但我還是根據上面連結的方法新增xml folder 並創建react_native_config.xml,結果卻出現以下錯誤:
FAILURE: Build completed with 2 failures.
1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':app:processDebugMainManifest'.
> Manifest merger failed : Attribute application@networkSecurityConfig value=(@xml/react_native_config) from
AndroidManifest.xml:17:7-67 is also present at AndroidManifest.xml:17:7-67 value=(@xml/network_security_config).
Suggestion: add 'tools:replace="android:networkSecurityConfig"' to <application> element at AndroidManifest.xml:7:5-13:19 to override.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
==============================================================================
2: Task failed with an exception.
-----------
* What went wrong:
java.lang.StackOverflowError (no error message)
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
——————————————————————
錯誤訊息指android: networkSecuituConfiguration 重複了,我按建議新增tools:replace 卻會出現另一個錯誤。
這是我的react native project info:
System:
OS: macOS 13.4
CPU: (8) arm64 Apple M1
Memory: 90.45 MB / 16.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.14.0 - ~/.nvm/versions/node/v18.14.0/bin/node
Yarn: Not Found
npm: 9.3.1 - ~/.nvm/versions/node/v18.14.0/bin/npm
Watchman: 2023.05.22.00 - /opt/homebrew/bin/watchman
Managers:
CocoaPods: 1.12.1 - /opt/homebrew/bin/pod
SDKs:
iOS SDK:
Platforms: DriverKit 22.1, iOS 16.1, macOS 13.0, tvOS 16.1, watchOS 9.1
Android SDK:
API Levels: 32, 33
Build Tools: 30.0.3, 33.0.0, 34.0.0
System Images: android-33 | Google APIs ARM 64 v8a
Android NDK: Not Found
IDEs:
Android Studio: 2022.2 AI-222.4459.24.2221.9971841
Xcode: 14.1/14B47b - /usr/bin/xcodebuild
Languages:
Java: 18.0.2.1 - /usr/bin/javac
npmPackages:
@react-native-community/cli: Not Found
react: 18.2.0 => 18.2.0
react-native: 0.71.10 => 0.71.10
react-native-macos: Not Found
npmGlobalPackages:
*react-native*: Not Found
——————————————————————
請問我有漏掉甚麼嗎?
2023/06/28更新:我發現這是因為debug/AndroidManifest.xml和main/AndroidManifest.xml沒有相同的用戶權限配置,導致Manifest merger 合併失敗。我將所有用戶權限複製到 debug/AndroidManifest.xml,現在可以使用HTTP fetch了。
