bonjour les amis Android,
je suis entrain de développer mon application qui permet de récupérer les données à partir d'un fichier XML se trouvant dans le web.
mon problème : lorsque je parse les données directement de web il prend beaucoup de temps pour afficher les données sur tél.
je besoin d'une solution pour mettre les données dans la base ou en utilisant le service REST ....
voila mon code source
le code source de l'activité SAXParserActivity
le code source de l'activité XMLGettersSetters
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 package com.android.SAXParser; import java.net.URL; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.InputSource; import org.xml.sax.XMLReader; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; public class SAXParserActivity extends Activity { XMLGettersSetters data; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /** * Get the view of the layout within the main layout, so that we can add TextViews. **/ View layout = findViewById(R.id.layout); /** * Create TextView Arrays to add the retrieved data to. **/ TextView title[]; TextView teaser[]; TextView content[]; try { /** * Create a new instance of the SAX parser **/ SAXParserFactory saxPF = SAXParserFactory.newInstance(); SAXParser saxP = saxPF.newSAXParser(); XMLReader xmlR = saxP.getXMLReader(); URL url = new URL("http://www.mosaiquefm.net/smart/newscast.xml?Cat=4"); // URL of the XML /** * Create the Handler to handle each of the XML tags. **/ XMLHandler myXMLHandler = new XMLHandler(); xmlR.setContentHandler(myXMLHandler); xmlR.parse(new InputSource(url.openStream())); } catch (Exception e) { System.out.println(e); } data = XMLHandler.data; /** * Makes the TextView length the size of the TextView arrays by getting the size of the **/ title = new TextView[data.getTitle().size()]; teaser = new TextView[data.getTeaser().size()]; content = new TextView[data.getContent().size()]; /** * Run a for loop to set All the TextViews with text until * the size of the array is reached. * **/ for (int i = 0; i < data.getTitle().size(); i++) { title[i] = new TextView(this); title[i].setText("Title = "+data.getTitle().get(i)); teaser[i] = new TextView(this); teaser[i].setText("Artist = "+data.getTeaser().get(i)); content[i] = new TextView(this); content[i].setText("Country = "+data.getContent().get(i)); ((ViewGroup) layout).addView(title[i]); ((ViewGroup) layout).addView(teaser[i]); ((ViewGroup) layout).addView(content[i]); } setContentView(layout); } }
le code source de l'activité XMLHandler
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 package com.android.SAXParser; import java.util.ArrayList; import android.util.Log; /** * This class contains all getter and setter methods to set and retrieve data. * **/ public class XMLGettersSetters { private ArrayList<String> title = new ArrayList<String>(); private ArrayList<String> teaser = new ArrayList<String>(); private ArrayList<String> content = new ArrayList<String>(); public ArrayList<String> getTitle() { return title; } public void setTitle(String title) { this.title.add(title); Log.i("This is the title:", title); } public ArrayList<String> getTeaser() { return teaser; } public void setTeaser(String teaser) { this.teaser.add(teaser); Log.i("teaserrrrrrrrrr:", teaser); } public ArrayList<String> getContent() { return title; } public void setContent(String content) { this.content.add(content); Log.i("le contenu est :", content); } }
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 package com.android.SAXParser; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class XMLHandler extends DefaultHandler { String elementValue = null; Boolean elementOn = false; public static XMLGettersSetters data = null; public static XMLGettersSetters getXMLData() { return data; } public static void setXMLData(XMLGettersSetters data) { XMLHandler.data = data; } /** * This will be called when the tags of the XML starts. **/ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { elementOn = true; if (localName.equals("articles")) { data = new XMLGettersSetters(); } else if (localName.equals("article")) { /** * We can get the values of attributes for eg. if the CD tag had an attribute( <CD attr= "band">Akon</CD> ) * we can get the value "band". Below is an example of how to achieve this. * * String attributeValue = attributes.getValue("attr"); * data.setAttribute(attributeValue); * * */ } } /** * This will be called when the tags of the XML end. **/ @Override public void endElement(String uri, String localName, String qName) throws SAXException { elementOn = false; /** * Sets the values after retrieving the values from the XML tags * */ if (localName.equalsIgnoreCase("title")) data.setTitle(elementValue); else if (localName.equalsIgnoreCase("content")) data.setContent(elementValue); else if (localName.equalsIgnoreCase("Teaser")) data.setTeaser(elementValue); } /** * This is called to get the tags value **/ @Override public void characters(char[] ch, int start, int length) throws SAXException { if (elementOn) { elementValue = new String(ch, start, length); elementOn = false; } } }
Partager