2014-03-28 83 views
0

我正在做一個應用程序,其中一個服務啓動一個GPSListener,它定期獲取位置更新的時間。使用這部分它的woking很好,而且Im正確獲取更新。一個GPS偵聽器中的http post請求...由一個服務發起的

我需要更新這個緯度,長的值到服務器數據庫,爲此我使用http post請求到php腳本;單獨工作fien.But當它在LocationListener中調用時,Im得到以下錯誤stacktrace 。

我該如何克服這種情況並提出要求?

> android.os.NetworkOnMainThreadException 
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 
at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 
at java.net.InetAddress.getAllByName(InetAddress.java:214) 
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 
at com.childtrack.service.SimpleService$mylocationlistener.postData(SimpleService.java:165) 
at com.childtrack.service.SimpleService$mylocationlistener.onLocationChanged(SimpleService.java:101) 
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:237) 
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:170) 
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:186) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4899) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558) 
at dalvik.system.NativeStart.main(Native Method) 
W/System.err(5560): android.os.NetworkOnMainThreadException 
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117) 
at java.net.InetAddress.lookupHostByName(InetAddress.java:385) 
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 
at java.net.InetAddress.getAllByName(InetAddress.java:214) 
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137) 
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 
at com.childtrack.service.SimpleService$mylocationlistener.postData(SimpleService.java:165) 
at com.childtrack.service.SimpleService$mylocationlistener.onLocationChanged(SimpleService.java:101) 
at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:237) 
at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:170) 
at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:186) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4899) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:511) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558) 
at dalvik.system.NativeStart.main(Native Method) 

回答

1

您在主(UI)線程上正在執行network operation。請使用asyntask進行網絡操作。 android不允許在主線程上執行網絡操作。因爲它可能會凍結UI

每當發生這種異常android.os.NetworkOnMainThreadException它的意思是你已經在UI上做了一些網絡操作。

儘管服務用於後臺處理,但它們在主線程中運行。

從文檔的簡短報價:

Caution: A services runs in the same process as the application in which 
it is declared and in the main thread of that application, by default. So, 
if your service performs intensive or blocking operations while the user 
interacts with an activity from the same application, the service will 
slow down activity performance. To avoid impacting application performance, 
you should start a new thread inside the service. 
+0

但是@wqrahd,具有服務內的請求林... whethere也將被視爲一個UI線程??? – subrussn90

+0

我從來沒有使用服務,但我想是的。這就是爲什麼它拋出異常。 –

+1

我剛剛讀取的服務也在主線程中運行。我正在更新我的答案。 –

相關問題