【问题标题】:Widget update service not working properly小部件更新服务无法正常工作
【发布时间】:2014-10-20 03:11:30
【问题描述】:

我的小部件更新服务正在抛出: 当我尝试从我的网络服务器获取 SOAP 响应时出现“android.os.NetworkOnMainThreadException”。

我确信 SOAP 代码没有错误,因为我只通过 IntentService 而不是 Service 在应用程序中使用相同的代码。

我根据最后一个示例设计了我的小部件:http://www.vogella.com/tutorials/AndroidWidgets/article.html

错误代码: 08-26 19:22:14.931: W/System.err(16244): android.os.NetworkOnMainThreadException 08-26 19:22:14.936: W/System.err(16244): 在 android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1125) 08-26 19:22:14.941: W/System.err(16244): 在 java.net.InetAddress.lookupHostByName(InetAddress.java:385) 08-26 19:22:14.941: W/System.err(16244): 在 java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 08-26 19:22:14.941: W/System.err(16244): 在 java.net.InetAddress.getAllByName(InetAddress.java:214) 08-26 19:22:14.941: W/System.err(16244): 在 libcore.net.http.HttpConnection.(HttpConnection.java:70) 08-26 19:22:14.941: W/System.err(16244): 在 libcore.net.http.HttpConnection.(HttpConnection.java:50) 08-26 19:22:14.941: W/System.err(16244): 在 libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) 08-26 19:22:14.941: W/System.err(16244): 在 libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 08-26 19:22:14.941: W/System.err(16244): 在 libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 08-26 19:22:14.956: W/System.err(16244): 在 libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316) 08-26 19:22:14.956: W/System.err(16244): 在 libcore.net.http.HttpEngine.connect(HttpEngine.java:311) 08-26 19:22:14.956: W/System.err(16244): 在 libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) 08-26 19:22:14.961: W/System.err(16244): 在 libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240) 08-26 19:22:14.961: W/System.err(16244): 在 libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81) 08-26 19:22:14.961: W/System.err(16244): 在 libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197) 08-26 19:22:14.961: W/System.err(16244): 在 org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:120) 08-26 19:22:14.961: W/System.err(16244): 在 org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:176) 08-26 19:22:14.961: W/System.err(16244): 在 org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114) 08-26 19:22:14.961: W/System.err(16244): 在 com.aek.callstatistics.UpdateWidgetService.soapGetOperators(UpdateWidgetService.java:280) 08-26 19:22:14.961: W/System.err(16244): 在 com.aek.callstatistics.UpdateWidgetService.callLogUpdate(UpdateWidgetService.java:245) 08-26 19:22:14.966: W/System.err(16244): at com.aek.callstatistics.UpdateWidgetService$1.done(UpdateWidgetService.java:481)

【问题讨论】:

    标签: android widget android-widget networkonmainthread


    【解决方案1】:

    这对您的应用来说是一个安全问题,因为在应用的主线程上执行网络操作会阻塞所有其他应用操作。重构您的 Service 以使用后台线程来执行网络操作并为您的小部件提供结果。为此,您可以使用AsyncTask,或者更高级一点并使用您自己的线程池等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-11
      • 1970-01-01
      • 2018-06-12
      • 2015-03-06
      • 1970-01-01
      • 2020-12-24
      相关资源
      最近更新 更多