solucionar networkonmainthreadexception error como caused android android-emulator android-parser networkonmainthread

error - Causado por: android.os.NetworkOnMainThreadException



como solucionar android os networkonmainthreadexception (1)

Hay dos soluciones de este problema.

1) No escriba una llamada de red en el subproceso de la interfaz de usuario principal, use la tarea asíncrona para eso.

2) Escriba el código a continuación en su archivo MainActivity después de 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); }

Y debajo de la declaración de importación en su archivo java.

import android.os.StrictMode;

Hola a todos los espectadores y lectores, estoy usando el link . Cada vez que ejecuto este código en un paquete separado, funciona bien, pero siempre que trato de incrustar esta muestra en mi aplicación, la aplicación Lamentablemente se ha detenido, solo cuando hago clic en la pestaña en la que lo llamo, y Logcat dice:

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

dígame lo que necesito hacer para que sea útil para mí, excepto que esta aplicación particular de la pestaña funciona bien.

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