Android从url cut-off读取xml

时间:2014-07-22 12:42:19

标签: android xml facebook httpurlconnection

我有一个从Facebook页面获取xml的应用程序。问题是缺少这部分:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
      xmlns:media="http://search.yahoo.com/mrss/"
      xmlns:dc="http://purl.org/dc/elements/1.1/"
    >
  <channel>
    <title>Стената на Interesting Engineering</title>
    <link>https://www.facebook.com/</link>
    <description>Стената на Interesting Engineering</description>
    <language>en-us</language>
    <category domain="Facebook">PageSyndicationFeed</category>
    <generator>Facebook Syndication</generator><docs>http://www.rssboard.org/rss-specification</docs>
    <lastBuildDate>Tue, 22 Jul 2014 10:00:01 +0000</lastBuildDate>
    <webMaster>webmaster@facebook.com</webMaster>
    <item>
      <guid isPermaLink="false">www.facebook.com/notification/9433896181aa13927fc

这是xml的开头。 我是这样得到的:

@Override
        protected Void doInBackground(Void... params) {
            URL url;
            String pageXML = ""; 
            try {
                url = new URL(pageUrl);

                HttpURLConnection urlConnection = (HttpURLConnection) url
                        .openConnection();

                InputStream in = urlConnection.getInputStream();
                streamToString(in);

                Log.i("", pageXML);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        private void streamToString(InputStream in) throws IOException {
            StringBuilder inputStringBuilder = new StringBuilder();
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(in, "UTF-8"));
            String line = bufferedReader.readLine();

            while (line != null) {
                inputStringBuilder.append(line);
                inputStringBuilder.append('\n');
                line = bufferedReader.readLine();
            }

            pageXML = inputStringBuilder.toString();

            Log.i("", pageXML);
            write("interesting_engineering", pageXML);
        }

你知道问题出在哪里吗? This是我从中获取xml的网址。

1 个答案:

答案 0 :(得分:0)

我发现了问题。我的代码中有一部分(我在这里删除了)弄乱了这些东西。这导致了这个问题:

        InputStreamReader isw = new InputStreamReader(in);

        int data = isw.read();

        //This line is correct
        streamToString(in);

        while (data != -1) {
            char current = (char) data;
            data = isw.read();
        }