小部件更新服务无法正常工作

时间:2014-08-26 17:32:33

标签: android widget android-widget networkonmainthread

我的小部件更新服务正在抛出: 当我尝试从我的Web服务器获取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):at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 08-26 19:22:14.941:W / System.err(16244):at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 08-26 19:22:14.941:W / System.err(16244):at java.net.InetAddress.getAllByName(InetAddress.java:214) 08-26 19:22:14.941:W / System.err(16244):at libcore.net.http.HttpConnection。(HttpConnection.java:70) 08-26 19:22:14.941:W / System.err(16244):at libcore.net.http.HttpConnection。(HttpConnection.java:50) 08-26 19:22:14.941:W / System.err(16244):at libcore.net.http.HttpConnection $ Address.connect(HttpConnection.java:340) 08-26 19:22:14.941:W / System.err(16244):at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 08-26 19:22:14.941:W / System.err(16244):at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 08-26 19:22:14.956:W / System.err(16244):at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316) 08-26 19:22:14.956:W / System.err(16244):at libcore.net.http.HttpEngine.connect(HttpEngine.java:311) 08-26 19:22:14.956:W / System.err(16244):at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) 08-26 19:22:14.961:W / System.err(16244):at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240) 08-26 19:22:14.961:W / System.err(16244):at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81) 08-26 19:22:14.961:W / System.err(16244):at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197) 08-26 19:22:14.961:W / System.err(16244):at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:120) 08-26 19:22:14.961:W / System.err(16244):at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:176) 08-26 19:22:14.961:W / System.err(16244):at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114) 08-26 19:22:14.961:W / System.err(16244):at com.aek.callstatistics.UpdateWidgetService.soapGetOperators(UpdateWidgetService.java:280) 08-26 19:22:14.961:W / System.err(16244):at 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)

1 个答案:

答案 0 :(得分:0)

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