我正在构建一个使用HTTPUrlConnection()从网页收集一些数据的Android Wear应用程序。这在仿真器中完全正常,但不在真实设备上(我用于测试的Moto 360)。我在本地网络和外部网络上都运行了服务器,但不知怎的,真正的设备最终会出现连接超时。这也不是服务器,因为我尝试了不同的计算机,不同的软件和不同的路由器与不同的网络。 Android清单似乎也很好。我做错了什么,或者这可能是一个错误?Android Wear应用程序连接超时
try {
// create the HttpURLConnection
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
// just want to do an HTTP GET here
connection.setRequestMethod("GET");
// uncomment this if you want to write output to this url
//connection.setDoOutput(true);
// give it 15 seconds to respond
connection.setReadTimeout(15 * 1000);
connection.connect();
// read the output from the server
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
stringBuilder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
bodyHtml = stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
//throw e;
} finally {
// close the reader; this can throw an exception too, so
// wrap it in another try/catch block.
if (reader != null) {
try {
reader.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="vendor.atestapp" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.type.watch" />
<service android:name=".WearMessageListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.DeviceDefault" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
日志输出:
09-09 00:50:50.177 3664-8913/vendor.atestapp W/System.err﹕ java.net.ConnectException: failed to connect to /192.168.1.39 (port 8080): connect failed: ETIMEDOUT (Connection timed out)
09-09 00:50:50.177 3664-8913/vendor.atestapp W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:124)
09-09 00:50:50.177 3664-8913/vendor.atestapp W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
09-09 00:50:50.177 3664-8913/vendor.atestapp W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
09-09 00:50:50.177 3664-8913/vendor.atestapp W/System.err﹕ at java.net.Socket.connect(Socket.java:882)
可能是网络或防火墙问题。您可以试试您的手表是否可以访问诸如“http:// www.google.com”之类的外部地址。或者,也许你的网址有错误,因为'/ 192.168.1.39'似乎很奇怪。 – hgoebl
穿戴防火墙问题?我如何解决这样的问题?如果服务器上有禁用的防火墙,我可以从Wear模拟器完美连接,但不能从真正的Wear设备连接。我的URL根据System.output():'“http://”+ host +“:”+ port +“/index.html”'产生'http://192.168.1.39:8080/index.html '我没有看到该网址的任何问题,这在浏览器中也是完全可以访问的。我尝试删除'http://',但只导致出现异常情况:'找不到协议:192.168.1.39:8080/index.html' – murdoch
手表上不是防火墙,而是您的PC上运行服务器的防火墙。您的服务器也可能不绑定到0.0.0.0,而只是您的一个网络接口。模拟器可以访问它,但也许看不到。 – hgoebl