27π
If you are testing your application from a real android device then you need to put the IP address of your PC while you are trying to connect to your Django server through APIs. And yes, you need to be in the same network as well. So you need to check the following things.
- Make sure that the PC (where you are running the server) and the Android device (where you are testing your application) are in the same network (connected with same Wifi network maybe).
-
Make sure you are connecting to the IP address of your PC where the server is running. For example, right now, the IP address of your PC is
192.168.0.100
. Then, you need to connect to this IP address and call your API like the following.http://192.168.0.100:8000/api/update/1/
-
Make sure you are accepting requests to the port
8000
in your PC. Check your Firewall configuration if it is blocking any incoming requests to the8000
port. If it is found blocking, then please allow an incoming request to the8000
port using the following.sudo ufw allow 8000/tcp
If there is nothing which is helping you, then please check your Android code to check if the API calling is okay. I would strongly recommend using Volley for API calls suggested in developers documentation.
Last, but not the least, please check if you have necessary permission in your AndroidManifest.xml
file. You need to add the following permission to grant your application to use the internet.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
14π
To be able to connect your localhost (I assume you are using emulator, I can edit the answer if not)
You need to use the following url:
http://10.0.2.2:8000/
7π
Please run your django development server using the following command.
python manage.py runserver 0.0.0.0:8000
Once you get it running, in a new terminal window find out the ip address of your computer in your wifi/network subnet using the following command
Ipconfig or ifconfig (depends on your OS)
Then change the base url of your api from 127.0.0.1 to the ip you found in the above step. Now connect the android phone in which your app is being tested to your same wifi or network to which the computer running django is connected.
Now you can request and receive response.
127.0.0.1 is the home of your system, your android app will not be able to access that. You need to do it like this.
4π
1)Andoird manifest to put internet permission and network acceess
2) if using Android simulator on local machine. Use 10.0.2.2. alias created by default for 127.0.0.1 i.e losthoast.
Its works for me
- Managing multiple settings.py files
- Can't login to Django /admin interface
- Django i18n: how to not translate the admin site?
0π
The API isnβt working in POSTMAN I tried http://127.0.0.1:8000/api/update/1/ shows an error because it requires some params to be inputted.
in this case, http://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=8190df9eb51445228e397e4185311a66 the required params have been provided to the API, ie source, sortBy & apiKey
- Creating an entire web application using django admin
- Use of unicode in Django
- ASGI_APPLICATION not working with Django Channels
- Django can't access raw_post_data
0π
It is possible to call local api with some minor changes.These were the steps I did to hit the local django api from my android app.
- You need to be connected to same network. Better create a hotspot from your pc or laptop and connect android with it
- Create static IP from your system. If you are on VM you may require additional network adapter. Please google it as there are plenty of videos and blogs on the same.
- Host django on the created local IP.
You are then good to go with your android app.
- How to compare version string ("x.y.z") in MySQL?
- Django ALLOWED_HOSTS with ELB HealthCheck
- How to throttle Django error emails
- Method in Django to display all attributes' values belonging to a created object?
- TypeError: βDoesNotExistβ object is not callable
0π
You can not access a local api outside from that local device. 127.0.0.1 is the local address of its own. If you want to access the api outside from local device, replace the ip with the ip that is open to outside. For example, 192.168.43.2 is a ip that is accessible from outside. Update your api with the accessible ip(use ifconfig or ipconfig in terminal to obtain the ip and use that ip to access from outsid?). Also, please make sure if you have proper permission in AndroidManifest.xml
file.
- Whats the best way to duplicate data in a django template?
- Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
- TemplateDoesNotExist at / base.html
0π
For me, the problem was that my server was running on HTTP, not HTTPS. Once I set up my SSL certificate, everything worked fine. Iβm using Expo and strangely the API works when in development mode through the Expo app, but after I build an APK it fails to connect to any non-HTTPS server.
-1π
In local host You must start the server and also do app development in same system then only it works for you..
- Where are the files downloaded using pip stored in virtualenv?
- Django: IntegrityError during Many To Many add()
- Django prefetch_related's Prefetch, order_by?
- Foreign Key to User model
- Django no csrftoken in cookie
- Display foreign key columns as link to detail object in Django admin
- Pylint (pylint_django) does not work when βdjango-settings-module flag is specified
- Django template for loop
- Pycharm (Python IDE) doesn't auto complete Django modules
- Django template {%for%} tag add li every 4th element