Android HTTP上的EHOSTUNREACH(无主机路由)

Wak*_*a02 11 android http

我试图通过HTTP连接到与我的Android手机在同一网络上的服务器.我的代码如下:

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;
Run Code Online (Sandbox Code Playgroud)

返回的内容String应该是一个JSON字符串供我解析.在一个黄金时刻,我成功访问了服务器,但是对于所有其他尝试,我遇到了TimeoutExceptions(我设置了60秒的超时)或更麻烦的错误:

org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.137.1 refused
Run Code Online (Sandbox Code Playgroud)

这是由于:

java.net.ConnectException: failed to connect to /192.168.137.1 (port 80): connect failed: EHOSTUNREACH (No route to host)
Run Code Online (Sandbox Code Playgroud)

而这又是由以下原因引起的:

libcore.io.ErrnoException: connect failed: EHOSTUNREACH (No route to host)
Run Code Online (Sandbox Code Playgroud)

我被困在这里,因为我甚至无法为我的应用程序执行基本身份验证.我究竟做错了什么?

kou*_*nho 4

确保您的服务器确实在服务!

当我的 Android 应用程序向本地网络中的计算机发送请求时,我遇到了完全相同的问题,得到了完全相同的异常。事实证明,由于计算机(即“服务器”)关闭,webapi服务不可用。

另外,搜索“TIMEDOUT”日志。这再次表明您的设置没有问题,只是服务器没有响应。

ps我知道这个答案来得有点晚了,但我仍然认为它可能有益于面临同样问题的其他人。