React Native: fetch request failed with error - TypeError: Network request failed(…)

前端 未结 2 1492
日久生厌
日久生厌 2020-12-15 22:00

I am developing a simple app using React Native. I am testing it on Android device. I have created a Node.js server to listen to the requests, it is running at http://localh

相关标签:
2条回答
  • 2020-12-15 22:32

    Since your Android device has an IP of its own, you need to point the URL to your computers IP address instead of just localhost. For example fetch('http://192.168.0.2:3333/').

    0 讨论(0)
  • 2020-12-15 22:35

    Use the reverse command of the Android Debug Bridge tool (adb):

    adb reverse <remote> <local> - reverse socket connections.
                                   reverse specs are one of:
                                     tcp:<port>
                                     localabstract:<unix domain socket name>
                                     localreserved:<unix domain socket name>
                                     localfilesystem:<unix domain socket name>
    

    For example:

    adb reverse tcp:3333 tcp:3333
    

    This makes localhost:3333 accessible from your device. You can also use different ports. For example:

    adb reverse tcp:8081 tcp:3333
    

    This will redirect the device port 8081 to the local port 3333.

    0 讨论(0)
提交回复
热议问题