I've a php file named test.php stored in my Openshift server (http://phpgear-shifz.rhcloud.com/v1/test.php) with the below code.
<?php
echo "Hello";
Task
I am trying to download the text from an Android application.
Problem
I am getting a java.net.UnknownHostException: Unable to resolve host "phpgear-shifz.rhcloud.com": No address associated with hostname while connecting through a WiFi network, but everything is fine with Mobile Data.
Android Activity Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
final TextView tvTest = (TextView) findViewById(R.id.tvTest);
new AsyncTask<Void, Void, String>() {
#Override
protected String doInBackground(Void... params) {
try {
final URL url = new URL("http://phpgear-shifz.rhcloud.com/v1/test.php");
final BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
final StringBuilder sb = new StringBuilder();
String line;
while((line = br.readLine())!=null){
sb.append(line).append("\n");
}
br.close();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return "Error: " + e.getMessage();
}
}
#Override
protected void onPostExecute(String result) {
tvTest.setText(result);
}
}.execute();
}
RESPONSES
on WiFi
on Mobile Data
Question
1) Why I can't connect through the WiFi network where Mobile Data is perfectly fine ?
2) How to solve this problem ?
NOTE: Sometime it's getting connected, sometime won't.
Your DNS doesn't know the IP address of the requested site.
You are experiencing problems, because the DNS of your Wifi connection cannot convert a hostname to an IP address.
And your data carrier is using different DNS which has associated IP address to hostname.
Try to change your DNS server address on your Wifi router or use direct IP address of the website if available.
Here are some google DNS server addresses
8.8.8.8
8.8.4.4
You may have an IPv4 vs IPv6 problem. Many mobile data plans use IPv6, while most WiFi installations currently use IPv4, so you may be switching more than just the network layer; you may actually be switching layer 3 protocols.
The DNS entry for phpgear-shifz.rhcloud.com points to an IPv4 address (only), so it should work on WiFi. But maybe your mobile device uses an IPv6 DNS server and can't resolve the name via IPv4?
Another possibility: your mobile device may have a more general problem in the IPv4 stack. Your mobile data may be using one of the 6-to-4 transition technologies and thus bypass your local IPv4 problem.
I noticed another problem with the DNS name phpgear-shifz.rhcloud.com although I doubt it is related.
That DNS entry is actually a CNAME entry that points to another CNAME entry, which in turn points to an A record on Amazon. Double indirections of CNAMEs are a violation of the DNS RFCs, although most resolver should handle it anyway. Also, if this was the problem, it should affect both WiFi and mobile data equally.
Related
I have a deployed app that is failing on Android 9. Part of its function is to configure a module over an Access Point network to allow that that module to connect to the users home network.
I have code that detects and connects to the correct WIFI network, but when I attempt to open a socket to the device, it fails - only on Android 9 and only if mobile data is enabled. If I manually disable mobile data on the device everything runs fine.
Socket open() {
Socket sock = new Socket(Proxy.NO_PROXY);
try {
sock.bind(new InetSocketAddress(localIpAddress(), 50000));
} catch (IOException e) {
activity.logContent("Warning: Failed to bind socket : " + e.toString());
}
try {
sock.connect(new InetSocketAddress("192.168.17.1", 5555), (int)5000);
} catch (IOException e) {
// This catch fires when Mobile Data is on.
activity.logContent("Connected to " + activity.mWifiManager.getConnectionInfo().getSSID());
activity.logContent("Couldn't open socket : " + e.toString());
}
return sock;
}
I have tried this with and without the Proxy.NO_PROXY and with and without the bind() call. If the bind call is missing the error implies that the socket is attempting to connect over the cell network. (Note: activity.logContent() is an on-screen log so it is easier to see what is happening when not connected to a debugger).
Any ideas what is going wrong?
After a few days of imprecations I believe I have come to the identification of the problem and therefore to the solution:
The problem occurs due to some changes in the version of android (I presume to be 9.0 even if other changes had occurred on API 21), in particular on the creation of the socket, if the system detects that there is a "better" network (access to internet, high signal, etc, etc) socket creation refers to that network and no longer to the wifi network you would like.
I looked for ways to force the creation of the socket on the wifi network (which is the network I want) and the only way I found is this:
Simply put instead of:
Socket sock = new Socket ();
Do:
ConnectivityManager connectivity = (ConnectivityManager) MyApp.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
for (Network network : connectivity.getAllNetworks())
{
NetworkInfo networkInfo = connectivity.getNetworkInfo(network);
if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI)
{
if (networkInfo.isConnected())
{
Socket sock = network.getSocketFactory().createSocket();
}
}
}
}
Practically browse the networks present in the device and when you find your active wifi you do nothing but take advantage of this function to get the right socket for sure:
getSocketFactory().createSocket()
Now you have the working socket!
In my case it now works perfectly, if someone finds better solutions, it is welcome, but for now it is the only way I have found to make everything work as in the previous version of android.
In Android 9 there a security config about network: Android security config
Adding your domain in network_security_config might solve your problem. I had this in my network_security_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">119.xxx.xxx.xxx</domain>
</domain-config>
</network-security-config>
I am not sure about the exact reason of why this is happening. However, when you are turning on your mobile data and you are only connected to the internet using your mobile data (considering your wifi is turned off), it gets the IP address from the cellular network which is no more connected in your home network. Hence, this is trivial to expect such timeout scenarios, because, it cannot reach the private IP addresses of your home network starting with 192.168.....
Now my confusion is that even if the mobile data is turned on, and both wifi and mobile data is turned on at the same time, the device should connect to the wifi as a default behavior.
Hence I would like to suggest you check the following.
Android 9 (Pie) introduces special Wifi preference, which prevents connecting to public networks automatically. You might consider checking the settings.
Please check the IP address of your device and check if it has some IP address starting with 192.168..... If not, then definitely, you are getting your IP address from your cellular network and hence it cannot reach your private IP addresses of the home network.
hi have at home a rasperry pi running a server java app, connected to de router with the dynamic DNS configured and the in/out communication ports openned.
When i run the android apication client througt 4g everithing is working sucessfull. But when i run the same app connected to the wifi on my local net, where the server are running, the server application looks like death.
Any ideas?
Thanks.
router config
I think the problem is related to your DNS. If you are connected using your wifi, you have to return a local IP-Address. Do you have any chance of configuring your router to return the raspberries local IP-Address for wifi-clients?
Take a look here: link
A simple solution, even if it is not very elegant, is adding the following conditions to your code:
If there is an error connecting to the Dyn DNS, try to connect to the local IP address. (In case you are in the Wifi LAN)
If the local IP address fails, try again your Dyn DNS (in case the user is a real user with real communication problems)
(repeat until the connection is successful)
You can also identify your testing devices (using Settings.Secure.ANDROID_ID, or the IMEI) and use the local IP only for them. Another option is making the URL configurable (with a hidden option for example).
Because of my app can run local, only needs to connect to server for updates. I have block the connection on the server app if the ip from client and server are equals.
At the moment is the best solution to keep the server app running properly.
URL whatismyip = new URL("http://checkip.amazonaws.com");
BufferedReader in = new BufferedReader(new
InputStreamReader(whatismyip.openStream()));
String ip = in.readLine();
try {
SSLServerSocketFactory sslFactory = (SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
SSLServerSocket ss = (SSLServerSocket) sslFactory.createServerSocket(PORT);
int idSession = 0;
while (true) {
SSLSocket socket = (SSLSocket)ss.accept();
if(socket.getInetAddress().getHostAddress().equals(ip)){
if (socket != null && !socket.isClosed()) {
socket.close();
}
}
((ServidorThread) new ServidorThread(socket, idSession)).start();
idSession++;
}
} catch (IOException ex) {
ex.printStackTrace(System.out);
Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
}
}
I'm trying to connect my android application to a local host url thanks to wamp server but it doesn't work. My goal here, is to fetch json data and parse these data. For my test, i'm using a device not the emulator and i use permission in AndroidManifest.xml :
<uses-permission android:name="android.permission.INTERNET" />
My url looks like this :
String url = "http://10.0.2.2:8080/tests/PhpProject1/connectionBDD.php";
i tried :
http://localhost/
http://10.0.2.2:8080/
http://10.0.2.2/
But it never worked so far :
java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
failed to connect to /10.0.2.2 (port 8080): connect failed: ETIMEDOUT (Connection timed out)
java.net.ConnectException: failed to connect to /10.0.2.2 (port 80): connect failed: ETIMEDOUT (Connection timed out)
Then i tried with a json url test found on the internet : http://headers.jsontest.com/
It worked really good and i got json data at this address. So i guess my code is good and the issue here is my localhost url, i don't know what should be its exact form..
I read many threads about it but i didn't find a solution.
Here my code :
Main activity :
public class MainActivity extends Activity {
private String url = "http://10.0.2.2:8080/tests/PhpProject1/connectionBDD.php";
private ListView lv = null;
private Button bGetData;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final JsonDownloaderTask task = new JsonDownloaderTask(this);
lv = (ListView) findViewById(R.id.list);
bGetData = (Button)findViewById(R.id.getdata);
bGetData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
task.execute(url);
}
});
}
public void jsonTaskComplete(JSONArray data){
//todo
}
}
AsyncTask :
public class JsonDownloaderTask extends AsyncTask<String, String, JSONArray> {
MainActivity ma;
public JsonDownloaderTask(MainActivity main){
ma = main;
}
#Override
protected JSONArray doInBackground(String... url) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONArray jsonArray = null;
try {
jsonArray = jParser.getJSONFromUrl(url[0]);
} catch (IOException e) {
e.printStackTrace();
}
return jsonArray;
}
protected void onPostExecute(JSONArray data){
ma.jsonTaskComplete(data);
}
}
JSONParser :
public class JSONParser {
String data = "";
JSONArray jsonArray = null;
InputStream is = null;
public JSONParser(){}
// Method to download json data from url
public JSONArray getJSONFromUrl(String strUrl) throws IOException{
try{
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
is = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuffer sb = new StringBuffer();
String line = "";
while( ( line = br.readLine()) != null){
sb.append(line);
}
is.close();
data = sb.toString();
//br.close();
jsonArray = new JSONArray(data);
}catch(Exception e){
Log.d("Exception while downloading url", e.toString());
}finally{
is.close();
}
return jsonArray;
}
}
IP-address 10.0.2.2 is used to fetch data from the emulator.
Localhost will always point to the emulator/android device running the application.
To let your device fetch data from your pc, it should be in the same network (connected by WiFi to your router) and you should use the local IP-address of your pc (normally a 192.168.1.x-number).
If you try to connect to "localhost", it will resolve to the Android device, not to your own localhost (unless you are running within the emulator). What I recommend for development is to add an overflow menu in the action bar that has an entry named "Settings" that provides a Settings activity for specifying application settings, and to have a "Developer options" entry in "Settings" that lets you specify a custom server address to use. During development, you can use this option to enter a custom server address for your app. (You will need a real server address that is actually reachable over the Internet rather than using localhost for this).
First you have to bind the IP address of the machine where your server is running in the eclipse settings.
You can do this like this.
Right click on the PHP project in the eclipse then Run Configuration then In the Web Application where you will find the Argument tab. Now here give the port and LAN IP address of your machine on which your server is running.
Something like this --port=8888 --address=192.168.1.6 then update the URL to http://192.168.1.6:8080/tests/PhpProject1/connectionBDD.php
Here in my case this is my LAN IP address 192.168.1.6, there you will have to find it using the network command like ipconfig , ifconfig and use that IP address.
if you are using your phone instead of emulator and running services on localhost then in url instead of '10.0.2.2' use IP address of your PC.
I solved it by:
1. Adding another android permission in the manifest: "android.permission.ACCESS_NETWORK_STATE"
2. As I'm using xampp, I've shared the xampp folder of the desktop in the network.
3. The xampp is running in a desktop whose ip is 192.168.x.x so the webservice's url instead of beign "http://localhost/myapi..." is "http://192.168.x.x/myapi..."
I tested the app using the emulator and also in a device. Both cases works out.
One simple way i know is keep mobile data on and share wifi . Connect your laptop or computer to this wifi . Now see ip of ur laptop or desktop. Call service from ur phone . Since your phone and your computer are in same network now.
I assume you are trying to access web service available on your PC from either an android simulator or a real device.
For an android emulator, you must NOT just use "localhost", because "localhost" means android emulator itself, NOT the host PC.
you need modify the /etc/hosts file or the simulator or real device. add a line like "192.168.0.100 service.local".
I tried "10.0.2.2:80/mysitename/page.php"
Miracle happened, it's working now.
I am on Mac and using XAMPP for server.
You can change port no. to 80 and try.
port 8080 was not working for me!
Cheers.
Just Install the "conveyor by Keyoti" the extension in Visual studio and it will generate a url according to your ip address automatically. here's the link:
conveyor
so far so good....!
I have a Android application which consumes a webservice on a local network. There's a config screen where the user inform the server IP address, which is running Apache Tomcat.
I'm looking for a way to auto-detect the server based on the current connected wi-fi network.
i.e: The smartphone's IP is 10.1.1.90 and the server IP is 10.1.1.254.
Is there a way to achieve this? I'm thinking on using ping, but I don't know if is a good ideia.
The way I understand it, you need to discover IP of your tomcat server and connect it using your client.
I am assuming , both the server and client is in your control.
One simple way can be to use jGroups Cluster.
You can make your tomcat discoverable
Client can discover it using the name of the cluster you have provided .Refer the JChannel API that Jgroups uses
I simulated it making following server class
public class TomcatServer {
JChannel channel;
private void start() throws Exception {
channel = new JChannel(); // use the default config, udp.xml
channel.connect("TomcatCluster");
}
public static void main(String[] args) throws Exception {
new TomcatServer().start();
}
}
The simulated client class
public class MobileApp extends ReceiverAdapter {
JChannel channel;
private void start() throws Exception {
channel = new JChannel(); // use the default config, udp.xml
channel.setReceiver(this);
channel.connect("TomcatCluster");
channel.close();
}
public static void main(String args[]) throws Exception {
new MobileApp().start();
}
The client will provide you following information
GMS: address=MACHINENAME-47879, cluster=TomcatCluster, physical address=xxxxx:0:xxx:xxxx:xxxx:xxxx:xxx:xxxx:xxxx
** view: [MACHINENAME-31239|1] [MACHINENAME-31239, MACHINENAME-47879]
Where MACHINENAME-47879 is the client machine and port & MACHINENAME-31239 is the tomcat server name and port
Do you want to detect "a tomcat server" or "your tomcat server" ?
I mean, do you have any way to custom your server ? If it's the case, then you could create a very simple test page on your server (say a "Hello" JSP page), which your Android application could look for.
If your Android gets a "Hello" result with a GET request on http://<tomcat_ip>/hello.jsp, then you may assume that the tomcat is online.
If you can't add this test page, then you can test any page which the server is supposed to serve. (even a 404 page which sometimes is not configured well, and shows the tomcat version...)
Tomcat response headers can contain the xpoweredBy field that would advertise Tomcat if enabled. However it is most often disabled due security considerations, and even disabled by default. You however could re-enable it if you need to auto-detect exactly your Tomcat servers. From the other side, indeed, if you can place a web page on your server, you can simply place a marking page with the agreed signature.
If the server IP is unknown, I would propose the following ways to detect the server on the network:
The most straightforward way is to do the breadcast ping (ping -b broadcast_address where breadcast address can be computed here, for instance). All network devices that are configured so would reply, then verify as explained above which one is the server. However pinging broadcast address requires a rooted phone. Also the router may not support.
Your DHCP service (most likely your router) can often be configured to issue always the same IP address for the same MAC address of your server network card.
If the server is a desktop computer or laptop, it could show its address as QR code on display. It is possible for a smartphone to scan the code from the screen, and this is way easier than to enter IP address through the touchscreen. QR code can also include auto-generated password for extra security.
If there is wireless router with the possible login where both server and client are connected, the internal pages of that router often contain the relevant IP addresses. You would need to implement logging into the router and doing some screen scrapping.
I made an Android app which used a local server in the WLAN. I made the terminal (the phone) broadcast it's own IP address, which the server then picked up.
I used MultiCast class on the phone, which added the ip-address of itself to the payload. The server always has a thread in multicast read class that obains the payload of the packet (which is the terminals ip-address). Set the terminal in datagram read state and send the servers ip-address to terminal.
Maybe are better ways, but a great way to get the ip-addresses of unknown terminals in the network.
The way i had resolved this problem is with the use of enumerations.
public String getLocalIpAddress()
{
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (Exception ex) {
}
return null;
}
}
I've been asked to activate a certain piece of code if i was in my college. So I need to find the iP of where i am to match to my colleges iP. Was wonderng how to do this in java? I have already tried a loop back interface.
By using NetworkInterface.getNetworkInterfaces() and calling getInetAddresses() on each interface, you can see all IP addresses assigned to your computer. To check if you have an IP in your university's range, you could do something like this:
boolean onCampusNetwork() {
for(Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) {
NetworkInterface iface = ifaces.nextElement();
for(Enumeration<InetAddress> addresses = iface.getInetAddresses(); addresses.hasMoreElements;) {
InetAddress address = addresses.nextElement();
// return true if address is in the university's range; something like:
if(address.toString().startsWith("10.0")) {
return true;
}
}
}
// None of the IP addresses were in the university's range.
return false;
}
I haven't run this code, but it should do what you need.
Isn't there supposed to be some kind of protocol for automatic proxy discovery and configuration? Does your college have this already setup? Then it would be better if your code discovered the correct settings and had an option to override the settings.
There are all kinds of websites out there that will give you your public ip (or the public IP of your gateway, which I assume is what you want). You could tell the program to make an HTTP connection to one of those sites and get the page with the info on it. Since these sites have a very predictable format, the result would be very easy to parse with a regex or two. This only works if you have an internet connection though.
Alternatively, you could have the program try to connect to one of your college's intranet servers. If it can make the connection to a site that is not accessible to the outside world, it's on the LAN.