【问题标题】:Android connection to http server: Eclipse throws exception about localhost even though I state my online server?Android 连接到 http 服务器:即使我声明了我的在线服务器,Eclipse 也会抛出关于 localhost 的异常?
【发布时间】:2013-04-11 13:15:06
【问题描述】:

我正在 Eclipse 中开发一个 android 项目,但我遇到了以下问题。

为什么下面的代码会抛出异常

W/System.err(5354): java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)

即使我的变量 AUTHENTICATION_SERVER_ADDRESS 不是 localhost 而是在线服务器地址,例如 http://myserver.com/folder/

应该连接到我的服务器的代码示例:

url = new URL(AUTHENTICATION_SERVER_ADDRESS);
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);

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

PrintWriter 行中抛出异常。我该如何解决这个问题?谢谢!

【问题讨论】:

    标签: java android mysql url connection


    【解决方案1】:

    Dnt 使用 localhost,使用 Ip 地址。

    例如String URL="http://127.0.0.1(thats your ip):1802(thats your port)/.."

    【讨论】:

    • 这很奇怪,我使用 url = new URL(180.211.94.12:1802/..) 但错误说明了本地主机,这是无法建立连接时的默认错误吗??
    • 你可以试试 127.0.0.1 是给设备(模拟器)自己用的。
    • 你试过你的IP和你的端口吗?不要在您的 AUTHENTICATION_SERVER_ADDRESS 中写入“localhost”
    • 我没有在任何地方使用 localhost,只有错误 Returns "localhost..." I don't give a port number, only: 'String AUTHENTICATION_SERVER_ADDRESS = "h ttp://myserver.com/folder/" ;'然后'url = new URL(AUTHENTICATION_SERVER_ADDRESS);'
    • 它无法访问您的局域网。只需在您的局域网上添加服务器的 IP,您就可以从模拟器访问它。 :)
    猜你喜欢
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 2018-06-06
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多