2013-02-04 43 views
0

RSS閱讀器發送HTML字符,如「#8220;」所以當飼料是「嘿#8220」時該應用程序只顯示「嘿」但我知道爲什麼。我嘗試了諸如Html.fromHtml之類的東西,但它確實沒有工作。有沒有人看到錯誤? 感謝答案RSS閱讀器發送HTML字符

'公共類AndroidRssReader擴展ListActivity {

private RSSFeed myRssFeed = null; 

TextView feedTitle; 
TextView feedDescribtion; 
TextView feedPubdate; 
TextView feedLink; 

public class MyCustomAdapter extends ArrayAdapter<RSSItem> { 

    public MyCustomAdapter(Context context, int textViewResourceId, 
      List<RSSItem> list) { 
     super(context, textViewResourceId, list); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     //return super.getView(position, convertView, parent); 

     View row = convertView; 

     if(row==null){ 
      LayoutInflater inflater=getLayoutInflater(); 
      row=inflater.inflate(R.layout.row, parent, false); 
     } 

     TextView listTitle=(TextView)row.findViewById(R.id.listtitle); 
     listTitle.setText(myRssFeed.getList().get(position).getTitle()); 
     TextView listPubdate=(TextView)row.findViewById(R.id.listpubdate); 
     listPubdate.setText(myRssFeed.getList().get(position).getPubdate()); 

     if (position%2 == 0){ 
      listTitle.setBackgroundColor(0xff101010); 
      listPubdate.setBackgroundColor(0xff101010); 
     } 
     else{ 
      listTitle.setBackgroundColor(0xff080808); 
      listPubdate.setBackgroundColor(0xff080808); 
     } 

     return row; 
    } 
} 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    feedTitle = (TextView)findViewById(R.id.feedtitle); 
    feedDescribtion = (TextView)findViewById(R.id.feeddescribtion); 
    feedPubdate = (TextView)findViewById(R.id.feedpubdate); 
    feedLink = (TextView)findViewById(R.id.feedlink); 

    readRss(); 
} 

private void readRss(){ 

    feedTitle.setText("--- wait ---"); 
    feedDescribtion.setText(""); 
    feedPubdate.setText(""); 
    feedLink.setText(""); 
    setListAdapter(null); 

    Toast.makeText(this, "News werden geladen...", Toast.LENGTH_LONG).show(); 

    try { 
     URL rssUrl = new URL("MYURL"); 
     SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance(); 
     SAXParser mySAXParser = mySAXParserFactory.newSAXParser(); 
     XMLReader myXMLReader = mySAXParser.getXMLReader(); 
     RSSHandler myRSSHandler = new RSSHandler(); 
     myXMLReader.setContentHandler(myRSSHandler); 
     InputSource myInputSource = new InputSource(rssUrl.openStream()); 
     myXMLReader.parse(myInputSource); 

     myRssFeed = myRSSHandler.getFeed(); 

    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ParserConfigurationException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SAXException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    if (myRssFeed!=null) 
    { 
     Calendar c = Calendar.getInstance(); 
     String strCurrentTiime = "\n(Time of Reading - " 
           + c.get(Calendar.HOUR_OF_DAY) 
           + " : " 
           + c.get(Calendar.MINUTE) + ")\n"; 

     feedTitle.setText(myRssFeed.getTitle() + strCurrentTiime); 
     feedDescribtion.setText(myRssFeed.getDescription()); 
     feedPubdate.setText(myRssFeed.getPubdate()); 
     feedLink.setText(myRssFeed.getLink()); 


     MyCustomAdapter adapter = 
      new MyCustomAdapter(this, R.layout.row, myRssFeed.getList()); 
     setListAdapter(adapter); 

    } 
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    /*Intent intent = new Intent(this,ShowDetails.class); 
    Bundle bundle = new Bundle(); 
    bundle.putString("keyTitle", myRssFeed.getItem(position).getTitle()); 
    bundle.putString("keyDescription", myRssFeed.getItem(position).getDescription()); 
    bundle.putString("keyLink", myRssFeed.getItem(position).getLink()); 
    bundle.putString("keyPubdate", myRssFeed.getItem(position).getPubdate()); 
    intent.putExtras(bundle); 
    startActivity(intent);*/ 
    Uri feedUri = Uri.parse(myRssFeed.getItem(position).getLink()); 
    Intent myIntent = new Intent(Intent.ACTION_VIEW, feedUri); 
    startActivity(myIntent); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // TODO Auto-generated method stub 
    menu.add(0, 0, 0, "Refresh"); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // TODO Auto-generated method stub 
    switch(item.getItemId()){ 
    case (0): readRss(); 
      break; 
    default: 
      break; 
    } 

    return true; 
} 

}`

public class RSSFeed { 
private String title = null; 
private String description = null; 
private String link = null; 
private String pubdate = null; 
private List<RSSItem> itemList; 

RSSFeed(){ 
    itemList = new Vector<RSSItem>(0); 
} 

void addItem(RSSItem item){ 
    itemList.add(item); 
} 

RSSItem getItem(int location){ 
    return itemList.get(location); 
} 

List<RSSItem> getList(){ 
    return itemList; 
} 

void setTitle(String value) 
{ 
    title = value; 
} 
void setDescription(String value) 
{ 
    description = value; 
} 
void setLink(String value) 
{ 
    link = value; 
} 
void setPubdate(String value) 
{ 
    pubdate = value; 
} 

String getTitle() 
{ 
    return title; 
} 
String getDescription() 
{ 
    return description; 
} 
String getLink() 
{ 
    return link; 
} 
String getPubdate() 
{ 
    return pubdate; 
} 

}

公共類RSSHandler擴展了DefaultHandler {

final int state_unknown = 0; 
final int state_title = 1; 
final int state_description = 2; 
final int state_link = 3; 
final int state_pubdate = 4; 
int currentState = state_unknown; 

RSSFeed feed; 
RSSItem item; 

boolean itemFound = false; 

RSSHandler(){ 
} 

RSSFeed getFeed(){ 
    return feed; 
} 

@Override 
public void startDocument() throws SAXException { 
    // TODO Auto-generated method stub 
    feed = new RSSFeed(); 
    item = new RSSItem(); 

} 

@Override 
public void endDocument() throws SAXException { 
    // TODO Auto-generated method stub 
} 

@Override 
public void startElement(String uri, String localName, String qName, 
     Attributes attributes) throws SAXException { 
    // TODO Auto-generated method stub 

    if (localName.equalsIgnoreCase("item")){ 
     itemFound = true; 
     item = new RSSItem(); 
     currentState = state_unknown; 
    } 
    else if (localName.equalsIgnoreCase("title")){ 
     currentState = state_title; 
    } 
    else if (localName.equalsIgnoreCase("description")){ 
     currentState = state_description; 
    } 
    else if (localName.equalsIgnoreCase("link")){ 
     currentState = state_link; 
    } 
    else if (localName.equalsIgnoreCase("pubdate")){ 
     currentState = state_pubdate; 
    } 
    else{ 
     currentState = state_unknown; 
    } 

} 

@Override 
public void endElement(String uri, String localName, String qName) 
     throws SAXException { 
    // TODO Auto-generated method stub 
    if (localName.equalsIgnoreCase("item")){ 
     feed.addItem(item); 
    } 
} 

@Override 
public void characters(char[] ch, int start, int length) 
     throws SAXException { 
    // TODO Auto-generated method stub 

    String strCharacters = new String(ch,start,length); 

    if (itemFound==true){ 
    // "item" tag found, it's item's parameter 
     switch(currentState){ 
     case state_title: 
      item.setTitle(strCharacters); 
      break; 
     case state_description: 
      item.setDescription(strCharacters); 
      break; 
     case state_link: 
      item.setLink(strCharacters); 
      break; 
     case state_pubdate: 
      item.setPubdate(strCharacters); 
      break; 
     default: 
      break; 
     } 
    } 
    else{ 
    // not "item" tag found, it's feed's parameter 
     switch(currentState){ 
     case state_title: 
      feed.setTitle(strCharacters); 
      break; 
     case state_description: 
      feed.setDescription(strCharacters); 
      break; 
     case state_link: 
      feed.setLink(strCharacters); 
      break; 
     case state_pubdate: 
      feed.setPubdate(strCharacters); 
      break; 
     default: 
      break; 
     } 
    } 

    currentState = state_unknown; 
} 

}

public class RSSItem { 

private String title = null; 
private String description = null; 
private String link = null; 
private String pubdate = null; 

RSSItem(){ 
} 

void setTitle(String value) 
{ 
    value.replace("#8220;", "hi"); 
    title =value; 
} 
void setDescription(String value) 
{ 
    description = value; 
} 
void setLink(String value) 
{ 
    link = value; 
} 
void setPubdate(String value) 
{ 
    pubdate = value; 
} 

String getTitle() 
{ 
    return title; 
} 
String getDescription() 
{ 
    return description; 
} 
String getLink() 
{ 
    return link; 
} 
String getPubdate() 
{ 
    return pubdate; 
} 

/*@Override 
public String toString() { 
    // TODO Auto-generated method stub 
    return title; 
}*/ 

}

+0

更改編碼可以幫助你。嘗試不同的字符編碼。 – Tugrul

+0

RSS Feed編碼? –

+0

解析器編碼。 – Tugrul

回答

0
InputSource myInputSource = new InputSource(rssUrl.openStream()); 
     myXMLReader.parse(myInputSource); 

設置一個編碼輸入源或流。 This sample cod e可以幫助你。