Bonjour, je suis actuellement en train de faire une application android pour afficher le flux RSS d'un site web mais je suis bloquée. L'application s'ouvre mais rien ne s'affiche. Je cherche la solution depuis deux semaines donc si quelqu'un voulais bien me donner au moins une piste
Merci d'avance
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152 package com.theopentutorials.XMLParse; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.List; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import com.theopentutorials.XMLParse.beans.FluxRSS; import com.theopentutorials.XMLParse.xml.XMLDOMParser; import com.theopentutorials.android.R; import com.theopentutorials.XMLParse.adapters.CustomListViewAdapter; import android.app.Activity; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.Toast; public class DOMParserActivity extends Activity implements OnClickListener,OnItemClickListener { ListView listView; Button button; List<FluxRSS> fluxRSSes; CustomListViewAdapter listViewAdapter; // All static variables static final String URL = "https://www.androidpit.com/feed/main.xml"; // XML node names static final String NODE_LINK = "link"; static final String NODE_TITLE = "title"; static final String NODE_DESC = "description"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewsById(); button.setOnClickListener(this); listView.setOnItemClickListener(this); } private void findViewsById() { button = (Button) findViewById(R.id.button); listView = (ListView) findViewById(R.id.RSSList); } public void onClick(View view) { GetXMLTask task = new GetXMLTask(this); task.execute(new String[] { URL }); } public void onItemClick(AdapterView<?> adapter, View view, int position, long id) { Toast.makeText(getApplicationContext(), fluxRSSes.get(position).toString(), Toast.LENGTH_SHORT).show(); } //private inner class extending AsyncTask private class GetXMLTask extends AsyncTask<String, Void, String> { private Activity context; public GetXMLTask(Activity context) { this.context = context; } @Override protected String doInBackground(String... urls) { String xml = null; for (String url : urls) { xml = getXmlFromUrl(url); } return xml; } @Override protected void onPostExecute(String xml) { XMLDOMParser parser = new XMLDOMParser(); InputStream stream = new ByteArrayInputStream(xml.getBytes()); Document doc = parser.getDocument(stream); NodeList nodeList = doc.getElementsByTagName(NODE_TITLE); fluxRSSes = new ArrayList<FluxRSS>(); FluxRSS fluxRSS = null; for (int i = 0; i < nodeList.getLength(); i++) { fluxRSS = new FluxRSS(); Element e = (Element) nodeList.item(i); fluxRSS.setTitle(parser.getValue(e, NODE_TITLE)); fluxRSS.setDescription(parser.getValue(e, NODE_DESC)); fluxRSS.setLink(parser.getValue(e, NODE_LINK)); fluxRSSes.add(fluxRSS); } listViewAdapter = new CustomListViewAdapter(context, fluxRSSes); listView.setAdapter(listViewAdapter); } /* uses HttpURLConnection to make Http request from Android to download the XML file */ private String getXmlFromUrl(String urlString) { StringBuffer output = new StringBuffer(""); InputStream stream = null; URL url; try { url = new URL(urlString); URLConnection connection = url.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection) connection; httpConnection.setRequestMethod("GET"); httpConnection.connect(); if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { stream = httpConnection.getInputStream(); BufferedReader buffer = new BufferedReader( new InputStreamReader(stream)); String s = ""; while ((s = buffer.readLine()) != null) output.append(s); } } catch (MalformedURLException e) { Log.e("Error", "Unable to parse URL", e); } catch (IOException e) { Log.e("Error", "IO Exception", e); } return output.toString(); } } }
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 package com.theopentutorials.XMLParse.adapters; import java.util.List; import com.theopentutorials.XMLParse.beans.FluxRSS; import com.theopentutorials.android.R; import android.app.Activity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class CustomListViewAdapter extends BaseAdapter { Activity context; List<FluxRSS> fluxRSSes; public CustomListViewAdapter(Activity context, List<FluxRSS> fluxRSSes) { this.context = context; this.fluxRSSes = fluxRSSes; } // private View Holder class private class ViewHolder { TextView txtTitle; TextView txtDescription; TextView txtLink; } public int getCount() { return fluxRSSes.size(); } public Object getItem(int position) { return fluxRSSes.get(position); } public long getItemId(int position) { return fluxRSSes.indexOf(getItem(position)); } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; LayoutInflater inflater = context.getLayoutInflater(); if (convertView == null) { convertView = inflater.inflate(R.layout.list_item, null); holder = new ViewHolder(); holder.txtTitle = (TextView) convertView.findViewById(R.id.title); holder.txtDescription = (TextView) convertView.findViewById(R.id.description); holder.txtLink = (TextView) convertView.findViewById(R.id.link); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } FluxRSS fluxRSS = (FluxRSS) getItem(position); holder.txtTitle.setText(fluxRSS.getTitle() + ": " ); holder.txtDescription.setText(fluxRSS.getDescription()); holder.txtLink.setText(fluxRSS.getLink()); return convertView; } }
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 package com.theopentutorials.XMLParse.xml; import java.io.IOException; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import android.util.Log; public class XMLDOMParser { // Returns the entire XML document public Document getDocument(InputStream inputStream) { Document document = null; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(inputStream); document = db.parse(inputSource); } catch (ParserConfigurationException e) { Log.e("Error: ", e.getMessage(), e); return null; } catch (SAXException e) { Log.e("Error: ", e.getMessage(), e); return null; } catch (IOException e) { Log.e("Error: ", e.getMessage(), e); return null; } return document; } /* * I take a XML element and the tag name, look for the tag and get the text * content i.e for <employee><name>Kumar</name></employee> XML snippet if * the Element points to employee node and tagName is name I will return * Kumar. Calls the private method getTextNodeValue(node) which returns the * text value, say in our example it is Kumar. */ public String getValue(Element item, String title) { NodeList nodes = item.getElementsByTagName(title); return this.getTextNodeValue(nodes.item(0)); } private final String getTextNodeValue(Node node) { Node child; if (node != null) { if (node.hasChildNodes()) { child = node.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.TEXT_NODE) { return child.getNodeValue(); } child = child.getNextSibling(); } } } return ""; } }
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 package com.theopentutorials.XMLParse.beans; public class FluxRSS { private String title; private String link; private String description; public String getLink() { return link; } public void setLink(String link) { this.link = link; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return title + ": " + description + "\n" + link; } }![]()
Partager