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 :

String vers un xml


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Avril 2007
    Messages
    35
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 35
    Par défaut String vers un xml
    Bonsoir,
    Peut être mon problème ne concerne pas android directement, mais c'est le cadre de mon projet. En fait, mon programme consiste a ce connecter à un web service et extraire les données contenant dans la résultat (qui est sous forme d'un xml).mon problème c'est que la réponse du client est sous forme de string moi je veux transformer ce bout de texte de nouveau un fichier sous forme xml. J'ai utilisé la fonction suivante parse mais je ne sais pas c'est quoi le problème

    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
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
     
     
    package lol.siter;
     
    import java.io.ByteArrayInputStream;
     
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
     
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    /*
     NOTE: You have to give this app permission to access
     the internet!!! See the AndroidManifest.xml file
     */
    public class NKHttpClient extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
     
            final EditText txtUrl = (EditText)findViewById(R.id.url);
            final Button btnFetch = (Button)findViewById(R.id.button);
            final TextView txtResult = (TextView)findViewById(R.id.content);
     
            btnFetch.setOnClickListener(new Button.OnClickListener(){
                public void onClick(View v){
                    getRequest(txtResult,txtUrl);
     
     
     
                    String txt=(String) txtResult.getText();
     
                    try {
                    	Toast.makeText(v.getContext(),"2" ,Toast.LENGTH_SHORT).show();
    					parse(txt);
    					Toast.makeText(v.getContext(),"3" ,Toast.LENGTH_SHORT).show();
    				} catch (Exception e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
                }
            });
        }
     
        public void getRequest(TextView txtResult, EditText txtUrl){
            String url = txtUrl.getText().toString();
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet(url);
            try{
                HttpResponse response = client.execute(request);
                txtResult.setText(HttpHelper.request(response));
               }catch(Exception ex){
                txtResult.setText("Failed!");
            }
        }
     
        public void  parse(String input) throws Exception {
        	Toast.makeText(this,"209" ,Toast.LENGTH_SHORT).show();
    		 DocumentBuilderFactory dbFactory =	(DocumentBuilderFactory) DocumentBuilderFactory.newInstance();
    		 Toast.makeText(this,"20" ,Toast.LENGTH_SHORT).show();
    		 DocumentBuilder parser =(DocumentBuilder) dbFactory.newDocumentBuilder();
    		 Toast.makeText(this,"21" ,Toast.LENGTH_SHORT).show();
             Document document =(Document) parser.parse(new ByteArrayInputStream(input.getBytes()));
             Toast.makeText(this,"22" ,Toast.LENGTH_SHORT).show();
    		Element root = (Element) document.getDocumentElement();
    		Toast.makeText(this,"23" ,Toast.LENGTH_SHORT).show();
    		NodeList recipesList = (NodeList)root.getChildNodes();
    		Toast.makeText(this,"24" ,Toast.LENGTH_SHORT).show();
    	//	Toast.makeText(this,recipesList.getLength() ,Toast.LENGTH_SHORT).show();
    		for (int i = 0; i < recipesList.getLength(); i++) {
     
    			Toast.makeText(this,"25" ,Toast.LENGTH_SHORT).show();
    			Node recipe =(Node) recipesList.item(i);
     
    			Toast.makeText(this,"26" ,Toast.LENGTH_SHORT).show();
     
    			NodeList fields =(NodeList) recipe.getChildNodes();
    		//	int x=fields.getLength();
     
    			Toast.makeText(this,"27" ,Toast.LENGTH_SHORT).show();
    			// faute hounni 
    			//Toast.makeText(this,x ,Toast.LENGTH_SHORT).show();
     
    			Element hj=(Element) fields.item(0);
    			Toast.makeText(this,"28" ,Toast.LENGTH_SHORT).show();
    			String id = (String) hj.getNodeValue();
     
    			Toast.makeText(this,"29" ,Toast.LENGTH_SHORT).show();
    			String title = ((Element) fields.item(1)).getNodeValue();
    			Toast.makeText(this,"Nadia" ,Toast.LENGTH_SHORT).show();
    			Toast.makeText(this,title ,Toast.LENGTH_SHORT).show();
    		}		
     
     
    	} 
     
     
    }

    il ne dépasse pas la ligne suivante :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    String id = (String) hj.getNodeValue();
    voici l'url :
    http://api.geonames.org/cities?north...&username=demo

    Est ce qu'il y a quelqu'un qui peut m'aider ? et merci d'avance

  2. #2
    Rédacteur
    Avatar de MrDuChnok
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Juin 2002
    Messages
    2 112
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 41
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Produits et services télécom et Internet

    Informations forums :
    Inscription : Juin 2002
    Messages : 2 112
    Par défaut
    Et en utilisant le debugueur, tu vois quoi comme erreur . Des objets vides ?
    Quand tu affiches le résultat de ta requete, ça donne quoi ?


    Et un conseil, à la place des Toasts, utilises les Log ou le debuguer, tu verras que c'est bien plus efficace.

Discussions similaires

  1. [C#] Fonction pour convertir string[] vers ArrayList
    Par luimême dans le forum Windows Forms
    Réponses: 6
    Dernier message: 07/04/2005, 13h25
  2. [String]Convertir pour xml
    Par Pill_S dans le forum Format d'échange (XML, JSON...)
    Réponses: 2
    Dernier message: 05/11/2004, 19h41
  3. [STRING]Contenu d'un String <> vers &lt; et &g
    Par TreizeSegments dans le forum Entrée/Sortie
    Réponses: 4
    Dernier message: 03/08/2004, 08h37
  4. [XSLT] Transformer un String contenant du xml
    Par VinceFromBcn dans le forum Format d'échange (XML, JSON...)
    Réponses: 5
    Dernier message: 07/07/2004, 12h42
  5. [xsl java]Appliquer une XSL à un String contenant doc XML
    Par aldo047 dans le forum XSL/XSLT/XPATH
    Réponses: 4
    Dernier message: 03/04/2003, 15h46

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