简体   繁体   English

与本地主机连接时,android设备上的httpconnection被拒绝

[英]httpconnection refused on android device when connecting with localhost

I am making an app which requires connection with my localhost on PC. 我正在制作一个需要与PC上的localhost连接的应用程序。 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. 其中8090是运行我的Apache Tomcat的端口,而192.168.xx.xx是我的PC的IP地址。 I have tried out many things but none worked. 我已经尝试了很多东西,但是都没有用。 Also I'm using USB connection not wifi to access localhost from device. 另外我正在使用USB连接而不是wifi从设备访问本地主机。

Please help!!!! 请帮忙!!!! thanks 谢谢

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? 您是否看过由Stackoverflow链接的类似问题,例如无法从Android设备访问本地主机如何 从Android设备 访问本地主机? ?

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.). 尝试使用手机的浏览器导航到http://192.168.XX.XX:8090 ,如果成功,则可能是您的应用存在问题(代码,权限等)。 If that fails you have more subquestions, which are general network troubleshooting questions: - can the phone indeed reach the IP of your PC? 如果失败,您还有更多子问题,这是一般的网络故障排除问题:-电话确实可以到达您PC的IP吗? If yes: - does the webserver listen for outside connections? 如果是:-Web服务器是否侦听外部连接? If yes: - what is blocking the connection between phone and webserver? 如果是:-阻止电话和Web服务器之间连接的原因是什么? 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). (通过USB连接,这就是两个主机本身)。

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 ). 如果你不这样做,你要学会解决好,你可能会考虑进行故障排除的科学方法(谷歌“疑难解答科学方法”,有几篇文章看起来有帮助,像这样 )。

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'. 当我仔细检查logcat时,发现某些权限“未被识别”。 So, check your manifest permissions, or re-write them, it will solve your problem. 因此,检查您的清单权限,或重新编写它们,将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM