Cannot connect to localhost API from Android app - java

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.

Related

which IP should I use when sending a file from windows to vm using java

I am trying to send a file from Android emulator on Windows to ubuntu.
https://www.rgagnon.com/javadetails/java-0542.html
https://www.youtube.com/watch?v=7QNJvxXCYOY
How do I use the Simple HTTP client in Android?
These all helped me learn how to send a file through socket, but I am not sure which IP address to use. I set up ServerSocket and Socket, but the code won't proceed at socket = ServerSocket.accept()
I wonder if this is because I am not using the correct IP address. I appreciate any help. Thanks in advance.
I am trying to send a file from Android emulator on Windows to ubuntu.
So you're running an emulator on Windows and want to send a file from the emulator to another machine running Ubuntu? Then there is no way for us to answer this question exactly, as IP addresses depend on your personal network setup. I would suggest first moving the file form emulator -> Windows, then you can send it like any other file between your machines (you can use scp from Ubuntu, or a service like DropBox, etc.)
You can also probably run ifconfig on your Ubuntu side to obtain your destination IP.

Add Java API to online server not localhost

I have just created a Hello World RESTful Java API with IntelliJ IDEA and Spring and I make several requests with http://localhost:8080/function and it works fine and return the JSON data well, but now I want to make request from my android app to get the same JSON data from another network. How to create global API or make my local API global or online?
If it is working on your PC, your next step is to host it somewhere which is reachable from the outside world. If it is still in testing mode, probably what you need to do is port-forwarding from your router, so that your requests to your public IP get forwarded to your PC.
In the long term of course you need to host it somewhere, unless you want to leave your PC running. You can buy a cheap hosting to start with on something like Digital Ocean, or go for the more advanced cloud service providers like Google and AWS.
in your router you have to redirect port 8080 to you network IP and allow port 8080 in your firewall
And use this IP address https://whatismyip.com.br/
You can create your java application in a container and deploy it on one of the cloud providers (AWS, Azure, Google App Engine).

Android: Open/read or copy file in local network

completely newby in Android, coming from c# world..
I've written a little private app for android and that must not be available on play store and the like...
the app is using JTDS to directly connect to local SQL Server to manage "inputs" from the device, and it works as expected :)
again, the app can not be updated via play store, but now I'd like to provide a feature to update it [occasionally it will :)]
so I'd like to verify on a local network share a text file containing the latest build available and, eventually, copying the "new" apk to android \Download directory...
but I still have to find a way to "read\connect" the the LAN's share...
I've seen other threads like
How to use httpget in my app
and basically my code follows How to get file via HttpGet from pc in local network on android
but the code "fails" with 404 status code...
in the HttpGet(file) I've provided the LAN location of the file I need to read, like
String file = "http://192.168.x.xxx/NetworkShare/file2read.txt";
the share is on a Windows7 machine with readonly permissions and no authentication required... It's not a Windows Domain, it's just a WorkGroup so permissions troubles should be excluded...
I'm still checking with the emulator and not the physical devices...
the manifest already includes Internet permissions, WIFI_STATE, NETWORK_STATE
If helps, I'm using Android Studio 1.1.0
any hint or alternative?
TIA
asql
What you try to do with HttpGet won't work with a Lan location because when you share a directory you are using SMB,CIFS or NFS protocol and it's impossible to access those files via HTTP. If you want to use HttpGet you must publish your files through a HTTP server.
I think there is no way to mount a drive programmatically in Android. It is possible with busybox but maybe you need to be root.
The best way to do what you want is getting the information from a webservice or maybe downloading your file from a ftp server.

How to connect android device to localhost server?

I am trying to establish a connection between my client app running on my android mobile and a server running on my windows 7 PC.
I am new to android and so while looking for some client server tutorial, got the below 2 links.
http://www.compiletimeerror.com/2013/09/creating-java-web-service-using-axis-2.html
http://www.compiletimeerror.com/2013/09/accessing-web-service-from-android.html
Here, server is developed on java and published using axis2 webservice. I followed the tutorial and could develop the similar client and server which runs fine on the emulator. But if I try to access the same using my android mobile it is not working. My mobile is unable to speak to the localhost on my PC.
What I am doing here is,
1. Connected both my PC and mobile to same WIFI network.
2. Connected the mobile to PC using USB.
3. Started the Server on Tomcat
4. Running the Client Project from eclipse and selecting my mobile as target.
Below is my published webservice URL which I am calling from client code,
http://192.168.0.3:8080/MyWebService/services/WebService?wsdl
Here 192.168.0.3 is my PC IP
Please let me know what am I doing wrong here?
Windows 7 might block port 8080. You could test that your port is open from another pc using
telnet 192.168.0.3 8080
or from android using this code.
If 8080 is blocked, then you may want to open it using these steps:
Open Windows Firewall by clicking the Start button Picture of the Start button, and then clicking Control Panel. In the search box, type firewall, and then click Windows Firewall.
In the left pane, click Advanced settings. Administrator permission required If you're prompted for an administrator password or confirmation, type the password or provide confirmation.
In the Windows Firewall with Advanced Security dialog box, in the left pane, click Inbound Rules, and then, in the right pane, click New Rule.
Follow the instructions in the New Inbound Rule wizard.
Note: You must be sure beforehand that your android device is connected to the same local area network or wifi.
Try to access your URL
http://192.168.0.3:8080/MyWebService/services/WebService?wsdl
from your android browser. If you are getting an xml page, then it's not the network problem. If it's network problem, try restarting your local server and changing firewall and router configurations.
You have to provide more information to get a specific answer.
If everything is fine then u should check your Android App.
U have to add the following permission for Internet access.
<uses-permission android:name="android.permission.INTERNET"/>

DNS Error On BlackBerry Simulator and Phone

Hey all, having a little trouble here with connecting my application on BB. I'm using ksoap2 to call a java webservice i made running on GlassFish 3. On the simulator when i call this url
"http://equilibrio-120:8088/SocialVoiceServer/SocialServerWSService"
everything works well and it connects, the web service is currently running on my own computer. But if i append ";deviceside=true" to the url it will fail giving me a DNS Error. Anyone know why this happens?
"deviceside=true" tells the connection to use DirectTCP which goes over the cell phone carrier's network. Since the service is running on your local computer the blackberry will need to be connected to that network so append the URL with ";interface=wifi" to use your wi-fi network. Alternatively, you could host the web service somewhare that is accessible to the internet.

Categories

Resources