0

RSS 阅读器不会获取诸如“#8220;”之类的 HTML 字符。所以当提要是“Hey#8220;”时 该应用程序只显示“嘿”,但我不知道为什么。我尝试了 Html.fromHtml 之类的东西,但它确实不起作用。有没有人看到错误?感谢您的回答

`公共类 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;
}*/

}

4

1 回答 1

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

为您的输入源或流设置编码。此示例代码可以帮助您。

于 2013-02-06T09:11:59.443 回答