2015-06-27 25 views
1

我有兩個爲Android RSS閱讀器設置的類文件。從另一個班級訪問最終變量並正確輸出結果

而不是在下面的GetNetworkRequest中獲取public static class FeedResponsepublic static class ItemResponse中的信息的獲取者。我使用了公共的最終字符串變量。

似乎我無法弄清楚是如何得到那些最終變量的信息,他們DataSource設置爲feedsitems有效地取代我createFakeData方法。

我已驗證來自網絡請求的所有信息都已加載到FeedReponseItemResponse列表中。

如何訪問這些列表並在DataSource中設置它們?

數據源

package io.bloc.android.blocly.api; 

import java.util.ArrayList; 
import java.util.List; 

import io.bloc.android.blocly.BloclyApplication; 
import io.bloc.android.blocly.R; 
import io.bloc.android.blocly.api.model.RssFeed; 
import io.bloc.android.blocly.api.model.RssItem; 
import io.bloc.android.blocly.api.network.GetFeedsNetworkRequest; 
import io.bloc.android.blocly.api.network.NetworkRequest; 

import static io.bloc.android.blocly.api.network.GetFeedsNetworkRequest.*; 

public class DataSource extends GetFeedsNetworkRequest { 

    public List<RssFeed> feeds; 
    public List<RssItem> items; 

    public DataSource() { 
     feeds = new ArrayList<RssFeed>(); 
     items = new ArrayList<RssItem>(); 

     createFakeData(); 

     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       new GetFeedsNetworkRequest("http://feeds.feedburner.com/androidcentral?format=xml").performRequest(); 
      } 
     }).start(); 

    } 

    public List<RssFeed> getFeeds() { 
     return feeds; 
    } 

    public List<RssItem> getItems() { 
     return items; 
    } 

    void createFakeData(){ 

     feeds.add(new RssFeed("My Favorite Feed!", 
       "This feed is just incredible, I can't even begin to tell you…", 
       "http://favoritefeed.net", "http://feeds.feedburner.com/favorite_feed?format=xml")); 
     for (int i = 0; i < 10; i++) { 
      items.add(new RssItem(String.valueOf(i), 
        BloclyApplication.getSharedInstance().getString(R.string.placeholder_headline) + " " + i, 
        BloclyApplication.getSharedInstance().getString(R.string.placeholder_content), 
        "http://favoritefeed.net?story_id=an-incredible-news-story", 
        "http://rs1img.memecdn.com/silly-dog_o_511213.jpg", 
        0, System.currentTimeMillis(), false, false)); 
     } 
    } 
} 

GetNetworkRequest

package io.bloc.android.blocly.api.network; 

import android.provider.DocumentsContract; 
import android.util.Log; 

import org.w3c.dom.Document; 
import org.w3c.dom.NamedNodeMap; 
import org.w3c.dom.Node; 
import org.w3c.dom.NodeList; 
import org.xml.sax.SAXException; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 
import javax.xml.parsers.ParserConfigurationException; 


public class GetFeedsNetworkRequest extends NetworkRequest<List<GetFeedsNetworkRequest.FeedResponse>> { 

    public static final int ERROR_PARSING = 3; 

    private static final String XML_TAG_TILE = "title"; 
    private static final String XML_TAG_DESCRIPTION = "description"; 
    private static final String XML_TAG_LINK = "link"; 
    private static final String XML_TAG_ITEM = "item"; 
    private static final String XML_TAG_PUB_DATE = "pubDate"; 
    private static final String XML_TAG_GUID = "guid"; 
    private static final String XML_TAG_ENCLOSURE = "enclosure"; 
    private static final String XML_ATTRIBUTE_URL = "url"; 
    private static final String XML_ATTRIBUTE_TYPE = "type"; 


    String [] feedUrls; 

    public GetFeedsNetworkRequest(String... feedUrls) { 
     this.feedUrls = feedUrls; 
    } 

    @Override 
    public List<FeedResponse> performRequest(){ 
     List<FeedResponse> responseFeeds = new ArrayList<FeedResponse>(feedUrls.length); 
     for (String feedUrlString : feedUrls) { 
      InputStream inputStream = openStream(feedUrlString); 
      if (inputStream == null) { 
       return null; 
      } 
      try { 
       DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
       Document xmlDocument = documentBuilder.parse(inputStream); 

       String channelTitle = optFirstTagDocument(xmlDocument, XML_TAG_TILE); 
       String channelDescription = optFirstTagDocument(xmlDocument, XML_TAG_DESCRIPTION); 
       String channelURL = optFirstTagDocument(xmlDocument, XML_TAG_LINK); 

       NodeList allItemNodes = xmlDocument.getElementsByTagName(XML_TAG_ITEM); 
       List<ItemResponse> responseItems = new ArrayList<ItemResponse>(allItemNodes.getLength()); 
       for (int itemIndex = 0; itemIndex < allItemNodes.getLength(); itemIndex++) { 
        String itemURL = null; 
        String itemTitle = null; 
        String itemDescription = null; 
        String itemGUID = null; 
        String itemPubDate = null; 
        String itemEnclosureURL = null; 
        String itemEnclosureMIMEType = null; 

        Node itemNode = allItemNodes.item(itemIndex); 
        NodeList tagNodes = itemNode.getChildNodes(); 
        for(int tagIndex = 0; tagIndex < tagNodes.getLength(); tagIndex++){ 
         Node tagNode = tagNodes.item(tagIndex); 
         String tag = tagNode.getNodeName(); 

         if (XML_TAG_LINK.equalsIgnoreCase(tag)){ 
          itemURL = tagNode.getTextContent(); 
         } else if (XML_TAG_TILE.equalsIgnoreCase(tag)) { 
          itemTitle = tagNode.getTextContent(); 
         } else if (XML_TAG_DESCRIPTION.equalsIgnoreCase(tag)){ 
          itemDescription = tagNode.getTextContent(); 
         } else if (XML_TAG_ENCLOSURE.equalsIgnoreCase(tag)){ 
          NamedNodeMap enclosureAttributes = tagNode.getAttributes(); 
          itemEnclosureURL = enclosureAttributes.getNamedItem(XML_ATTRIBUTE_URL).getTextContent(); 
          itemEnclosureMIMEType = enclosureAttributes.getNamedItem(XML_ATTRIBUTE_TYPE).getTextContent(); 
         } else if (XML_TAG_PUB_DATE.equalsIgnoreCase(tag)){ 
          itemPubDate = tagNode.getTextContent(); 
         } else if (XML_TAG_GUID.equalsIgnoreCase(tag)){ 
          itemGUID = tagNode.getTextContent(); 
         } 
        } 

        responseItems.add(new ItemResponse(itemURL, itemTitle, itemDescription, 
          itemGUID, itemPubDate, itemEnclosureURL, itemEnclosureMIMEType)); 
        responseFeeds.add(new FeedResponse(feedUrlString, channelTitle, channelURL, channelDescription, responseItems)); 
        inputStream.close(); 
       } 
      } catch (IOException e){ 
       e.printStackTrace(); 
       setErrorCode(ERROR_IO); 
       return null; 

      } catch (SAXException e) { 
       e.printStackTrace(); 
       setErrorCode(ERROR_PARSING); 
       return null; 
      } catch (ParserConfigurationException e) { 
       e.printStackTrace(); 
       setErrorCode(ERROR_PARSING); 
       return null; 
      } 
     } 
     return responseFeeds; 

    } 

    private String optFirstTagDocument(Document document, String tagName) { 
     NodeList elementsByTagName = document.getElementsByTagName(tagName); 
     if (elementsByTagName.getLength() > 0){ 
      return elementsByTagName.item(0).getTextContent(); 
     } 
     return null; 

    } 

    public static class FeedResponse { 
     public final String channelFeedURL; 
     public final String channelTitle; 
     public final String channelURL; 
     public final String channelDescription; 
     public final List<ItemResponse> channelItems; 

     FeedResponse(String channelFeedURL, String channelTitle, String channelURL, String channelDescription, List<ItemResponse> channelItems) { 
      this.channelFeedURL = channelFeedURL; 
      this.channelTitle = channelTitle; 
      this.channelURL = channelURL; 
      this.channelDescription = channelDescription; 
      this.channelItems = channelItems; 
     } 

    } 

    public static class ItemResponse { 
     public final String itemURL; 
     public final String itemTitle; 
     public final String itemDescription; 
     public final String itemGUID; 
     public final String itemPubDate; 
     public final String itemEnclosureURL; 
     public final String itemEnclosureMIMEType; 

     ItemResponse(String itemURL, String itemTitle, String itemDescription, String itemGUID, 
        String itemPubDate, String itemEnclosureURL, String itemEnclosureMIMEType) { 
      this.itemURL = itemURL; 
      this.itemTitle = itemTitle; 
      this.itemDescription = itemDescription; 
      this.itemGUID = itemGUID; 
      this.itemPubDate = itemPubDate; 
      this.itemEnclosureURL = itemEnclosureURL; 
      this.itemEnclosureMIMEType = itemEnclosureMIMEType; 
     } 
    } 
} 
+0

也許將它們更改爲受保護?受保護的靜態最終字符串 –

回答

0

如果環比FeedResponse列表,你可以拉出自己的域,然後遍歷其FeedItem列出來獲取項目和建設RssFeed和RssItems隨你走:

GetFeedsNetworkRequest request = new GetFeedsNetworkRequest(feedURLs); 
List<FeedResponse> feedResponses = request.performRequest(); 
for (FeedResponse feedResponse : feedReponses) { 
    // gather fields you need to construct RssFeed 
    String channelTitle = feedResponse.channelTitle; 
    List<RssItem> items = new ArrayList<>(); 
    for (ItemResponse itemResponse : feedResponse.channelItems) { 
     // gather fields you need to construct RssItem 
     String itemDescription = itemResponse.itemDescription; 
     items.add(new RssItem(..., itemDescription, ...)); 
    } 
    feeds.add(new RssFeed(..., channelTitle, ..., items, ...); 
} 
+0

謝謝! 'List feedResponse'和'String channelTitle = feedResponse.channelTitle'是我正在繪製的空白部分。 – stamps

+0

我的榮幸。我很高興能夠提供幫助。 –