隔离RSS Feed中的链接

问题描述:

我是编程新手,所以请在我身上轻松一下,我一直在用一个简单的RSS阅读器搞砸,试图获得artice的链接,以在用户点击时在webview中打开文章。隔离RSS Feed中的链接

我找到了控制和存储链接的字符串,但是当我尝试打印链接时,链接出现了,但整篇文章的发布日期等...我如何获得打印链接自己做什么,命令我需要使用通过链接到网页视图一旦我已经分离了,这里是一些代码我有

RSSActivity

public class RssActivity extends ListActivity { 
    private RssListAdapter adapter; 

     /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     List<JSONObject> jobs = new ArrayList<JSONObject>(); 
     try { 
      jobs = RssReader.getLatestRssFeed(); 
     } catch (Exception e) { 
      Log.e("RSS ERROR", "Error loading RSS Feed Stream >> " + e.getMessage() + " //" + e.toString()); 
     } 

     adapter = new RssListAdapter(this,jobs); 
     setListAdapter(adapter); 
    } 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     // Get the item that was clicked 
     Object o = this.getListAdapter().getItem(position); 
     adapter.getItem(position).toString(); 
     String link = o.toString(); 


     Toast.makeText(this, "You selected: " + link, Toast.LENGTH_LONG) 
       .show(); 


    } 

} 

Article.class

public class Article { 

    private long articleId; 
    private long feedId; 
    private String title; 
    private String description; 
    private String pubDate; 
    private URL url; 
    private String encodedContent; 
    private String link; 


    public void setArticleId(long articleId) { 
     this.articleId = articleId; 
    } 
    /** 
    * @return the feedId 
    */ 
    public long getFeedId() { 
     return feedId; 
    } 
    /** 
    * @param feedId the feedId to set 
    */ 
    public void setFeedId(long feedId) { 
     this.feedId = feedId; 
    } 
    public String getLink() { 
     return link; 
    } 
    /** 
    * @param title the title to set 
    */ 
    public void setLink(String link) { 
     this.link = link; 
    } 
    /** 
    * @return the title 
    */ 
    public String getTitle() { 
     return title; 
    } 
    /** 
    * @param title the title to set 
    */ 
    public void setTitle(String title) { 
     this.title = title; 
    } 
    /** 
    * @return the url 
    */ 
    public URL getUrl() { 
     return url; 
    } 
    /** 
    * @param url the url to set 
    */ 
    public void setUrl(URL url) { 
     this.url = url; 
    } 
    /** 
    * @param description the description to set 
    */ 
    public void setDescription(String description) { 
     this.description = description; 

     //parse description for any image or video links 
     if (description.contains("<img ")){ 
      String img = description.substring(description.indexOf("<img ")); 
      String cleanUp = img.substring(0, img.indexOf(">")+1); 

      int indexOf = img.indexOf("'"); 
      if (indexOf==-1){ 

      } 


      this.description = this.description.replace(cleanUp, ""); 
     } 
    } 
    /** 
    * @return the description 
    */ 
    public String getDescription() { 
     return description; 
    } 
    /** 
    * @param pubDate the pubDate to set 
    */ 
    public void setPubDate(String pubDate) { 
     this.pubDate = pubDate; 
    } 
    /** 
    * @return the pubDate 
    */ 
    public String getPubDate() { 
     return pubDate; 
    } 
    /** 
    * @param encodedContent the encodedContent to set 
    */ 
    public void setEncodedContent(String encodedContent) { 
     this.encodedContent = encodedContent; 
    } 
    /** 
    * @return the encodedContent 
    */ 
    public String getEncodedContent() { 
     return encodedContent; 
    } 
} 

RSS处理器

public class RSSHandler extends DefaultHandler { 

    // Feed and Article objects to use for temporary storage 
    private Article currentArticle = new Article(); 
    private List<Article> articleList = new ArrayList<Article>(); 

    // Number of articles added so far 
    private int articlesAdded = 0; 

    // Number of articles to download 
    private static final int ARTICLES_LIMIT = 15; 

    //Current characters being accumulated 
    StringBuffer chars = new StringBuffer();  

    public void startElement(String uri, String localName, String qName, Attributes atts) { 
     chars = new StringBuffer(); 
    } 

    public void endElement(String uri, String localName, String qName) throws SAXException { 

     if (localName.equalsIgnoreCase("title")) 
     { 
      Log.d("LOGGING RSS XML", "Setting article title: " + chars.toString()); 
      currentArticle.setTitle(chars.toString()); 

     } 
     else if (localName.equalsIgnoreCase("description")) 
     { 
      Log.d("LOGGING RSS XML", "Setting article description: " + chars.toString()); 
      currentArticle.setDescription(chars.toString()); 
     } 
     else if (localName.equalsIgnoreCase("pubDate")) 
     { 
      Log.d("LOGGING RSS XML", "Setting article published date: " + chars.toString()); 
      currentArticle.setPubDate(chars.toString()); 
     } 
     else if (localName.equalsIgnoreCase("encoded")) 
     { 
      Log.d("LOGGING RSS XML", "Setting article content: " + chars.toString()); 
      currentArticle.setEncodedContent(chars.toString()); 
     } 
     else if (localName.equalsIgnoreCase("item")) 
     { 

     } 
     else if (localName.equalsIgnoreCase("link")) 
     { 
      Log.d("LOGGING RSS XML", "Setting article link: " + chars.toString()); 
      currentArticle.setLink(chars.toString()); 
      try { 
       Log.d("LOGGING RSS XML", "Setting article link url: " + chars.toString()); 
       currentArticle.setUrl(new URL(chars.toString())); 
      } catch (MalformedURLException e) { 
       Log.e("RSA Error", e.getMessage()); 
      } 
     } 

     // Check if looking for article, and if article is complete 
     if (localName.equalsIgnoreCase("item")) { 

      articleList.add(currentArticle); 

      currentArticle = new Article(); 

      // Lets check if we've hit our limit on number of articles 
      articlesAdded++; 
      if (articlesAdded >= ARTICLES_LIMIT) 
      { 
       throw new SAXException(); 
      } 
     } 
    } 

    public void characters(char ch[], int start, int length) { 
     chars.append(new String(ch, start, length)); 
    } 

    public List<Article> getLatestArticles(String feedUrl) { 
     URL url = null; 
     try { 

      SAXParserFactory spf = SAXParserFactory.newInstance(); 
      SAXParser sp = spf.newSAXParser(); 
      XMLReader xr = sp.getXMLReader(); 

      url = new URL(feedUrl); 

      xr.setContentHandler(this); 
      xr.parse(new InputSource(url.openStream())); 


     } catch (IOException e) { 
      Log.e("RSS Handler IO", e.getMessage() + " >> " + e.toString()); 
     } catch (SAXException e) { 
      Log.e("RSS Handler SAX", e.toString()); 
     } catch (ParserConfigurationException e) { 
      Log.e("RSS Handler Parser Config", e.toString()); 
     } 

     return articleList; 
    } 

} 

public class RssReader { 

    private final static String BOLD_OPEN = "<B>"; 
    private final static String BOLD_CLOSE = "</B>"; 
    private final static String BREAK = "<BR>"; 
    private final static String ITALIC_OPEN = "<I>"; 
    private final static String ITALIC_CLOSE = "</I>"; 
    private final static String SMALL_OPEN = "<SMALL>"; 
    private final static String SMALL_CLOSE = "</SMALL>"; 
    private final static String WEB_LINK = "<A>"; 
    private final static String WEB_CLOSE = "<A/"; 

    public static List<JSONObject> getLatestRssFeed(){ 
     String feed = "http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/eng_prem/rss.xml"; 

     RSSHandler rh = new RSSHandler(); 
     List<Article> articles = rh.getLatestArticles(feed); 
     Log.e("RSS ERROR", "Number of articles " + articles.size()); 
     return fillData(articles); 
    } 


    private static List<JSONObject> fillData(List<Article> articles) { 

     List<JSONObject> items = new ArrayList<JSONObject>(); 
     for (Article article : articles) { 
      JSONObject current = new JSONObject(); 
      try { 
       buildJsonObject(article, current); 
      } catch (JSONException e) { 
       Log.e("RSS ERROR", "Error creating JSON Object from RSS feed"); 
      } 
      items.add(current); 
     } 

     return items; 
    } 

    private static void buildJsonObject(Article article, JSONObject current) throws JSONException { 
     String link = article.getLink(); 
     String title = article.getTitle(); 
     String description = article.getDescription(); 
     String date = article.getPubDate(); 


     StringBuffer sb = new StringBuffer(); 
     sb.append(BOLD_OPEN).append(title).append(BOLD_CLOSE); 
     sb.append(BREAK); 
     sb.append(description); 
     sb.append(BREAK); 
     sb.append(SMALL_OPEN).append(ITALIC_OPEN).append(date).append(ITALIC_CLOSE).append(SMALL_CLOSE); 
     sb.append(BREAK); 
     sb.append(BREAK); 
     sb.append(BOLD_OPEN).append(WEB_LINK).append(link).append(BOLD_CLOSE).append(WEB_CLOSE); 
     current.put("link", link); 

     current.put("text", Html.fromHtml(sb.toString())); 

    } 
} 

RssListAdapter

import java.util.List; 

import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.text.Spanned; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.TextView; 

public class RssListAdapter extends ArrayAdapter<JSONObject> { 

    public RssListAdapter(Activity activity, List<JSONObject> imageAndTexts) { 
     super(activity, 0, imageAndTexts); 
    } 


    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     Activity activity = (Activity) getContext(); 
     LayoutInflater inflater = activity.getLayoutInflater(); 

     // Inflate the views from XML 
     View rowView = inflater.inflate(R.layout.image_text_layout, null); 
     JSONObject jsonImageText = getItem(position); 

     ////////////////////////////////////////////////////////////////////////////////////////////////////// 
     //The next section we update at runtime the text - as provided by the JSON from our REST call 
     //////////////////////////////////////////////////////////////////////////////////////////////////// 
     TextView textView = (TextView) rowView.findViewById(R.id.job_text); 

     try { 
      Spanned text = (Spanned)jsonImageText.get("text"); 
      textView.setText(text); 

     } catch (JSONException e) { 
      textView.setText("JSON Exception"); 
     } 

     return rowView; 

    } 

} 

试试这个

protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     // Get the item that was clicked 
     JSONObject o = (JSONObject) this.getListAdapter().getItem(position); 
     adapter.getItem(position).toString(); 
     String link = o.getString("NameOfLinkInJsonObject"); 


     Toast.makeText(this, "You selected: " + link, Toast.LENGTH_LONG) 
       .show(); 

     mMyWebView.loadUrl(link); 
    } 

请注意,您需要修复 “NameOfLinkInJsonObject”。

在您的活动添加一个字段像这样

private WebView mMyWebView; 

在你onCreate方法添加

mMyWebView = (WebView) findViewById(R.id.webViewId); 

您必须将R.id.webViewId更改为相应的ID为您的网站视图。

+0

感谢您的回复,在行JSONObeject o = this.getlistAdapter我得到的错误无法从对象转换为JSONObject在webview方法我得到不能静态引用非静态webview.loadurl(链接); – JonniBravo 2011-06-07 07:44:04

+0

我编辑我的答案是更确切一点。我的第一个剪辑不是为了复制粘贴。请记住,您需要将NameOfLinkInJsonObject更改为包含该链接的JSON对象中的字段名称。 – 2011-06-07 14:50:13

+0

谢谢贾斯汀我相信我将能够得到这个工作我很感激 – JonniBravo 2011-06-08 16:10:25

Open URL in default browser Open URL with Android

如果你有一个点的URL或URI对象,可能是在链接的一个解释上面会得到你在你的路上。

- 编辑:

protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    // Get the item that was clicked 
    Object o = this.getListAdapter().getItem(position); 
    adapter.getItem(position).toString(); 
    String link = o.toString(); 
    Toast.makeText(this, "You selected: " + link, Toast.LENGTH_LONG) 
      .show(); 
} 

猜测这是该代码显示的链接,但如果你没有提供给我们什么getListAdapter()方法等做的话,我恐怕很难帮助你出去。

+0

我有我自己的webview,我想打开其中的链接,所以这些意图不适合我的需求,如何获得链接?我应该使用捆绑通过URL或put.extras getextras等..... – JonniBravo 2011-05-27 14:25:32

+0

许多道歉,我已经提供了上面的RSSListAdapter类,这是RSS阅读器的完整代码 – JonniBravo 2011-05-27 15:59:18

+0

我建议你试着找出你自己在'Object o = this.getListAdapter()。getItem(position)'这一行得到什么样的对象(哪个类)?'显然它有一个toString()实现来显示整个RSS提要内容。可能有一种方法来'getUrl()'或任何东西.. – Wivani 2011-05-28 13:47:41