Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从另一个类访问最终变量并正确输出结果_Java_Android - Fatal编程技术网

Java 从另一个类访问最终变量并正确输出结果

Java 从另一个类访问最终变量并正确输出结果,java,android,Java,Android,我为android RSS阅读器设置了两个类文件 而不是在下面的GetNetworkRequest中为public static class FeedResponse和public static class itemsresponse中的信息提供getter。我使用了公共最终字符串变量 我似乎不知道如何从这些最终变量中获取信息,并将它们设置为feed和items中的DataSource有效地替换我的createFakeData方法 我已验证来自网络请求的所有信息是否已加载到feedrespons

我为android RSS阅读器设置了两个类文件

而不是在下面的
GetNetworkRequest
中为
public static class FeedResponse
public static class itemsresponse
中的信息提供getter。我使用了公共最终字符串变量

我似乎不知道如何从这些最终变量中获取信息,并将它们设置为
feed
items
中的
DataSource
有效地替换我的
createFakeData
方法

我已验证来自网络请求的所有信息是否已加载到
feedresponse
ItemResponse
列表中

如何访问这些列表并在
数据源中设置它们

数据源

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));
        }
    }
}
包io.bloc.android.blocly.api;
导入java.util.ArrayList;
导入java.util.List;
导入io.bloc.android.blocly.blocly应用程序;
导入io.bloc.android.blocly.R;
导入io.bloc.android.blocly.api.model.RssFeed;
导入io.bloc.android.blocly.api.model.RssItem;
导入io.bloc.android.blocly.api.network.GetFeedsNetworkRequest;
导入io.bloc.android.blocly.api.network.NetworkRequest;
导入静态io.bloc.android.blocly.api.network.GetFeedsNetworkRequest.*;
公共类数据源扩展GetFeedsNetworkRequest{
公共列表提要;
公共清单项目;
公共数据源(){
feed=newarraylist();
items=newarraylist();
createFakeData();
新线程(newrunnable()){
@凌驾
公开募捐{
新建GetFeedsNetworkRequest(“http://feeds.feedburner.com/androidcentral?format=xml)。性能要求();
}
}).start();
}
公共列表getFeeds(){
回馈;
}
公共列表getItems(){
退货项目;
}
void createFakeData(){
Feed.add(新RssFeed(“我最喜欢的Feed!”),
“这条消息太不可思议了,我甚至不能告诉你……”,
"http://favoritefeed.net", "http://feeds.feedburner.com/favorite_feed?format=xml"));
对于(int i=0;i<10;i++){
添加(新的RssItem)项(字符串值),
BloclyApplication.getSharedInstance().getString(R.string.placeholder_headline)+“+i,
BloclyApplication.getSharedInstance().getString(R.string.placeholder_内容),
"http://favoritefeed.net?story_id=an-难以置信的新闻故事“,
"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;
        }
    }
}
包io.bloc.android.blocly.api.network;
导入android.provider.documents合同;
导入android.util.Log;
导入org.w3c.dom.Document;
导入org.w3c.dom.NamedNodeMap;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
导入org.xml.sax.SAXException;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.parserConfiguration异常;
公共类GetFeedsNetworkRequest扩展了NetworkRequest{
公共静态final int ERROR_PARSING=3;
私有静态最终字符串XML\u TAG\u TILE=“title”;
私有静态最终字符串XML\u TAG\u DESCRIPTION=“DESCRIPTION”;
私有静态最终字符串XML_TAG_LINK=“LINK”;
私有静态最终字符串XML_TAG_ITEM=“ITEM”;
私有静态最终字符串XML_TAG_PUB_DATE=“pubDate”;
私有静态最终字符串XML_TAG_GUID=“GUID”;
私有静态最终字符串XML_TAG_ENCLOSURE=“ENCLOSURE”;
私有静态最终字符串XML\u ATTRIBUTE\u URL=“URL”;
私有静态最终字符串XML_属性_TYPE=“TYPE”;
字符串[]feedURL;
公共GetFeedsNetworkRequest(字符串…feedUrls){
this.feedUrls=feedUrls;
}
@凌驾
公共列表performRequest(){
List responseFeeds=new ArrayList(feedUrls.length);
for(字符串feedUrlString:feedUrls){
InputStream InputStream=openStream(feedUrlString);
如果(inputStream==null){
返回null;
}
试一试{
DocumentBuilder DocumentBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document xmlDocument=documentBuilder.parse(inputStream);
字符串channelTitle=optFirstTagDocument(xmlDocument,XML_TAG_TILE);
字符串channelDescription=optFirstTagDocument(xmlDocument,XML\u TAG\u DESCRIPTION);
字符串channelURL=optFirstTagDocument(xmlDocument,XML_标记_链接);
NodeList allItemNodes=xmlDocument.getElementsByTagName(XML_标记_项);
List responseItems=newArrayList(allItemNodes.getLength());
对于(int-itemIndex=0;itemIndexGetFeedsNetworkRequest 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, ...);
}