web-dev-qa-db-fra.com

Causé par: Android.os.NetworkOnMainThreadException

Salut tous les téléspectateurs et lecteurs, j'utilise lien . Chaque fois que j'exécute ce code dans un package séparé, cela fonctionne bien, mais chaque fois que j'essaie d'incorporer cet exemple dans mon application, obtenir l'application malheureusement s'est arrêté, uniquement lorsque je clique sur l'onglet de cet onglet particulier qui l'appelle, et Logcat dit: -

Caused by: Android.os.NetworkOnMainThreadException
10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.Java:1117),

s'il vous plaît dites-moi ce que je dois faire pour le rendre utile pour moi, sauf que cette application particulière de l'onglet particulier fonctionne correctement.

  10-30 15:07:26.770: D/dalvikvm(732): GC_FOR_ALLOC freed 61K, 3% free 8035K/8259K, paused 39ms, total 46ms
    10-30 15:07:26.781: I/dalvikvm-heap(732): Grow heap (frag case) to 8.481MB for 614416-byte allocation
    10-30 15:07:26.850: D/dalvikvm(732): GC_CONCURRENT freed 1K, 4% free 8634K/8903K, paused 27ms+13ms, total 71ms
    10-30 15:07:26.881: D/dalvikvm(732): GC_FOR_ALLOC freed 0K, 4% free 8634K/8903K, paused 28ms, total 28ms
    10-30 15:07:26.900: I/dalvikvm-heap(732): Grow heap (frag case) to 9.798MB for 1382416-byte allocation
    10-30 15:07:26.970: D/dalvikvm(732): GC_CONCURRENT freed 0K, 4% free 9984K/10311K, paused 28ms+5ms, total 72ms
    10-30 15:07:27.110: D/gralloc_goldfish(732): Emulator without GPU emulation detected.
    10-30 15:07:30.591: D/dalvikvm(732): GC_FOR_ALLOC freed 675K, 8% free 9875K/10695K, paused 33ms, total 62ms
    10-30 15:07:30.671: D/dalvikvm(732): GC_FOR_ALLOC freed 519K, 9% free 9806K/10695K, paused 30ms, total 31ms
    10-30 15:07:30.740: D/dalvikvm(732): GC_FOR_ALLOC freed 310K, 10% free 9713K/10695K, paused 28ms, total 29ms
    10-30 15:07:30.751: I/dalvikvm-heap(732): Grow heap (frag case) to 10.488MB for 1000016-byte allocation
    10-30 15:07:30.820: D/dalvikvm(732): GC_CONCURRENT freed <1K, 9% free 10690K/11719K, paused 25ms+5ms, total 74ms
    10-30 15:07:30.911: D/dalvikvm(732): GC_FOR_ALLOC freed 0K, 9% free 10690K/11719K, paused 36ms, total 36ms
    10-30 15:07:30.931: I/dalvikvm-heap(732): Grow heap (frag case) to 12.633MB for 2250016-byte allocation
    10-30 15:07:31.030: D/dalvikvm(732): GC_CONCURRENT freed 0K, 8% free 12887K/13959K, paused 32ms+16ms, total 93ms
    10-30 15:07:31.211: D/dalvikvm(732): GC_FOR_ALLOC freed 3683K, 30% free 10314K/14599K, paused 35ms, total 38ms
    10-30 15:07:31.211: I/dalvikvm-heap(732): Grow heap (frag case) to 11.439MB for 1382416-byte allocation
    10-30 15:07:31.360: D/dalvikvm(732): GC_CONCURRENT freed <1K, 21% free 11663K/14599K, paused 81ms+5ms, total 144ms
    10-30 15:07:31.551: D/dalvikvm(732): GC_FOR_ALLOC freed 2555K, 32% free 9936K/14599K, paused 32ms, total 38ms
    10-30 15:07:31.561: I/dalvikvm-heap(732): Grow heap (frag case) to 10.706MB for 1000016-byte allocation
    10-30 15:07:31.701: D/dalvikvm(732): GC_CONCURRENT freed 1K, 26% free 10912K/14599K, paused 78ms+16ms, total 139ms
    10-30 15:07:31.770: D/dalvikvm(732): GC_FOR_ALLOC freed 0K, 26% free 10912K/14599K, paused 31ms, total 31ms
    10-30 15:07:31.770: I/dalvikvm-heap(732): Grow heap (frag case) to 12.850MB for 2250016-byte allocation
    10-30 15:07:31.871: D/dalvikvm(732): GC_CONCURRENT freed 0K, 11% free 13109K/14599K, paused 29ms+6ms, total 91ms
    10-30 15:07:32.060: D/dalvikvm(732): GC_FOR_ALLOC freed 1134K, 11% free 13102K/14599K, paused 35ms, total 39ms
    10-30 15:07:32.070: I/dalvikvm-heap(732): Grow heap (frag case) to 14.162MB for 1382416-byte allocation
    10-30 15:07:32.150: D/dalvikvm(732): GC_FOR_ALLOC freed <1K, 10% free 14451K/16007K, paused 36ms, total 36ms
    10-30 15:07:32.161: I/Choreographer(732): Skipped 460 frames!  The application may be doing too much work on its main thread.
    10-30 15:07:35.280: D/AndroidRuntime(732): Shutting down VM
    10-30 15:07:35.280: W/dalvikvm(732): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
    10-30 15:07:35.341: E/AndroidRuntime(732): FATAL EXCEPTION: main
    10-30 15:07:35.341: E/AndroidRuntime(732): Java.lang.RuntimeException: Unable to start activity ComponentInfo{com.erachnida.restaurant.versionoct/com.erachnida.restaurant.versionoct.CustomizedListView}: Android.os.NetworkOnMainThreadException
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2059)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.app.ActivityThread.startActivityNow(ActivityThread.Java:1900)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.app.LocalActivityManager.moveToState(LocalActivityManager.Java:135)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.app.LocalActivityManager.startActivity(LocalActivityManager.Java:347)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.Java:694)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.widget.TabHost.setCurrentTab(TabHost.Java:358)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.widget.TabHost$2.onTabSelectionChanged(TabHost.Java:150)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.widget.TabWidget$TabClickListener.onClick(TabWidget.Java:546)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.view.View.performClick(View.Java:4084)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.view.View$PerformClick.run(View.Java:16966)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.os.Handler.handleCallback(Handler.Java:615)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.os.Handler.dispatchMessage(Handler.Java:92)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.os.Looper.loop(Looper.Java:137)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.app.ActivityThread.main(ActivityThread.Java:4745)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Java.lang.reflect.Method.invokeNative(Native Method)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Java.lang.reflect.Method.invoke(Method.Java:511)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:786)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:553)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at dalvik.system.NativeStart.main(Native Method)
    10-30 15:07:35.341: E/AndroidRuntime(732): Caused by: Android.os.NetworkOnMainThreadException
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.Java:1117)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Java.net.InetAddress.lookupHostByName(InetAddress.Java:385)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Java.net.InetAddress.getAllByNameImpl(InetAddress.Java:236)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Java.net.InetAddress.getAllByName(InetAddress.Java:214)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at org.Apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.Java:137)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at org.Apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.Java:164)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at org.Apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.Java:119)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at org.Apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.Java:360)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:555)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:487)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:465)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at com.erachnida.restaurant.versionoct.XMLParser.getXmlFromUrl(XMLParser.Java:45)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at com.erachnida.restaurant.versionoct.CustomizedListView.onCreate(CustomizedListView.Java:42)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.app.Activity.performCreate(Activity.Java:5008)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1079)
    10-30 15:07:35.341: E/AndroidRuntime(732):  at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2023)
    10-30 15:07:35.341: E/AndroidRuntime(732):  ... 18 more
    10-30 15:07:42.250: I/Process(732): Sending signal. PID: 732 SIG: 9
21
Stanley

Il existe deux solutions à ce problème.

1) N'écrivez pas d'appel réseau dans le thread d'interface utilisateur principal, utilisez la tâche asynchrone pour cela.

2) Écrivez le code ci-dessous dans votre fichier MainActivity après setContentView (R.layout.activity_main);

if (Android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

Et en dessous de la déclaration d'importation dans votre fichier Java.

import Android.os.StrictMode;
113
Dipak Keshariya