Java 连接失败:ECONNREFUSED

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18341652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:43:11  来源:igfitidea点击:

connect failed: ECONNREFUSED

javaandroidexceptionservletsconnect

提问by Ján Srni?ek

I am developing one app, and what I think I wanna do is receive data from server database in android. So I started to run some tutorials. I found one that is doing pretty much what I want. But I am getting:

我正在开发一个应用程序,我想我想做的是从 android 中的服务器数据库接收数据。所以我开始运行一些教程。我找到了一个可以做我想做的事情。但我得到:

detailMessage   "failed to connect to localhost/127.0.0.1 (port 8080): connect failed: ECONNREFUSED (Connection refused)" (id=830021648792) 

Whole code of this tutorial is here link

本教程的完整代码在这里链接

Error occurs on :

错误发生在:

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());

I checked that:

我查了一下:

  • firewall is disabled
  • ping is working
  • connection string is exactly the same as when I run the servlet from server admin interface - http://localhost:8080/Requestor/RQSRV-the servlet is running
  • 防火墙被禁用
  • 平正在工作
  • 连接字符串与我从服务器管理界面运行 servlet 时完全相同http://localhost:8080/Requestor/RQSRV- servlet 正在运行

My configuration :

我的配置:

  • Server : Glass Fish 4.0
  • Android App - Developed in Eclipse
  • Servlet - Developed in NetBeans
  • 服务器:玻璃鱼4.0
  • Android 应用程序 - 在 Eclipse 中开发
  • Servlet - 在 NetBeans 中开发

采纳答案by laalto

To access your PC localhost from Android emulator, use 10.0.2.2 instead of 127.0.0.1. localhost or 127.0.0.1 refers to the emulated device itself, not the host the emulator is running on.

要从 Android 模拟器访问您的 PC 本地主机,请使用 10.0.2.2 而不是 127.0.0.1。localhost 或 127.0.0.1 指的是模拟设备本身,而不是运行模拟器的主机。

Reference: https://developer.android.com/studio/run/emulator-networking#networkaddresses

参考:https: //developer.android.com/studio/run/emulator-networking#networkaddresses

For Genymotionuse: 10.0.3.2instead of 10.0.2.2

对于Genymotion使用: 10.0.3.2而不是 10.0.2.2

回答by Rahul pawar

if you are using genymotion then go to command prompt type ipconfig -find for Ethernet adapter virtualBox host-only network: -find for Ipv4 Address..............192.1**.**.* copy

如果您使用的是 genymotion,请在命令提示符下输入 ipconfig -find 用于以太网适配器 virtualBox 仅主机网络:-find 用于 Ipv4 地址......................192.1**.**.*复制

http://192.1..*/foldername/filename/filename.json or filename.php

http://192.1 .*/foldername/filename/filename.json 或 filename.php

to check if its working go to browser in emulator copy paste the url you should find the file which you are looking in your browser..

要检查它是否工作,请转到模拟器中的浏览器复制粘贴网址,您应该在浏览器中找到您正在查找的文件..

Sorry for English.. This worked for me hope this might help some one.. Thank u..

对不起英语..这对我有用希望这可以帮助某人..谢谢你..