在真实的Android设备上运行本机应用程序时无法与服务器连接 [英] Unable to connect with server while running react native app on real android device

查看:41
本文介绍了在真实的Android设备上运行本机应用程序时无法与服务器连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的真实 android 设备上运行我的本机应用程序,因为笔记本电脑在使用 Android studio 时变得太热(8 Gb RAM,很痛苦).因此,我启用了我的 USB 调试,插入电缆并运行命令react-native run-android".该应用程序启动时没有问题.我也启动了后端服务器,它们在 localhost:3000 和 localhost:3001 上启动并运行.该应用程序运行并迎接登录和注册页面.我尝试注册,但出现错误提示

I am trying to run my react native app on my real android device as the laptop gets too hot while using Android studio (8 Gb RAM, the pain). Hence, I enabled my USB debugging, plugged the cable and ran the command "react-native run-android". The app started with no issue. I started the backend server also which are up and running on localhost:3000 and localhost:3001. The app runs and greets with a login and sign up page. I tried to sign up but there is an error saying

错误[类型错误:网络请求失败]

Error [TypeError: Network request failed]

我在同一网络上通过 WIFI 连接,希望服务器和应用程序可以通信,但应用程序不能.

I am connecting via WIFI on the same network and hoping the server and the app can communicate but the app doesn't.

代码本身没有任何问题,因为我之前在 android studio 中多次运行它并且应用程序运行良好.我能够以新用户的身份注册并使用相同的 ID 登录.我无法在真实设备上做同样的事情.

There is not any issue with the code itself as I ran it multiple times in android studio previously and the app ran well. I was able to sign up as a new user and login with the same id. I am not able to do the same in the real device.

我的问题:

  1. 我阅读了官方文档,它说打开应用内开发者菜单并输入机器IP地址和本地主机服务器.这个本地主机服务器是运行 Metor JS 的服务器还是应用程序本身的后端服务器?

  1. I read the official document and it said to open in-app developer menu and put the machine ip address and localhost server. Is this localhost server the one that the Metor JS runs on or the backend server of the app itself?

我在这里遗漏了什么,任何命令或任何配置?

What am I missing here, any commands or any configuration?

为什么我的应用无法与服务器通信?

Why is my app not able to communicate with the server?

网络错误背后的原因.

推荐答案

您应该按照以下步骤连接捆绑程序.

You should follow these steps to connect with the bundler.

  1. 确保应用程序和捆绑程序使用同一网络.

  1. Make sure that the app and bundler using the same network.

如果您使用的是 android,请将此代码添加到您的 androidMenifest.xml 文件中.

If you are using android add this code in your androidMenifest.xml file.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission 
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <application
   android:usesCleartextTraffic="true" // make sure that this line is added.
     .......>
      .......
 </application>

  • 如果您在开发者设置中添加 localhost,请尝试像这样添加您的 IP 地址,yourIP:yourPort

  • if you are adding localhost in the developer setting try to add your IP address like this, yourIP:yourPort

    示例:192.168.89.24:8081

    这篇关于在真实的Android设备上运行本机应用程序时无法与服务器连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆