RSS Reader Problem :/

  • Antworten:1
Max Holzkamp
  • Forum-Beiträge: 4

03.02.2013, 22:04:22 via Website

Guten Abend,
ich habe mal wieder eine Frage undzwar weiß jemand von euch warum der RSS Reader nicht richtig funktioniert? Ich hab mein Problem mal anhand eines Beispiels erklärt:
Zum Beispiel wenn der Titel : "Hallo #8220; ich bin ..." ist, dann zeigt er nur "Hallo #8220;" an...
Ich hab hier mal mein Code gepostet, würde mich über antworten freuen :)

1public class AndroidRssReader extends ListActivity {
2
3 private RSSFeed myRssFeed = null;
4
5 TextView feedTitle;
6 TextView feedDescribtion;
7 TextView feedPubdate;
8 TextView feedLink;
9
10 public class MyCustomAdapter extends ArrayAdapter<RSSItem> {
11
12 public MyCustomAdapter(Context context, int textViewResourceId,
13 List<RSSItem> list) {
14 super(context, textViewResourceId, list);
15 }
16
17 @Override
18 public View getView(int position, View convertView, ViewGroup parent) {
19 // TODO Auto-generated method stub
20 //return super.getView(position, convertView, parent);
21
22 View row = convertView;
23
24 if(row==null){
25 LayoutInflater inflater=getLayoutInflater();
26 row=inflater.inflate(R.layout.row, parent, false);
27 }
28
29 TextView listTitle=(TextView)row.findViewById(R.id.listtitle);
30 listTitle.setText(myRssFeed.getList().get(position).getTitle());
31 TextView listPubdate=(TextView)row.findViewById(R.id.listpubdate);
32 listPubdate.setText(myRssFeed.getList().get(position).getPubdate());
33
34 if (position%2 == 0){
35 listTitle.setBackgroundColor(0xff101010);
36 listPubdate.setBackgroundColor(0xff101010);
37 }
38 else{
39 listTitle.setBackgroundColor(0xff080808);
40 listPubdate.setBackgroundColor(0xff080808);
41 }
42
43 return row;
44 }
45 }
46
47 /** Called when the activity is first created. */
48 @Override
49 public void onCreate(Bundle savedInstanceState) {
50 super.onCreate(savedInstanceState);
51 setContentView(R.layout.main);
52
53 feedTitle = (TextView)findViewById(R.id.feedtitle);
54 feedDescribtion = (TextView)findViewById(R.id.feeddescribtion);
55 feedPubdate = (TextView)findViewById(R.id.feedpubdate);
56 feedLink = (TextView)findViewById(R.id.feedlink);
57
58 readRss();
59 }
60
61 private void readRss(){
62
63 feedTitle.setText("--- wait ---");
64 feedDescribtion.setText("");
65 feedPubdate.setText("");
66 feedLink.setText("");
67 setListAdapter(null);
68
69 Toast.makeText(this, "News werden geladen...", Toast.LENGTH_LONG).show();
70
71 try {
72 URL rssUrl = new URL("meinLink");
73 SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
74 SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
75 XMLReader myXMLReader = mySAXParser.getXMLReader();
76 RSSHandler myRSSHandler = new RSSHandler();
77 myXMLReader.setContentHandler(myRSSHandler);
78 InputSource myInputSource = new InputSource(rssUrl.openStream());
79 myXMLReader.parse(myInputSource);
80
81 myRssFeed = myRSSHandler.getFeed();
82
83 } catch (MalformedURLException e) {
84 // TODO Auto-generated catch block
85 e.printStackTrace();
86 } catch (ParserConfigurationException e) {
87 // TODO Auto-generated catch block
88 e.printStackTrace();
89 } catch (SAXException e) {
90 // TODO Auto-generated catch block
91 e.printStackTrace();
92 } catch (IOException e) {
93 // TODO Auto-generated catch block
94 e.printStackTrace();
95 }
96
97 if (myRssFeed!=null)
98 {
99 Calendar c = Calendar.getInstance();
100 String strCurrentTiime = "\n(Time of Reading - "
101 + c.get(Calendar.HOUR_OF_DAY)
102 + " : "
103 + c.get(Calendar.MINUTE) + ")\n";
104
105 feedTitle.setText(myRssFeed.getTitle() + strCurrentTiime);
106 feedDescribtion.setText(myRssFeed.getDescription());
107 feedPubdate.setText(myRssFeed.getPubdate());
108 feedLink.setText(myRssFeed.getLink());
109
110
111 MyCustomAdapter adapter =
112 new MyCustomAdapter(this, R.layout.row, myRssFeed.getList());
113 setListAdapter(adapter);
114
115 }
116 }
117
118 @Override
119 protected void onListItemClick(ListView l, View v, int position, long id) {
120 // TODO Auto-generated method stub
121 Uri feedUri = Uri.parse(myRssFeed.getItem(position).getLink());
122 Intent myIntent = new Intent(Intent.ACTION_VIEW, feedUri);
123 startActivity(myIntent);
124 }
125
126 @Override
127 public boolean onCreateOptionsMenu(Menu menu) {
128 // TODO Auto-generated method stub
129 menu.add(0, 0, 0, "Refresh");
130 return true;
131 }
132
133 @Override
134 public boolean onOptionsItemSelected(MenuItem item) {
135 // TODO Auto-generated method stub
136 switch(item.getItemId()){
137 case (0): readRss();
138 break;
139 default:
140 break;
141 }
142
143 return true;
144 }
145
146
147}

1public class RSSFeed {
2 private String title = null;
3 private String description = null;
4 private String link = null;
5 private String pubdate = null;
6 private List<RSSItem> itemList;
7
8 RSSFeed(){
9 itemList = new Vector<RSSItem>(0);
10 }
11
12 void addItem(RSSItem item){
13 itemList.add(item);
14 }
15
16 RSSItem getItem(int location){
17 return itemList.get(location);
18 }
19
20 List<RSSItem> getList(){
21 return itemList;
22 }
23
24 void setTitle(String value)
25 {
26 title = value;
27 }
28 void setDescription(String value)
29 {
30 description = value;
31 }
32 void setLink(String value)
33 {
34 link = value;
35 }
36 void setPubdate(String value)
37 {
38 pubdate = value;
39 }
40
41 String getTitle()
42 {
43 return title;
44 }
45 String getDescription()
46 {
47 return description;
48 }
49 String getLink()
50 {
51 return link;
52 }
53 String getPubdate()
54 {
55 return pubdate;
56 }
57
58}

1public class RSSHandler extends DefaultHandler {
2
3 final int state_unknown = 0;
4 final int state_title = 1;
5 final int state_description = 2;
6 final int state_link = 3;
7 final int state_pubdate = 4;
8 int currentState = state_unknown;
9
10 RSSFeed feed;
11 RSSItem item;
12
13 boolean itemFound = false;
14
15 RSSHandler(){
16 }
17
18 RSSFeed getFeed(){
19 return feed;
20 }
21
22 @Override
23 public void startDocument() throws SAXException {
24 // TODO Auto-generated method stub
25 feed = new RSSFeed();
26 item = new RSSItem();
27
28 }
29
30 @Override
31 public void endDocument() throws SAXException {
32 // TODO Auto-generated method stub
33 }
34
35 @Override
36 public void startElement(String uri, String localName, String qName,
37 Attributes attributes) throws SAXException {
38 // TODO Auto-generated method stub
39
40 if (localName.equalsIgnoreCase("item")){
41 itemFound = true;
42 item = new RSSItem();
43 currentState = state_unknown;
44 }
45 else if (localName.equalsIgnoreCase("title")){
46 currentState = state_title;
47 }
48 else if (localName.equalsIgnoreCase("description")){
49 currentState = state_description;
50 }
51 else if (localName.equalsIgnoreCase("link")){
52 currentState = state_link;
53 }
54 else if (localName.equalsIgnoreCase("pubdate")){
55 currentState = state_pubdate;
56 }
57 else{
58 currentState = state_unknown;
59 }
60
61 }
62
63 @Override
64 public void endElement(String uri, String localName, String qName)
65 throws SAXException {
66 // TODO Auto-generated method stub
67 if (localName.equalsIgnoreCase("item")){
68 feed.addItem(item);
69 }
70 }
71
72 @Override
73 public void characters(char[] ch, int start, int length)
74 throws SAXException {
75 // TODO Auto-generated method stub
76
77 String strCharacters = new String(ch,start,length);
78
79 if (itemFound==true){
80 // "item" tag found, it's item's parameter
81 switch(currentState){
82 case state_title:
83 item.setTitle(strCharacters);
84 break;
85 case state_description:
86 item.setDescription(strCharacters);
87 break;
88 case state_link:
89 item.setLink(strCharacters);
90 break;
91 case state_pubdate:
92 item.setPubdate(strCharacters);
93 break;
94 default:
95 break;
96 }
97 }
98 else{
99 // not "item" tag found, it's feed's parameter
100 switch(currentState){
101 case state_title:
102 feed.setTitle(strCharacters);
103 break;
104 case state_description:
105 feed.setDescription(strCharacters);
106 break;
107 case state_link:
108 feed.setLink(strCharacters);
109 break;
110 case state_pubdate:
111 feed.setPubdate(strCharacters);
112 break;
113 default:
114 break;
115 }
116 }
117
118 currentState = state_unknown;
119 }
120
121
122}

1public class RSSItem {
2
3 private String title = null;
4 private String description = null;
5 private String link = null;
6 private String pubdate = null;
7
8 RSSItem(){
9 }
10
11 void setTitle(String value)
12 {
13 title =value;
14 }
15 void setDescription(String value)
16 {
17 description = value;
18 }
19 void setLink(String value)
20 {
21 link = value;
22 }
23 void setPubdate(String value)
24 {
25 pubdate = value;
26 }
27
28 String getTitle()
29 {
30 return title;
31 }
32 String getDescription()
33 {
34 return description;
35 }
36 String getLink()
37 {
38 return link;
39 }
40 String getPubdate()
41 {
42 return pubdate;
43 }
44
45 /*@Override
46 public String toString() {
47 // TODO Auto-generated method stub
48 return title;
49 }*/
50
51
52
53}

— geändert am 03.02.2013, 22:05:29

Antworten
Max Holzkamp
  • Forum-Beiträge: 4

05.02.2013, 19:44:19 via Website

Hat niemand eine Idee?
Hab jetzt schon myInputSource.setEncoding("ISO-8859-1"); probiert, geht aber auch nicht...

Antworten