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.
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 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/
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.
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
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
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.
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.
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.
In local host You must start the server and also do app development in same system then only it works for you..
There is no way of calling local APIs from android. Leave it.
I have ogar on my main pc(windows) and I can't connect at that server on laptop. They are both on same wifi, but diiftent os(Xp and 10). I cannot install hamachi or any program like that.
I tried to do this:
agar.io/?ip=127.0.0.1:913(913 its valid port and i connected like this on my pc)
and then I changed it to 192.168.0.1 and it still didn't work.
You should post this question on a gaming forum. This forum is for programming related questions only.
By the way, if you want to connect your laptop and your "main pc" to the same server, follow this steps:
From your "main pc" go to agar.io
Change the gamemode from FFA to party
Now click on Create
A link will be displayed, go to that link from your laptop
Now your "main pc" and your laptop will be connected to the same match (the same server).
I have a server and 5 PCs connected to it. I want to archive implement the following,
Whenever any of the PCs tries to access internet(tries to browse Google o u-tube o what so ever), I want to redirect the PC to a login page. Login page will contain username and password. Unless and until they enter the username and password correctly, they wont be allowed to browse internet.
How to block the PCs??
I am a newb in network programming. I have no idea how to archive this.
Is this possible to do this in java o any other platform??
If so then please guide me...
Thanks in advance!!
i guess you are seeking for something like they have it on public wifi (hotels,library etc) Bluesocket and chillispot.if you want to program the same guess you can have a look at chillspot sourcecode
i think then you should do it on your Router itself.
DD-WRT
on windows You can setup VPN.
How to Set Up a Virtual Private Network with Windows
How to Create a VPN Server on Your Windows Computer Without Installing Any Software
How to setup a VPN Server in Windows Server 2008
I recently developed an Android application with which the Android device can communicate with another Android device running the application.
The communication works over sockets, therefore I developed a server which i run on my computer.
Here is my problem:
The communication between the devices over the Server running on my PC works fine, as long as all devices as well as the PC are in the same LAN (connected over the same Router for example).
Now I want to get the server online, so that the Android devices can connect to the "online" server and communicate with each other over the server from anywhere.
I simply have no idea of how to get the server online and running. How can I do that?
The main issue is, that I know about Client/Server communication locally, but have no experience in the "online" sector.
It is more a network problem than a programming one. Your server open a socket and therefore is available to anyone able to reach that socket.
You have to do a redirection on your router. The problem is that your machine doesn't have a public IP, only your router has one. So when your router receive a packet on port 21 for example, it doesn't know what to do with it. You have to configure it to say "the port 21 has to redirected to the local IP XXX"
Also the public IP of your modem/router can change, depending on your ISP. If your have a fixed IP, it won't change, otherwise you will have to install a software like dyndns to have a domain name associated with your IP.
So I wrote this jar that interacts with a database and also serves as a server. Problem is that I can only interact with that jar when I'm on the lan or run the jar from a different computer. The system with the problem is running windows xp with the firewall disabled and yes it is correctly set up with the router as I can launch a web server and view it remotely. I have no clue what the problem would be as the firewall is disabled and the jar works on other computers on the same network.
Note: I was noticing an exception "java.net.SocketException: Address family not supported by protocol family" earlier
1.Open Network Connections
2.Right-click any local area connection, and then click Properties.
3.Click Install.
4.In the Select Network Component Type dialog box, click Protocol, and then click Add.
5.In the Select Network Protocol dialog box, click Microsoft TCP/IP version 6, and then click OK
6.Click Close to save changes to your network connection.
Lastly Restart your application
See this: http://www.martinahrer.at/2009/04/16/javanetsocketexception-address-family-not-supported-by-protocol-family-bind/
Generally the problem is related to IPv6 support. Try to google your exception text and I am sure you will find an answer quickly.