我正在创建我的XML Reader应用程序,该应用程序试图解析XDA-Developers网站提要中的内容。事实是,它不断重复第一行,也给了我一些错误。

我正在使用此sample。示例中的链接可以正常工作,但是当我放置链接时,有时会出现错误。

我的XMLParser:

public class XMLParserXDA extends DefaultHandler{

    private String URL_MAIN = "http://www.xda-developers.com/feed/?format=xml";
    String TAG = "XMLHelper";

    Boolean currTag = false;
    String currTagVal = "";
    private XDAObject post = null;
    private ArrayList<XDAObject> posts = new ArrayList<>();

    public ArrayList<XDAObject> getPostsList() {
        return this.posts;
    }

    public void get() {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser mSaxParser = factory.newSAXParser();
            XMLReader mXmlReader = mSaxParser.getXMLReader();
            mXmlReader.setContentHandler(this);
            InputStream mInputStream = new URL(URL_MAIN).openStream();
            mXmlReader.parse(new InputSource(mInputStream));
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        if (currTag) {
            currTagVal = currTagVal + new String(ch, start, length);
            currTag = false;
        }
    }
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        currTag = false;

        if (localName.equalsIgnoreCase("title")) {
            post.setTitle(currTagVal);
        }

        else if (localName.equalsIgnoreCase("description")) {
            post.setDescription(currTagVal);
        }

        else if (localName.equalsIgnoreCase("guid")) {
            post.setGuid(currTagVal);
        }

        else if (localName.equalsIgnoreCase("item")) {
            posts.add(post);
        }
    }

    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException {
        Log.i(TAG, "START TAG: " + localName);

        currTag = true;
        currTagVal = "";
        if (localName.equals("item"))
            post = new XDAObject();
    }

}


问题在这里:

01-13 20:22:08.049    4274-4294/com.kostas.myapplication I/XMLHelper﹕ START TAG: rss
01-13 20:22:08.049    4274-4294/com.kostas.myapplication I/XMLHelper﹕ START TAG: channel
01-13 20:22:08.049    4274-4294/com.kostas.myapplication I/XMLHelper﹕ START TAG: title
01-13 20:22:08.052    4274-4294/com.kostas.myapplication E/XMLHelper﹕ Exception: Attempt to invoke virtual method 'void activityobjects.XDAObject.setTitle(java.lang.String)' on a null object reference
01-13 20:22:09.103    4274-4289/com.kostas.myapplication W/EGL_genymotion﹕ eglSurfaceAttrib not implemented
01-13 20:22:09.103    4274-4289/com.kostas.myapplication W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa36c8e80, error=EGL_SUCCESS
01-13 20:22:09.180    4274-4274/com.kostas.myapplication I/Choreographer﹕ Skipped 102 frames!  The application may be doing too much work on its main thread.
01-13 20:22:12.469    4274-4274/com.kostas.myapplication I/Choreographer﹕ Skipped 197 frames!  The application may be doing too much work on its main thread.


XDAObject类是:

public class XDAObject {
    private String title = null;
    private String guid = "";
    private String description = "";
    private String content = "";


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getGuid() {
        return guid;
    }

    public void setGuid(String link) {
        this.guid = link;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}


有任何想法吗??提前致谢!!

最佳答案

currtagval =“” =不作为字符串使用,并使用参数no覆盖方法字符

所以currtagval = currtagval +字符(intchar [] ch,int开头,int长度)

=

currtagvall =“” +未使用的方法==>是否为null?

08-04 05:42