Can't access my laptop's localhost through an Android app

假如想象 提交于 2020-01-24 23:21:07

问题


So I did a research before posting this and the solutions I found didn't work, more precisely:

-Connecting to my laptop's IPv4 192.168.XXX.XXX - didn't work

-Conecting to 10.0.2.2 (plus the port) - didn't work

I need to test an API i built using Django Rest framework so I can get the json it returns, but i cant access through an Android app i'm building (I'm testing with a real device, not an emulator). Internet permissions are set on Manifest and i can access remote websites normally. Just can't reach my laptop's localhost(they are in the same network)

I'm pretty new to Android and Python and Django as well (used to built Django's Rest Framework API).

EDIT: I use localhost:8000/snippets.json or smth like this to connect on my laptop.

PS: I read something about XAMP server... do I need it in this case?

Thanks in advance


回答1:


Have you started the server like this?

python manage.py runserver 0.0.0.0:8000

Then, try to connect to 192.168.XXX.XXX:8000




回答2:


I tried the above, but failed to work in my case. Then with running python manage.py runserver 0.0.0.0:8000 I also had to add my IP to ALLOWED_HOSTS in settings.py, which solved this issue.

eg. Add your ip to allowed hosts in settings.py ALLOWED_HOSTS = ['192.168.XXX.XXX']

Then run the server python manage.py runserver 0.0.0.0:8000



来源:https://stackoverflow.com/questions/23716724/cant-access-my-laptops-localhost-through-an-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!