I am new to Django. I have created an API named http://127.0.0.1:8000/api/update/1/
and it works perfectly in the browser and on the Postman app.
However, when I am trying to access the API from the Android app it is returning NULL.
The android code works when the API is the following.
http://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=8190df9eb51445228e397e4185311a66
However, it does not work when the API is the following, even if the following API works just fine from my browser running in the same PC and from the Postman application.
http://127.0.0.1:8000/api/update/1/
I am attaching the code where API call is made.
protected String doInBackground(String... args) {
String xml = "";
String api = "http://127.0.0.1:8000/api/update/1/"; // returns NULL works in Postman app and in the browser
// String api = "http://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=8190df9eb51445228e397e4185311a66"; // works in all places
String urlParameters = "";
xml = Function.excuteGet(api, urlParameters);
return xml;
}
Can anyone help me with this? Thanks in advance.
On your mobile device's browser (any will work), navigate to http://<Local IP Address>:<port number> . For example, if I was serving on localhost:8080 and my local IP address is 123.45. 67.890, on my mobile device's browser I would navigate to http://123.45.67.890:8080 .
If you're using Android Studio to run the emulator, then localhost of your host computer will be mapped to the IP address, 10.0. 2.2 , inside the emulator. If you're using other programs to run the emulator, then you may need to consult the documentation associated with those programs.
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 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 the 8000
port. If it is found blocking, then please allow an incoming request to the 8000
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" />
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With