簡體   English   中英

從另一個類訪問最終變量並正確輸出結果

[英]Accessing final variables from another class and correctly outputting the results

我為Android RSS閱讀器設置了兩個類文件。

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

我似乎無法弄清楚的是如何從這些最終變量中獲取信息並將其設置為DataSource feedsitems ,從而有效地替代了我的createFakeData方法。

我已經驗證了來自網絡請求的所有信息確實FeedReponse加載到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;
        }
    }
}

如果您遍歷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, ...);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM