IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Chargement XML depuis un site


Sujet :

Android

  1. #1
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut Chargement XML depuis un site
    Bonjour à tous, j'essaye dans une application de charger un fichier xml depuis un site.

    Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    	 URL url = new URL("http://api.androidhive.info/music/music.xml");
    		        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    		        DocumentBuilder db = dbf.newDocumentBuilder();
    		        InputStream in = url.openStream() ;
    		       Document doc = db.parse(new InputSource(url.openStream()));
     
    		        doc.getDocumentElement().normalize(); ...
    Je travaille avec la version 2.2 d'android.

    Mais lorsque je lance mon application je n'arrive pas à me connecter au site.

    Il semble que sa soit le url.openStream() qui pose problème.

    Avez-vous une solution? Merci d'avance

  2. #2
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Bonjour,

    As tu un rapport d'erreur dans ton logcat ? Pourrais tu nous le poster?

    Merci
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  3. #3
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    InputStream in = url.openStream() ;
    Document doc = db.parse(new InputSource(url.openStream()));
    devrait être
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    InputStream in = url.openStream() ;
    Document doc = db.parse(new InputSource(in));
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  4. #4
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut
    Tous d'abord merci de vos réponses.

    Oui il est vrai que cela irait mieux avec ton bous de code nicroman.

    Cela dit, rien ne change, j'ai toujours le même problème à la ligne :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
     InputStream in = url.openStream() ;
    Non Feanorin je n'ai pas d'apport d'erreur dans mon logcat.

    J'ai test le code dans un try catch de ma méthode onCreate de mon activty

    Voici mon code:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    	setContentView(R.layout.main);
     
    		try {
     
    			 URL url = new URL("http://api.androidhive.info/music/music.xml");
    		        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    		        DocumentBuilder db = dbf.newDocumentBuilder();
    		        Toast.makeText(this, "1", Toast.LENGTH_LONG).show();
    		        InputStream in = url.openStream() ;
    		        Toast.makeText(this, "2", Toast.LENGTH_LONG).show();
    		       Document doc = db.parse(new InputSource(in));
    		       Toast.makeText(this, "3", Toast.LENGTH_LONG).show();
     
    		        doc.getDocumentElement().normalize();
     
    			} catch (Exception e) {
    			System.out.println("XML Pasing Excpetion = " + e);
    			}
    Seulement la valeur 1 s'affiche à l'écran

  5. #5
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Bonjour,

    Non Feanorin je n'ai pas d'apport d'erreur dans mon logcat.
    Donc pas d'exception déjà, on peut enlever de tête l'appel dans le thread Ui et la permission, donc, est ce que tu pourrais nous dire ce que tu as dans la variable in (sa taille et est ce que les donnée présentes sont cohérentes avec ton xml, compare les deux tailles)?

    Car sans information c'est compliqué de t'aider...
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  6. #6
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    Ceci dit... pas sur qu'il n'y ai pas d'exception:
    Avec:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    System.out.println("XML Pasing Excpetion = " + e);
    pas facile de détecter les exceptions....
    Remplacer par:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Log.e("TAG","XML parsing errror",e);
    Au moins, ca ira dans le logcat si il y a une exception.

    Ensuite... vu qu'il y a un appel Internet (url.openStream()), j'imagine que le code tourne dans un thread (au moins une AsyncTask), dans ce cas, pas sur que les "Toast" fonctionne très bien... Préférer un simple:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    Log.d("TAG","Been here... done that...");
    A chaque fois que nécessaire.


    Pour finir, on ne sait même pas quel est le problème initial...
    Le document est vide ? L'InputStream est null ?
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  7. #7
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut
    Non nicroman mon code ne tourne pas dans un thread, est-ce une erreur de ne pas la faire?

    J'ai modifié le code comme tu me la dit, voici mon fichier logCat:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    02-22 15:10:50.514: I/dalvikvm(796): Debugger is active
    02-22 15:10:50.604: I/System.out(796): Debugger has connected
    02-22 15:10:50.604: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:50.814: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:51.014: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:51.224: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:51.424: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:51.634: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:51.834: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:52.037: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:52.234: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:52.444: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:52.644: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:52.899: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:53.104: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:53.317: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:53.525: I/System.out(796): waiting for debugger to settle...
    02-22 15:10:53.730: I/System.out(796): debugger has settled (1349)
    02-22 15:10:54.104: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:54.104: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:55.314: D/TAG(796): Been here... done that...
    02-22 15:10:55.504: E/TAG(796): XML parsing errror
    02-22 15:10:55.504: E/TAG(796): android.os.NetworkOnMainThreadException
    02-22 15:10:55.504: E/TAG(796): 	at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
    02-22 15:10:55.504: E/TAG(796): 	at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    02-22 15:10:55.504: E/TAG(796): 	at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    02-22 15:10:55.504: E/TAG(796): 	at java.net.InetAddress.getAllByName(InetAddress.java:214)
    02-22 15:10:55.504: E/TAG(796): 	at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
    02-22 15:10:55.504: E/TAG(796): 	at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
    02-22 15:10:55.504: E/TAG(796): 	at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
    02-22 15:10:55.504: E/TAG(796): 	at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
    02-22 15:10:55.504: E/TAG(796): 	at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    02-22 15:10:55.504: E/TAG(796): 	at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
    02-22 15:10:55.504: E/TAG(796): 	at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
    02-22 15:10:55.504: E/TAG(796): 	at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
    02-22 15:10:55.504: E/TAG(796): 	at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
    02-22 15:10:55.504: E/TAG(796): 	at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
    02-22 15:10:55.504: E/TAG(796): 	at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
    02-22 15:10:55.504: E/TAG(796): 	at java.net.URL.openStream(URL.java:462)
    02-22 15:10:55.504: E/TAG(796): 	at com.example.acitivtyxml.MainActivity.onCreate(MainActivity.java:41)
    02-22 15:10:55.504: E/TAG(796): 	at android.app.Activity.performCreate(Activity.java:5104)
    02-22 15:10:55.504: E/TAG(796): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    02-22 15:10:55.504: E/TAG(796): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    02-22 15:10:55.504: E/TAG(796): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    02-22 15:10:55.504: E/TAG(796): 	at android.app.ActivityThread.access$600(ActivityThread.java:141)
    02-22 15:10:55.504: E/TAG(796): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    02-22 15:10:55.504: E/TAG(796): 	at android.os.Handler.dispatchMessage(Handler.java:99)
    02-22 15:10:55.504: E/TAG(796): 	at android.os.Looper.loop(Looper.java:137)
    02-22 15:10:55.504: E/TAG(796): 	at android.app.ActivityThread.main(ActivityThread.java:5039)
    02-22 15:10:55.504: E/TAG(796): 	at java.lang.reflect.Method.invokeNative(Native Method)
    02-22 15:10:55.504: E/TAG(796): 	at java.lang.reflect.Method.invoke(Method.java:511)
    02-22 15:10:55.504: E/TAG(796): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    02-22 15:10:55.504: E/TAG(796): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    02-22 15:10:55.504: E/TAG(796): 	at dalvik.system.NativeStart.main(Native Method)
    02-22 15:10:55.644: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:55.654: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:55.804: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:55.854: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:56.024: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:56.059: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:56.074: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:56.134: I/Choreographer(796): Skipped 40 frames!  The application may be doing too much work on its main thread.
    02-22 15:10:56.214: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:56.214: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:56.264: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:56.264: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:56.284: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:56.284: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:56.304: D/gralloc_goldfish(796): Emulator without GPU emulation detected.
    02-22 15:10:56.354: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:10:56.394: W/Trace(796): Unexpected value from nativeGetEnabledTags: 0

  8. #8
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut
    Le précèdent était en mode debug, celui là est en mode Run.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    02-22 15:27:26.286: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:26.324: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:26.324: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:26.574: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:26.574: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:26.834: D/TAG(720): Been here... done that...
    02-22 15:27:26.864: E/TAG(720): XML parsing errror
    02-22 15:27:26.864: E/TAG(720): android.os.NetworkOnMainThreadException
    02-22 15:27:26.864: E/TAG(720): 	at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
    02-22 15:27:26.864: E/TAG(720): 	at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
    02-22 15:27:26.864: E/TAG(720): 	at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    02-22 15:27:26.864: E/TAG(720): 	at java.net.InetAddress.getAllByName(InetAddress.java:214)
    02-22 15:27:26.864: E/TAG(720): 	at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
    02-22 15:27:26.864: E/TAG(720): 	at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
    02-22 15:27:26.864: E/TAG(720): 	at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
    02-22 15:27:26.864: E/TAG(720): 	at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
    02-22 15:27:26.864: E/TAG(720): 	at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    02-22 15:27:26.864: E/TAG(720): 	at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
    02-22 15:27:26.864: E/TAG(720): 	at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
    02-22 15:27:26.864: E/TAG(720): 	at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
    02-22 15:27:26.864: E/TAG(720): 	at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
    02-22 15:27:26.864: E/TAG(720): 	at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
    02-22 15:27:26.864: E/TAG(720): 	at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
    02-22 15:27:26.864: E/TAG(720): 	at java.net.URL.openStream(URL.java:462)
    02-22 15:27:26.864: E/TAG(720): 	at com.example.acitivtyxml.MainActivity.onCreate(MainActivity.java:41)
    02-22 15:27:26.864: E/TAG(720): 	at android.app.Activity.performCreate(Activity.java:5104)
    02-22 15:27:26.864: E/TAG(720): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    02-22 15:27:26.864: E/TAG(720): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    02-22 15:27:26.864: E/TAG(720): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    02-22 15:27:26.864: E/TAG(720): 	at android.app.ActivityThread.access$600(ActivityThread.java:141)
    02-22 15:27:26.864: E/TAG(720): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    02-22 15:27:26.864: E/TAG(720): 	at android.os.Handler.dispatchMessage(Handler.java:99)
    02-22 15:27:26.864: E/TAG(720): 	at android.os.Looper.loop(Looper.java:137)
    02-22 15:27:26.864: E/TAG(720): 	at android.app.ActivityThread.main(ActivityThread.java:5039)
    02-22 15:27:26.864: E/TAG(720): 	at java.lang.reflect.Method.invokeNative(Native Method)
    02-22 15:27:26.864: E/TAG(720): 	at java.lang.reflect.Method.invoke(Method.java:511)
    02-22 15:27:26.864: E/TAG(720): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    02-22 15:27:26.864: E/TAG(720): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    02-22 15:27:26.864: E/TAG(720): 	at dalvik.system.NativeStart.main(Native Method)
    02-22 15:27:26.914: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:26.914: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:26.924: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:26.944: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:26.994: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:26.994: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:26.994: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:27.024: I/Choreographer(720): Skipped 30 frames!  The application may be doing too much work on its main thread.
    02-22 15:27:27.064: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:27.084: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:27.094: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:27.094: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:27.094: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:27.104: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:27.124: D/gralloc_goldfish(720): Emulator without GPU emulation detected.
    02-22 15:27:27.164: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0
    02-22 15:27:27.224: W/Trace(720): Unexpected value from nativeGetEnabledTags: 0

  9. #9
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut


    E/TAG(720): XML parsing errror
    02-22 15:27:26.864: E/TAG(720): android.os.NetworkOnMainThreadException
    02-22 15:27:26.864: E/TAG(720): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
    ben tu as bien une exception, à toi maintenant de la corriger

    Non nicroman mon code ne tourne pas dans un thread, est-ce une erreur de ne pas la faire?
    Oui c'est interdit de faire un appel au http depuis l'UIThread.

    Edit : Je n'avais pas vu le setContentView dans le second post .
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  10. #10
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut Résolu
    Merci à vous Feanorin et nicroman. J'ai pu résoudre mon problème en rajoutant ces lignes de code dans ma méthode on create() :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    	if (android.os.Build.VERSION.SDK_INT > 9) {
    			StrictMode.ThreadPolicy policy = 
    			        new StrictMode.ThreadPolicy.Builder().permitAll().build();
    			StrictMode.setThreadPolicy(policy);
    			}
    Problème résolu

  11. #11
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    J'ai pu résoudre mon problème en rajoutant ces lignes de code dans ma méthode on create() :
    Mais, comment dire si Google s'est embêté à rajouter un exception de ce type ce n'est pas pour que les développeur l'ignore ...

    Si il refuse le droit de faire du réseaux à parti du ThreadUI c'est pour une raison qui est que le ThreadUI ne doit pas être bloquant. Donc au lieu de chercher à contourner le problème essaye de le comprendre.

    Passe ta connexion réseaux dans un autre Thread que le Thread UI.
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  12. #12
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut


    Le système me dit que je fais pas les choses bien... donc.... je "mute" le système...
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  13. #13
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut
    Pas possible de faire comme cela? :/
    Je ne comprends pas ce que tu veux dire par "Passe ta connexion réseaux dans un autre Thread que le Thread UI".

    Est-ce ça? :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
     
    		setContentView(R.layout.activity_main);
    		XmlConection();
    	}
     
     
    	public void XmlConection()
    	{
    	    runOnUiThread(new Runnable() {
    	        public void run()
    	        {
    	  		  try {
     
    					 URL url = new URL("http://api.androidhive.info/music/music.xml");
    					 Toast.makeText(MainActivity.this, "-1", Toast.LENGTH_LONG).show();
    				        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    				        Toast.makeText(MainActivity.this, "0", Toast.LENGTH_LONG).show();
    				        DocumentBuilder db = dbf.newDocumentBuilder();
    				        Log.d("TAG","Been here... done that...");
    				        Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
    				        InputStream in = url.openStream() ;
    				        Toast.makeText(MainActivity.this, "2", Toast.LENGTH_LONG).show();
    				        Log.d("TAG","Been here... done that...");
     
    				       Document doc = db.parse(new InputSource(in));
    				       Log.d("TAG","Been here... done that...");
    				       Toast.makeText(MainActivity.this, "3", Toast.LENGTH_LONG).show();
     
    				        doc.getDocumentElement().normalize();
     
    					} catch (Exception e) {
    						Log.e("TAG","XML parsing errror",e);
     
    					}
    	        }
    	    });
    	}

  14. #14
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut
    J'ai essayé aussi ceci, je pense que tu me parlais de ça ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    @Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
     
    		setContentView(R.layout.activity_main);
    		XmlConection();
    	}
     
     
    	public void XmlConection()
    	{
    		Thread thread = new Thread(new Runnable() {
    	        public void run()
    	        {
    	  		  try {
    	  			  	Thread.sleep(100L);
    					 URL url = new URL("http://api.androidhive.info/music/music.xml");
    					 Toast.makeText(MainActivity.this, "-1", Toast.LENGTH_LONG).show();
    				        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    				        Toast.makeText(MainActivity.this, "0", Toast.LENGTH_LONG).show();
    				        DocumentBuilder db = dbf.newDocumentBuilder();
    				        Log.d("TAG","Been here... done that...");
    				        Toast.makeText(MainActivity.this, "1", Toast.LENGTH_LONG).show();
    				        InputStream in = url.openStream() ;
    				        Toast.makeText(MainActivity.this, "2", Toast.LENGTH_LONG).show();
    				        Log.d("TAG","Been here... done that...");
     
    				       Document doc = db.parse(new InputSource(in));
    				       Log.d("TAG","Been here... done that...");
    				       Toast.makeText(MainActivity.this, "3", Toast.LENGTH_LONG).show();
     
    				        doc.getDocumentElement().normalize();
     
    					} catch (Exception e) {
    						Log.e("TAG","XML parsing errror",e);
     
    					}
    	        }
    	    });
    		thread.start();
     
    	}

  15. #15
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    La seconde est la réponse .

    En gros toute connexion réseaux ne doit pas se faire dans le Thread UI, dans ton premier exemple le runOnThreadUI est un parfait exemple de ce qui ne faut pas faire .
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  16. #16
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut
    Ok je ne le ferais plus alors.. J'ai enlevé tous mes toast et j'ai run, mais j'ai une nouvelle erreur:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    02-25 15:47:06.000: E/Trace(793): error opening trace file: No such file or directory (2)
    La connais tu?

  17. #17
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Peux tu nous mettre tout ton logcat ?
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  18. #18
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut
    02-25 15:45:48.641: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:48.641: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:48.641: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:48.810: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:48.810: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.230: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.230: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.240: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.270: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.310: D/TAG(742): Been here... done that...
    02-25 15:45:49.380: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.380: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.390: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.490: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.490: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.500: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.500: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.510: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.510: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.530: D/gralloc_goldfish(742): Emulator without GPU emulation detected.
    02-25 15:45:49.592: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:49.650: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:45:51.560: D/TAG(742): Been here... done that...
    02-25 15:45:51.861: D/TAG(742): Been here... done that...
    02-25 15:46:58.000: W/Trace(742): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.000: E/Trace(793): error opening trace file: No such file or directory (2)
    02-25 15:47:06.000: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.000: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.000: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.210: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.260: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.550: D/TAG(793): Been here... done that...
    02-25 15:47:06.610: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.610: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.631: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.640: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.720: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.720: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.730: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.790: I/Choreographer(793): Skipped 47 frames! The application may be doing too much work on its main thread.
    02-25 15:47:06.820: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.820: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.830: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.830: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.830: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.830: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.850: D/gralloc_goldfish(793): Emulator without GPU emulation detected.
    02-25 15:47:06.880: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:06.960: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:47:07.160: D/TAG(793): Been here... done that...
    02-25 15:47:07.750: D/TAG(793): Been here... done that...
    02-25 15:56:39.236: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:56:39.246: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:56:39.286: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0
    02-25 15:56:39.286: W/Trace(793): Unexpected value from nativeGetEnabledTags: 0

  19. #19
    Expert éminent

    Homme Profil pro
    Ingénieur systèmes et réseaux
    Inscrit en
    Février 2007
    Messages
    4 253
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur systèmes et réseaux
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Février 2007
    Messages : 4 253
    Points : 7 618
    Points
    7 618
    Billets dans le blog
    3
    Par défaut
    Non mais c'est pas grave ça... il n'y a pas de trace-file sur emulateur....
    Ce n'est d'ailleurs pas une exception...

    Sinon, pour ne pas s’embêter avec les Handler et les Thread... il y a les Loader et aussi les AsyncTask....
    N'oubliez pas de cliquer sur mais aussi sur si un commentaire vous a été utile !
    Et surtout

  20. #20
    Membre à l'essai
    Profil pro
    Inscrit en
    Février 2013
    Messages
    35
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2013
    Messages : 35
    Points : 14
    Points
    14
    Par défaut
    Non c'est bon le Thread me va, je peux bien me connecter et lire mon fichier xml.

    Mais j'ai un autre problème, j'essaye de lire les informations de mon xml ajouté à ma list aTableRetour.

    Mais lorsque j'essaye de la lire en dehors de mon thread, rien ne s'affiche parce que ma list est vide.

    Voici mon code:


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    		XmlConection();
    	}
     
     
    	ArrayList<ArrayList> aTableRetour = new ArrayList<ArrayList>();
    	public void XmlConection()
    	{
    		Thread thread = new Thread(new Runnable() {
    	        public void run()
    	        {
    	  		  try {
     
     
    					 URL url = new URL("http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
     
    				        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
     
    				        DocumentBuilder db = dbf.newDocumentBuilder();
    				        Log.d("TAG","Been here... done that...");
     
    				        InputStream in = url.openStream() ;
     
    				        Log.d("TAG","Been here... done that...");
     
    				        Document  doc = db.parse(new InputSource(in));
    				       Log.d("TAG","Been here... done that...");
     
     
    				        doc.getDocumentElement().normalize();
     
    				      NodeList nodeList = doc.getElementsByTagName("website");
     
    				        for (int i = 0; i < nodeList.getLength(); i++) {
    				        ArrayList<String>  aTableauTmp =  new ArrayList<String>(); 
    				        Element fstElmnt = (Element) nodeList.item(i);
    				        aTableauTmp.add(fstElmnt.getAttribute("category"));
    				        aTableRetour.add(aTableauTmp);
    				        }
     
    					} catch (Exception e) {
    						Log.e("TAG","XML parsing errror",e);
     
    					}
     
     
    	        }
    	    });
    		//thread.start();
    	    for(int i=0; i< aTableRetour.size(); i++){
    	    	 ArrayList tmp= (ArrayList) aTableRetour.get(i);
    	    	 Toast.makeText(MainActivity.this, "Teste",Toast.LENGTH_LONG).show(); 	
    		 }

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. Enregistrer le contenu XML depuis site web
    Par h2qutc dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 07/03/2014, 13h07
  2. chargement images depuis un site
    Par Gob59 dans le forum Android
    Réponses: 3
    Dernier message: 30/04/2013, 17h35
  3. Chargement DataGrid depuis XML
    Par urubs dans le forum ActionScript 3
    Réponses: 1
    Dernier message: 25/10/2011, 19h50
  4. [AJAX/XML/JAVA] Afficher un flux de données depuis un site web
    Par chodaboy34 dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 10/10/2006, 16h38
  5. Recupération de données XML depuis une URL
    Par tonymo dans le forum Format d'échange (XML, JSON...)
    Réponses: 27
    Dernier message: 21/11/2005, 13h48

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo