16

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.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Mazharul Islam
  • 187
  • 1
  • 1
  • 10
  • 1
    are you sure your device network connected with to pc? or turn off your firewall – Ali Radman Aug 17 '18 at 06:50
  • 2
    127.0.0.1 is the ip address of the device/pc itself. Not that of any other computer or device. Use the ip of your pc instead. – greenapps Aug 17 '18 at 07:01
  • `and it works okay in the browser `. In the browser on that pc. Not in a browser on your device. – greenapps Aug 17 '18 at 07:02
  • 2
    127.0.0.1 is ip address of the same device you are making request from. so requesting 127.0.0.1 from the phone will connect to the phone – Vladyslav Matviienko Aug 17 '18 at 07:16
  • 1
    Possible duplicate of [How to connect to my http://localhost web server from Android Emulator in Eclipse](https://stackoverflow.com/questions/5806220/how-to-connect-to-my-http-localhost-web-server-from-android-emulator-in-eclips) – Anjani Mittal Aug 17 '18 at 09:19
  • You may check this https://stackoverflow.com/questions/50294513/accessing-localhost-laravel-app-routes-to-android-studio-failed/57728814#57728814 – Saddan Aug 30 '19 at 15:01

10 Answers10

26

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.

  1. 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).
  2. 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/
    
  3. 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" />
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • 1
    Thanks! As a newbie, this really helping me a lot. – kw88 Nov 05 '19 at 14:29
  • Glad to know that I could help. You are most welcome! – Reaz Murshed Nov 05 '19 at 15:11
  • @ReazMurshed Hi ,I have tried what you suggested. But still facing issue.https://stackoverflow.com/questions/63610468/not-able-to-get-local-server-json-response-in-androidjava-net-connectexception?noredirect=1#comment112491960_63610468 – GNK Aug 27 '20 at 12:58
  • how to figure out ip of my laptop? can i connect to my laptop from my phone if both devices use the same wifi ? – mrtechmaker Jul 15 '21 at 18:08
  • https://stackoverflow.com/questions/13322485/how-to-get-the-primary-ip-address-of-the-local-machine-on-linux-and-os-x You can find something similar if you are on Windows. – Reaz Murshed Jul 15 '21 at 21:15
  • Thank you for this! Do you happen to know if there is an option to recognize that you connect real device or emulator? Like maybe some compiler directive? – Adam Jachocki Dec 27 '21 at 15:41
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/
keser
  • 2,472
  • 1
  • 12
  • 38
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.

Arun T
  • 1,114
  • 6
  • 17
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

Umesh Bhutada
  • 301
  • 2
  • 4
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

Anjani Mittal
  • 507
  • 1
  • 7
  • 19
  • This API is available in the local environment. How come you can get the result from your end without running the server in the same local environment? – Reaz Murshed Aug 17 '18 at 08:39
  • https://futurestud.io/tutorials/how-to-run-an-android-app-against-a-localhost-api check if this helps – Anjani Mittal Aug 17 '18 at 09:18
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.

trigo
  • 387
  • 1
  • 7
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.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
mahfuz195
  • 116
  • 1
  • 4
  • I think there are some problems with the API call. I suggested him to use Volley for simpler API calling. – Reaz Murshed Aug 17 '18 at 12:42
  • I don't know what was the problem with how I called the API but using volley it worked. I tried using 192.168.0.100 instead of 127.0.0.100 and run Django server on 192.168.0.1. On the mobile browser, it worked but not on the Andriod app. Then I used volley and the problem was solved. – Mazharul Islam Aug 17 '18 at 14:41
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.

MoxJet
  • 11
  • 1
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/30762771) – st.huber Jan 10 '22 at 11:49
-1

In local host You must start the server and also do app development in same system then only it works for you..

-11

There is no way of calling local APIs from android. Leave it.

Faizan Mubasher
  • 4,427
  • 11
  • 45
  • 81
  • Nonsense. Those apis should not be local to begin with. – greenapps Aug 17 '18 at 07:47
  • 6
    This is pure nonsense. You should remove the answer. The API should be tested in the local environment first and of course the local APIs can be called from Android. – Reaz Murshed Aug 17 '18 at 08:45