如何让我的物理设备 运行 React Native on Expo Go 与我的应用 rails 后端 api 通信?

How do I get my physical device running React Native on Expo Go to communicate with my app's rails backend api?

我目前正在尝试 运行 我的 react-native/rails 应用程序在我的 phone 上进行测试。我可以 运行 我的登录和登录屏幕正常,因为在输入用户信息之前它们不会与我的服务器通信。当 运行 连接我的服务器时,我使用: $ rails s --binding=0.0.0.0

除了知道没有与我的服务器通信外,我没有收到任何错误。这一切在我的 Android Studio 模拟器上也能正常工作。

// one of my fetch GET requests

export function requestCurrentUser(username, auth_token) {
  return function action(dispatch) {
    const request = fetch(`'http://10.0.2.2:3000'/users/${username}`, {
      method: 'GET',
        headers: {
          "Authorization": auth_token
        }
    });
    return request.then(
      response => response.json(),
      err => console.log("user error"),
    )
    .then(
      json => dispatch(receiveCurrentUser({id: json.id, email: json.email, username: json.username})),
      err => console.log("user json error"),
    );
  }
}

我已经尝试将我的 Phone IP 设置更改为 10.0.2.2 网关,并在我的获取请求中使用我的 phone 的 IP。我觉得我在概念上缺少一些东西。提前致谢。

在提取请求中,您需要使用 运行 rails 服务器机器的 IP, 可能是你的笔记本,并使用相同的网络连接你的应用程序和你的 rails 后端 api。为了测试,您可以尝试在您的 phone 浏览器访问 http://IP_FROM_RAILS_MACHINE:3000

中直接访问您的 api