11

I am trying to connect to a server on the same network as my Android phone via HTTP. My code is as follows:

DefaultHttpClient client = new DefaultHttpClient();
String url = "http://192.168.137.1:80";
url += "/ebs/auth.php?username=" + username + "&password=" + password;

HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
HttpEntity respEntity = response.getEntity();

InputStream is = respEntity.getContent();
String content = GeneralUtility.fromStream(is);

return content;

where the returned content String is supposed to be a JSON String for me t parse through. For one golden moment, I succeeded in accessing the server, but for all other attempts I either ran into TimeoutExceptions (I set a 60second timeout) or a more troublesome error:

org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.137.1 refused

This was caused by:

java.net.ConnectException: failed to connect to /192.168.137.1 (port 80): connect failed: EHOSTUNREACH (No route to host)

And this was in turn caused by:

libcore.io.ErrnoException: connect failed: EHOSTUNREACH (No route to host)

I'm stuck here as I can't even perform basic authentication for my app. What am I doing wrong?

Wakka02
  • 1,491
  • 4
  • 28
  • 44
  • Are you sure the phone isn't changing over from WiFi to the cellular network while testing? – Joachim Isaksson Nov 13 '13 at 19:36
  • (a) Do you have `INTERNET` permissions? (b) Is `192.168.137.1` supposed to be `192.168.1.137`? (c) If the server is on DHCP, has its IP address changed? – 323go Nov 13 '13 at 19:42

2 Answers2

4

Make sure your server is actually serving!

I faced the exact same problem getting the exact same Exceptions when my Android app was sending requests to a computer in the local network. As it turned out, the webapi service was unavailable because the computer (i.e. the "server") was shutdown.

Also, search for a "TIMEDOUT" log. That's another indication that your settings are OK, and it's the server who is not responding.

p.s. I know this answer comes a bit late, but I still think it may benefit others facing the same issue.

kouretinho
  • 2,190
  • 1
  • 23
  • 37
1

In my case, I was testing with a smartphone with proxy setup. I solved the issue disabling the proxy on the phone configuration. If someone stuck in this error, my recommendation is to certify yourself that emulator/physical device network is correctly setup.