Get "SocketException: No route to host" instead of the expected "SocketTimeoutException" - android

I use the DefaultHttpClient to fulfill several downlaods. In the case when the enternet is absent I get SocketTimeoutException only for my first download, and for the next downloads I get java.net.SocketException: No route to host.Why does it occur? I use new httpClient for each request. I need to get SocketTimeoutException for each request, because I should keep the timeout delay between downloads attempts.

Checkout this answers:
Android HttpUrlConnection HttpPost receive a SocketException: no route to host
Android App - Java.Net.SocketException: No route to host
And here some explanation :
Your 192.168.1.114 address is a DHCP address (dynamically) assigned by
your router to your PC. As a result it is visible to the emulator.
But depending on how your WiFi is set up, the Android is probably on a
different router / subnet and can't see 192.168.1.114. You could check
that by typing 192.168.1.114 into the address bar of the Android
browser.
If 192.168.1.114 is not visible to your Android, then you will have to
either change the routing rules on your router to make it visible, or
add a DNS lookup for it.

Related

Flutter "Connection Refused" (errno=111) when trying to send an http get request to from physical device to pc

I built a flutter app which communicates with a web server that I wrote with flask. Everything works as intended without any errors if I use a virtual device. As soon as I try it in release mode on a physical device I get problems when it comes to the communication with the server
The only thing that I changed when using a physical device is the ip. I use 10.0.2.2 on the virtual device and my computers ip4 adress - that I get with ipconfig in windows 10 - on the physical device
Both devices are in the same network connected to the same router
Internet Permission is enabled in the AndroidManifest for all modes (Debug, Main, Profile)
I even disabled the firewall
The line that causes the issue is
await http.get(url).timeout(Duration(seconds: 15), onTimeout: () {
// Handle timeout
// This entire thing is in a try-catch block in an async function
});
In debug mode on the physical device when the HTTP get request is sent VSCode immediately says
Exception has occurred.
SocketException (SocketException: OS Error: Connection refused, errno = 111, address = 192.168.178.20, port = 43378)
First Question: Why Port 43378? Is that the port the HTTP request is sent to? Because when I run the flask app it says:
Running on http://127.0.0.1:5000/
Could that be the issue? I would have expected the exception to say the port is 5000 as declared in the URL. Or do I have to change something with how I set up the flask app? Currently it is the development server because I am still testing before I pay money and deploy
However I hope I didnt forget any important information. Any advice on what could be wrong or how to debug here is highly aprecciated
Pass an Uri object to http.get func. Uri classes let you specify the port as Documentation https://api.dart.dev/stable/2.12.0/dart-core/Uri-class.html

HttpHostConnectException 10.0.2.2:8080 refused

On executing the following statement the app does not respond for a minute and then following logcat message occurs :
HttpPost httpPost = new HttpPost("http://10.0.2.2:8080/webapp/CreateUser");
logcat -
org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:8080 refused
PS - i've tried using "ip address" , 127.0.0.1 AND localhost instead of 10.0.2.2 still the same case.
Also the permissions for INTERNET and ACCESS_NETWORK_STATE are also in the manifest file.
Your server code mostly will be accessible on the local and Wifi network if you have turned on sharing and network discovery on your system.
For the physical device to see this server it has to be on the same network. Also you need to use the IP address of your server.
Okay , the problem has been solved .
As answered by both Codemon and Jayesh , in order to use real device to run the app from Android Studio , you have to use the ip address of the server in place of 10.0.2.2 and make sure your phone device and server are in the same network:-
HttpPost httpPost = new HttpPost("http://192.168.1.53:8080/webapp/CreateUser");
I guess my internet connection was not stable , so ip was changing and thats why it didn't work before.
Thanks for your help guys.

ConnectException vs. IOException when wiFi is disabled?

I tried connecting to an IP address (e.g. http://222.222.222.222:8080) and a URL (e.g. http://www.website.com) while my wiFi is disabled. I noticed that if I don't have a wiFi and I tried connecting to an IP address, it gives me a ConnectException error. On the other hand, if I don't have a wiFi and I tried connecting to a URL, it gives me an IOException error. Why am I receiving different Exception for the 2 cases when the only difference is I supplied an IP address for the first one and a URL for the other? Can someone enlighten me on this one? I am asking this for clarification.
Thank you!
You're getting different errors because different steps are failing.
When you're trying to connect to port 80 of an IP address, it is a connect(2) system call that is failing. There are many different reasons why connect(2) could fail; you'll need to inspect the message from the exception to provide a good error message to the user.
When you're trying to connect to port 80 of a textual address, the libraries will first try to resolve the hostname into an IP address using getaddrinfo(3). The name resolution may or may not fail based on having network access -- if you were trying to connect to localhost, for example, no network access is usually required, as the nameservice lookup can be handled entirely on the local device. Because the nameservice failure happens because you cannot contact a nameserver, it makes sense to give a different error message (and exception), even if the underlying cause is the same for a given set of tests. You might not be able to contact the nameservers for any variety of reasons. Again, you'll need to inspect the message from the exception to give a good error message to the user.

Android: Which Internet connection gets used?

I'm curios if an Android device has both connections configured/available ConnectivityManager.TYPE_MOBILE & ConnectivityManager.TYPE_WIFI how it choose which connection to use for http requests?
Imagine I'm somewhere in WiFi zone without Internet access available (or requires login) but still i could connect to the Internet using GPRS/EDGE etc. (I mean MOBILE).
How Android handles such situations or how to handle it manually?
Making http requests is simple like:
HttpClient httpclient= new DefaultHttpClient();
HttpResponse response = httpclient.execute(mHttpRequest);
but there is nothing about which connection to use or force 2 use...
Check the answer here:
How to use 3G Connection in Android Application instead of Wi-fi?
Basically, you use ConnectivityManager's methods to route the connection. Try it with a test application that requests a GSM connection when WiFi is available and check whether it will do.

Android Unknown Host Exception (DNS Problem) !

i'm trying to connect to the server from my android device in order to get XML inputStream to do that i'm using XmlPullParser
my server Url is:
http://www.biat.com.tn
so, when i'm trying to get the inputstream from this url, I'm getting an unknown host Exception, the screen become black... (although it works for me in localhost)
Then, to check my config I tried to ping the server from the ADB shell, but there is no connection established !!!
I check an other server (http://www.topnet.tn), but i faced the same problem.
PS : I'm getting Streams from these URLs in Navigator
I think that .TN (Tunisia) domain name are not known by Android devices !!!!
i googled this problem and I found a solution, that i should lauch my application in -dns-server mode with putting 8.8.8.8 as dns server... so it works
but pinging my URL server does not !!! please any one can understand this problem !!!
please help...
How are you loading the data? URLConnection, HttpClient?
http://www.biat.com.tn does http 302 redirect to http://www.biat.com.tn/biat/.
In most cases you will need to handle redirect in your code.

Categories

Resources