Android上的服务启动android.os.NetworkOnMainThreadException

Android上的服务启动android.os.NetworkOnMainThreadException,第1张

概述在 android上尝试我的全新服务之后我得到了这个: 我猜是与清单文件和权限相关的东西,服务在最后一个活动之后启动,更新服务器上的数据并检索新数据并在android上的sqlite上保存id: 这里还有清单文件: <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.gg 在 android上尝试我的全新服务之后我得到了这个:

我猜是与清单文件和权限相关的东西,服务在最后一个活动之后启动,更新服务器上的数据并检索新数据并在androID上的sqlite上保存ID:

这里还有清单文件:

<manifest xmlns:androID="http://schemas.androID.com/apk/res/androID"        package="com.ggservice.democracy"        androID:versionCode="1"        androID:versionname="1.0" >        <uses-sdk            androID:minSdkVersion="8"            androID:targetSdkVersion="17" />        <uses-permission androID:name="androID.permission.INTERNET"/>        <application            androID:allowBackup="true"            androID:icon="@drawable/ic_launcher"            androID:label="@string/app_name"            androID:theme="@style/Apptheme" >            <activity                androID:name="com.ggservice.democracy.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>            <activity androID:label="@string/app_name" androID:name="com.ggservice.democracy.sondaggioActivity"/>            <activity androID:label="@string/app_name" androID:name="com.ggservice.democracy.domandeDiCategoria"/>            <service androID:name="com.ggservice.democracy.updateDemocracyService" />        </application>    </manifest>

logcat:

01-02 15:33:30.960: W/dalvikvm(2570): threadID=1: thread exiting with uncaught exception (group=0x409c01f8)01-02 15:33:31.160: E/AndroIDRuntime(2570): FATAL EXCEPTION: main01-02 15:33:31.160: E/AndroIDRuntime(2570): java.lang.RuntimeException: Unable to start service com.ggservice.democracy.updateDemocracyService@412f0c60 with Intent { cmp=com.ggservice.democracy/.updateDemocracyService }: androID.os.networkonmainthreadException01-02 15:33:31.160: E/AndroIDRuntime(2570):     at androID.app.ActivityThread.handleServiceArgs(ActivityThread.java:2376)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at androID.app.ActivityThread.access00(ActivityThread.java:123)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at androID.os.Handler.dispatchMessage(Handler.java:99)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at androID.os.Looper.loop(Looper.java:137)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at androID.app.ActivityThread.main(ActivityThread.java:4424)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at java.lang.reflect.Method.invokeNative(Native Method)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at java.lang.reflect.Method.invoke(Method.java:511)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:551)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at dalvik.system.NativeStart.main(Native Method)01-02 15:33:31.160: E/AndroIDRuntime(2570): Caused by: androID.os.networkonmainthreadException01-02 15:33:31.160: E/AndroIDRuntime(2570):     at androID.os.StrictMode$AndroIDBlockGuardPolicy.onNetwork(StrictMode.java:1099)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at java.net.InetAddress.lookupHostByname(InetAddress.java:391)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at java.net.InetAddress.getAllBynameImpl(InetAddress.java:242)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at java.net.InetAddress.getAllByname(InetAddress.java:220)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at org.apache.http.impl.conn.DefaultClIEntConnectionoperator.openConnection(DefaultClIEntConnectionoperator.java:137)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at org.apache.http.impl.clIEnt.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at org.apache.http.impl.clIEnt.AbstracthttpClIEnt.execute(AbstracthttpClIEnt.java:555)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at org.apache.http.impl.clIEnt.AbstracthttpClIEnt.execute(AbstracthttpClIEnt.java:487)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at org.apache.http.impl.clIEnt.AbstracthttpClIEnt.execute(AbstracthttpClIEnt.java:465)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at com.ggservice.democracy.JsONParser.getJsONFromUrl(JsONParser.java:38)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at com.ggservice.democracy.updateDemocracyService.onStartCommand(updateDemocracyService.java:47)01-02 15:33:31.160: E/AndroIDRuntime(2570):     at androID.app.ActivityThread.handleServiceArgs(ActivityThread.java:2359)01-02 15:33:31.160: E/AndroIDRuntime(2570):     ... 10 more

难道我做错了什么?

这也是服务:

public class updateDemocracyService extends Service{    private pollDataSource datasource;     int mStartMode;       // indicates how to behave if the service is killed        IBinder mBinder;      // interface for clIEnts that bind        boolean mAllowRebind; // indicates whether onRebind should be used        // url to make request        private static String url = "http://www.test.com/democracy/domande.PHP";        // JsON Node names        private static final String TAG_DOMANDE = "domande";        private static final String TAG_ID = "ID";        private static final String TAG_TESTO = "testo";        // contacts JsONArray        JsONArray contacts = null;        @OverrIDe        public voID onCreate() {            // The service is being created            datasource = new pollDataSource(this);            datasource.open();        }        @OverrIDe        public int onStartCommand(Intent intent,int flags,int startID) {            // Creating JsON Parser instance            JsONParser jParser = new JsONParser();            // getting JsON string from URL            JsONObject Json = jParser.getJsONFromUrl(url);            try {                // Getting Array of Contacts                contacts = Json.getJsONArray(TAG_DOMANDE);                // looPing through All Contacts                for(int i = 0; i < contacts.length(); i++){                    JsONObject c = contacts.getJsONObject(i);                    // Storing each Json item in variable                    String ID = c.getString(TAG_ID);                    String name = c.getString(TAG_TESTO);                    datasource.createCategoria(name);                }            } catch (JsONException e) {                e.printstacktrace();            }            Toast.makeText(this,"ho comunicato con un server!",Toast.LENGTH_LONG).show();              return mStartMode;        }        @OverrIDe        public IBinder onBind(Intent intent) {            // A clIEnt is binding to the service with bindService()            return mBinder;        }        @OverrIDe        public boolean onUnbind(Intent intent) {            // All clIEnts have unbound with unbindService()            return mAllowRebind;        }        @OverrIDe        public voID onRebind(Intent intent) {            // A clIEnt is binding to the service with bindService(),// after onUnbind() has already been called        }        @OverrIDe        public voID onDestroy() {            datasource.close();            // The service is no longer used and is being destroyed        }}
解决方法 发生这种情况是因为您正在主线程上进行网络 *** 作,而AndroID 3.0及更高版本不允许这样做.即使它在服务中,服务也在UI线程上运行,除非您在另一个线程中专门启动它们或在其中创建一个线程.

您可以通过使用Thread或AsyncTask在主UI线程中运行服务中的任务来解决此问题.

尝试在onStartCommand()中创建一个新线程,如@CommonsWare所建议的那样.

总结

以上是内存溢出为你收集整理的Android上的服务启动android.os.NetworkOnMainThreadException全部内容,希望文章能够帮你解决Android上的服务启动android.os.NetworkOnMainThreadException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1136849.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存