我有一个 django REST 项目,它在我的笔记本电脑上本地运行。我正在开发一个 android 应用程序来使用此 django 项目提供的 api。我的笔记本电脑和 Android 设备都连接到同一个 wifi 网络。我正在使用 android volly 库来访问 api。我尝试过的是
从终端运行
ifconfig
并得到inet addr
。就这样吧192.168.1.8.
在android volly中使用了url
"http://192.168.1.8:8000/api_part/". (The project is running on port 8000)
那么我从 volly 得到的回应是
com.android.volley.NoConnectionError: java.net.ConnectException: failed to connect to /192.168.1.8 (port 8000) after 2500ms: isConnected failed: ECONNREFUSED (Connection refused)
我是否使用了错误的 url 来访问 api?如果不是,那么如何从 android 应用程序访问这个本地运行的项目?
确保使用正确的 IP 启动运行服务器或将其绑定到所有 IP:
$ python manage.py 0.0.0.0:8000
在代码编辑器(Pycharm、Vscode)终端中运行此命令
python manage.py runserver 0.0.0.0:8000
打开系统cmd 写
ipconfig
获取IP地址
像这样
IPv4 地址。 。 。 。 。 。 。 。 。 。 。 : 192.168.246.217
在setting.py类中编辑它 ALLOWED_HOSTS = ['localhost', '127.0.0.1', '192.168.246.217']
确保您的系统和手机连接到同一网络,或者您可以通过手机热点连接您的系统。