Android Wear 2 WiFi本地网络访问

时间:2017-10-11 19:42:20

标签: android retrofit2 android-wear-2.0

我已经通过改造进行了一些REST调用,访问本地WiFi网络上的本地IP地址,在移动端工作得很好,但是一旦在独立的应用程序上,我就会出现这个错误:

java.net.SocketTimeoutException: connect timed out
10-08 14:50:00.154 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.net.PlainSocketImpl.socketConnect(Native Method)
10-08 14:50:00.154 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
10-08 14:50:00.154 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
10-08 14:50:00.154 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
10-08 14:50:00.154 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.net.Socket.connect(Socket.java:605)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:63)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:223)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:149)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:195)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:211)
10-08 14:50:00.155 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at com.mylisabox.network.interceptors.HostSelectionInterceptor.intercept(HostSelectionInterceptor.kt:33)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at com.mylisabox.network.interceptors.TokenInterceptor.intercept(TokenInterceptor.kt:25)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at okhttp3.RealCall.execute(RealCall.java:69)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at retrofit2.adapter.rxjava2.CallExecuteObservable.subscribeActual(CallExecuteObservable.java:41)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.Observable.subscribe(Observable.java:10903)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at retrofit2.adapter.rxjava2.BodyObservable.subscribeActual(BodyObservable.java:34)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.Observable.subscribe(Observable.java:10903)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.internal.operators.observable.ObservableSingleSingle.subscribeActual(ObservableSingleSingle.java:35)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.Single.subscribe(Single.java:2700)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at hu.akarnokd.rxjava2.debug.SingleOnAssembly.subscribeActual(SingleOnAssembly.java:41)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.Single.subscribe(Single.java:2700)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:452)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:61)
10-08 14:50:00.156 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:52)
10-08 14:50:00.157 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-08 14:50:00.157 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
10-08 14:50:00.157 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
10-08 14:50:00.157 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
10-08 14:50:00.157 1804-1804/com.mylisabox.lisa E/HomeViewModel$onRefresh:     at java.lang.Thread.run(Thread.java:761)

如果我用在线IP地址替换本地IP地址,它也可以在手表上正常工作。在移动应用程序上没问题,我可以正确访问本地或远程相同的URL。两个设备都在相同的WiFi网络下。 有什么想法吗?

3 个答案:

答案 0 :(得分:2)

这个问题一直困扰着我。似乎是这样的情况是,当Android Wear通过蓝牙(以及手机)连接到互联网时,http / web呼叫以某种方式被代理。

解决方案是禁用手机的蓝牙连接,这会强制Android Wear设备直接连接到本地wifi(如果它首先支持wifi),然后本地API请求会神奇地开始工作。

更合适的解决方法(因为您无法在每次使用应用时确实要求用户断开与手机的蓝牙连接),似乎是使用NodeApi检查是否生成的电话节点在附近(使用Node.isNearby()),如果是,请使用MessageClient自行代理API请求,然后在电话上发出请求,最后回复结果。相当麻烦,但对我而言,这是唯一可靠的方式来调用本地API ...

如果有人知道为什么Google会通过蓝牙代理这些来电或者有其他建议来解决这个问题,我也很想知道。

答案 1 :(得分:0)

当佩戴与移动设备连接时,在那种情况下使用移动互联网API代码无效。请从手机上断开磨损,然后再试一次它给你回复。 请查看此链接,我感谢您的帮助:https://www.sitepoint.com/connecting-to-web-services-with-android-wear/

答案 2 :(得分:0)

Wear将使用共享蓝牙连接,活动接口类型将是PROXY但不是WIFI。你可以adb shell dumpsys netstats查看。在这种情况下,磨损不在服务器的同一本地网络中,服务器的套接字将超时。

一种解决方案是让您的服务器具有公共IP或主机名。另一种方法是在访问API之前使用NetworkRequest更改与wifi的连接。请查看Google的示例:https://github.com/googlesamples/android-WearHighBandwidthNetworking