clueless
clueless

Reputation: 11

httpconnection refused on android device when connecting with localhost

I am making an app which requires connection with my localhost on PC. It is working fine with emulator but when trying it out in my real device it is throwing exception:

org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.XX.XX:8090 refused, network host unreachable etc etc

where 8090 is the port in which my Apache Tomcat is running and 192.168.xx.xx is my pc's ip address. I have tried out many things but none worked. Also I'm using USB connection not wifi to access localhost from device.

Please help!!!! thanks

Upvotes: 1

Views: 2180

Answers (2)

Darpan
Darpan

Reputation: 5795

It is probably your manifest file is corrupted. Even I was getting same error; while I checked my logcat closely, I found that some permissions were 'not recognized'. So, check your manifest permissions, or re-write them, it will solve your problem.

Upvotes: 0

Blaisorblade
Blaisorblade

Reputation: 6488

Have you looked at similar questions linked by Stackoverflow, such as Cannot access localhost from Android device or How can I access my localhost from my Android device??

This question does not have a single answer, there can be many different problems. You do not list what you tried to exclude. Also, can you paste the actual error without removing technical details? You can omit the stack trace. Can you add the line doing the connection?

As a first step, since the message says "network host unreachable" you should check that your phone can access your computer at all. Try navigating to http://192.168.XX.XX:8090 with your phone's browser, if that succeeds probably there's a problem in your app (the code, permissions, etc.). If that fails you have more subquestions, which are general network troubleshooting questions: - can the phone indeed reach the IP of your PC? If yes: - does the webserver listen for outside connections? If yes: - what is blocking the connection between phone and webserver? Is there any firewall involved? For this one, understanding how networking works "inside", that is, which nodes are supposed to relay the packets to the other side, will tell you which nodes might not be doing their job. (With a USB connection, that's the two hosts themselves).

Maybe you did some of the experiments and excluded some of these hypotheses. If so, it would be helpful to describe those experiments. If you are not doing that, and you want to learn to troubleshoot better, you might look into the scientific method for troubleshooting (Google "troubleshooting scientific method", there are several essays which look helpful, like this).

Upvotes: 1

Related Questions