2015-09-08 110 views
0

我正在构建一个使用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) 
+0

可能是网络或防火墙问题。您可以试试您的手表是否可以访问诸如“http:// www.google.com”之类的外部地址。或者,也许你的网址有错误,因为'/ 192.168.1.39'似乎很奇怪。 – hgoebl

+0

穿戴防火墙问题?我如何解决这样的问题?如果服务器上有禁用的防火墙,我可以从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

+0

手表上不是防火墙,而是您的PC上运行服务器的防火墙。您的服务器也可能不绑定到0.0.0.0,而只是您的一个网络接口。模拟器可以访问它,但也许看不到。 – hgoebl

回答

1

发现问题! HTTPUrlConnection在Wear上不起作用。谷歌禁用它,但你可以完美编码它。显然这是Google的某种政策,但他们忘了在模拟器中禁用它。

替代方案(这是我现在的做法):在手机上使用HTTPUrlConnection,并使用MessageApi与Wear设备进行通信。

1

Android手表不会通过蓝牙连接到手机连接到互联网,UrlHTTPConnection被禁用,因为它不是Wear问题,因为我会但是它会在通过WiFi连接时连接。

不幸的是,大多数设备前2015年中期不支持WiFi :-(更不幸的是,Android Wear应用的手持设备将不会与来自世界互联网从表桥蓝牙。

UrlHTTPConnection被启用,因为它不是一个磨损问题,有罪的是耐磨应用在手持设备上。

很显然,这是不可行的“为人生”但出于好奇,如果你连接你的设备通过WiFi和禁用蓝牙在您的手机上(它总是偏爱电源考虑BT)它会奇迹般地开始工作。

相关问题