简体   繁体   English

React Native 无法从另一台设备连接到本地主机

[英]React Native cannot connect to localhost from another device

I use Django rest framework as my API, however, I'm unable to connect to localhost from my android device (not an emulator).我使用 Django rest 框架作为我的 API,但是,我无法从我的 android 设备(不是模拟器)连接到 localhost。 I have gone through many questions on StackOverflow and Github but none worked我在 StackOverflow 和 Github 上遇到了很多问题,但都没有奏效

here is my code这是我的代码

import { create } from 'apisauce';

const apiClient = create({
    baseURL: 'http://tried-many-things-but-none-worked:8000',
});
// getting data is straight forward
import client from './client';
const getChat = id => client.get("/chat/get_chat/${id}/";

I tried many solutions but none worked for me.我尝试了很多解决方案,但没有一个对我有用。 I tried using我尝试使用

  • 10.0.2.2 but it worked for AVD only, 10.0.2.2 但它只适用于 AVD,
  • 10.0.3.2 worked for Genymotion only. 10.0.3.2 仅适用于 Genymotion。
  • localhost and 127.0.0.1 worked only in POSTMAN. localhost 和 127.0.0.1 仅适用于 POSTMAN。
  • also tried using the machine IP address but it keeps loading forever, I tried to wait for it to finish loading to end up with an error in both POSTMAN and my React Native app还尝试使用机器 IP 地址,但它一直在加载,我试图等待它完成加载,最终在 POSTMAN 和我的 React Native 应用程序中出现错误

And nothing worked for my external device!对我的外部设备没有任何作用! I would really appreciate any helpÏ我真的很感激任何帮助Ï

Please note that the problem isn't with the code as I tried to upload it to pythonanywhere web hosting service and used the URL I got from them and it was working fine with no errors (as well as from postman and the emulator)请注意,问题不在于代码,因为我试图将其上传到 pythonanywhere 网络托管服务并使用我从他们那里获得的 URL,并且它工作正常,没有错误(以及来自邮递员和模拟器)

Extra information: pc: macOS High Sierra version: 10.13.6, external device: Android额外信息:pc:macOS High Sierra 版本:10.13.6,外接设备:Android

You can use ngrok for this.您可以为此使用ngrok You will get a new domain name from ngrok instead playing localhost/ip etc. You can check this documentation for more info https://ngrok.com/docs您将从 ngrok 获得一个新域名,而不是播放 localhost/ip 等。您可以查看此文档以获取更多信息https://ngrok.com/docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM