我在Java中为Android设备编写代码。我只想阅读RSS提要中的所有数据,然后我有一个解析器准备好对数据采取行动。我遇到的麻烦是我的代码只读取了一小部分数据。然后它说已经到达阅读器的末尾并继续将部分数据返回给解析器。
我的代码是:
try
{
URL contentUrl = new URL(url);
URLConnection conn = contentUrl.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
if(httpConn.getResponseCode() == HttpURLConnection.HTTP_OK)
{
InputStreamReader inStr = new InputStreamReader(httpConn.getInputStream());
int charsRead;
char[] buff = new char[BUFFER_SIZE];
String data = "";
while((charsRead = inStr.read(buff,0,BUFFER_SIZE))!= -1)
{
data += String.copyValueOf(buff, 0, charsRead);
buff = new char[BUFFER_SIZE];
}
Log.d("Http","Data: "+data.);
return data;
}
else
{
return null;
}
}
catch(IOException e)
{
return null;
}
调试日志输出如下所示:
> D/dalvikvm(19134): GC_FOR_MALLOC freed 24 objects / 757272 bytes in 27ms
D/dalvikvm(19134): GC_FOR_MALLOC freed 2 objects / 48 bytes in 26ms
D/dalvikvm(19134): GC_FOR_MALLOC freed 3 objects / 301112 bytes in 29ms
I/dalvikvm-heap(19134): Grow heap (frag case) to 3.677MB for 305028-byte allocation
D/dalvikvm(19134): GC_FOR_MALLOC freed 0 objects / 0 bytes in 37ms
D/dalvikvm(19134): GC_FOR_MALLOC freed 10 objects / 760856 bytes in 27ms
D/dalvikvm(19134): GC_FOR_MALLOC freed 3 objects / 305112 bytes in 26ms
D/dalvikvm(19134): GC_FOR_MALLOC freed 10 objects / 770856 bytes in 26ms
D/dalvikvm(19134): GC_FOR_MALLOC freed 2 objects / 48 bytes in 26ms
D/dalvikvm(19134): GC_FOR_MALLOC freed 3 objects / 309112 bytes in 27ms
D/dalvikvm(19134): GC_FOR_MALLOC freed 22 objects / 777648 bytes in 27ms
D/dalvikvm(19134): GC_FOR_MALLOC freed 2 objects / 48 bytes in 30ms
D/dalvikvm(19134): GC_FOR_MALLOC freed 3 objects / 309288 bytes in 26ms
D/dalvikvm(19134): GC_FOR_MALLOC freed 27 objects / 778696 bytes in 27ms
D/Http (19134): Data: <?xml version="1.0" encoding="UTF-8"?>
D/Http (19134): <?xml-stylesheet type="text/xsl" media="screen" href="/~d/sty
les/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http:
//feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.or
g/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:geors
s="http://www.georss.org/georss" xmlns:thr="http://purl.org/syndication/thread/1
.0" xmlns:gd="http://schemas.google.com/g/2005" xmlns:feedburner="http://rssname
space.org/feedburner/ext/1.0" gd:etag="W/"CUcFSHc7fip7ImA9Wx5UFk8.""><
id>tag:blogger.com,1999:blog-11300808</id><updated>2010-10-20T18:03:39.906-07:00
</updated><title type="text">Google Code Blog</title><subtitle type="html">Updat
es from Google's open source projects.</subtitle><link rel="http://schemas.googl
e.com/g/2005#feed" type="application/atom+xml" href="http://googlecode.blogspot.
com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://g
ooglecode.blogspot.com/" /><link rel="next" type="application/atom+xml" href="ht
tp://www.blogger.com/feeds/11300808/posts/default?start-index=26&max-results
=25&redirect=false&v=2" /><author><name>Chris DiBona</name><email>norepl
y@blogger.com</email></author><generator version="7.00" uri="http://www.blogger.
com">Blogger</generator><openSearch:totalResults>753</openSearch:totalResults><o
penSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openS
earch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="
self" type="application/atom+xml" href="http://feeds.feedburner.com/blogspot/Dcn
i" /><feedburner:info uri="blogspot/dcni" /><atom10:link xmlns:atom10="http://ww
w.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry g
d:etag="W/"CUcFSHc6fCp7ImA9Wx5UFk8.""><id>tag:blogger.com,1999:blog-11
300808.post-3825033765857847156</id><published>2010-10-20T18:03:00.000-07:00</pu
blished><updated>2010-10-20T18:03:39.914-07:00</updated><app:edited xmlns:app="h
ttp://www.w3.org/2007/app">2010-10-20T18:03:39.914-07:00</app:edited><category s
cheme="http://www.blogger.com/atom/ns#" term="speed tracer" /><category scheme="
http://www.blogger.com/atom/ns#" term="gwt" /><category scheme="http://www.blogg
er.com/atom/ns#" term="eclipse" /><category scheme="http://www.blogger.com/atom/
ns#" term="cloud portability" /><title>Advancing cloud computing with integrated
developer tools by Google and VMware</title><content type="html"><p><i
>Cross-posted from the <a href="http://googlewebtoolkit.blogspot.com/">
Google Web Toolkit Blog</a></i></p>
D/Http (19134):
D/Http (19134): <p>
D/Http (19134): Earlier this year at Google I/O, we <a href="http://google
code.blogspot.com/2010/05/enabling-cloud-portability-with-google.html">announ
ced</a> a collaboration between <a href="http://www.google.com">Goog
le</a> and <a href="http://www.vmware.com">VMware</a> focused
on making it easy to build business-oriented, <a href="http://code.google.com
/cloudportability/">cloud portable</a> web apps. We showed how business
es could use our integrated developer tools to build modern web apps that are ÔÇ
£cloud readyÔÇØ from the start, and can be deployed to any standard environment,
including <a href="http://code.google.com/appengine/">Google App Engine&l
t;/a> and on <a href="http://www.springsource.com/products/cloud-applicati
on-platform">VMware vFabric</a> on-premise solutions. Today we are happ
y to announce that these tools will be generally available within the next few w
eeks.
D/Http (19134): </p>
D/Http (19134):
D/Http (19134): <p>
D/Http (19134): Of course, if youÔÇÖre itching to get a head start, you can j
ump right in by downloading the release candidate version of <a href="http://
www.springsource.com/landing/best-development-tool-enterprise-java">SpringSou
rce Tool Suite</a>.
D/Http (19134): </p>
D/Http (19134):
D/Http (19134): <p>
D/Http (19134): If youÔÇÖd prefer to wait for the general release, you can &l
t;a href="http://code.google.com/cloudportability/">sign up</a> to be n
otified as soon as they are
如果您查看了Feed(http://feeds2.feedburner.com/blogspot/Dcni),那么您会发现这只是内容的一小部分。我想,因为我收到堆消息,说电话可能已经耗尽了堆空间,因此退出了,但我找到了一个RSS TTS项目,它有自己的http代码:talkingrssreader当我把它们拉出来时尝试使用我的代码它仍然有同样的问题。尝试使用该Feed讨论RSS阅读器应用程序没有这个问题。
我现在不知道还有什么可尝试的。我可以毫无问题地获取和解析较短的RSS源,但似乎在一定长度之后它只是拒绝收集所有数据。我已经浏览了Android市场上的很多RSS阅读器,没有看到任何其他人缩短了Feed,所以这让我相信这是我的代码中的一个问题,而不是设备耗尽的内存。我的代码很容易读取一个119kb的xml文件,但是在Google RSS提要上只有32kb,这个事实进一步证明了这一点。
有人能就这个问题给我一些想法吗?
编辑: 添加100rabh和Isaac建议的更改后,代码现在看起来像这样:
InputStreamReader inStr = new InputStreamReader(httpConn.getInputStream());
int charsRead;
char[] buff = new char[BUFFER_SIZE];
StringBuffer data = new StringBuffer();
while((charsRead = inStr.read(buff,0,BUFFER_SIZE))!= -1)
{
data.append(buff, 0, charsRead);
}
Log.d("Http","Data: "+data.toString());
return data.toString();
问题仍然存在。它将数据缩短到与之前代码完全相同的位置。
答案 0 :(得分:1)
我认为如果你每次都避免创建buff,你的问题就会消失
buf = new char [BUFFER_SIZE];
答案 1 :(得分:0)
好的,这是我最近发现的。 Log.d不会打印长消息http://code.google.com/p/android/issues/detail?id=5745&q=logcat%20long%20string&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars
所以会发生什么,虽然你有数据,但logcat不打印它。您可以尝试打破字符串,然后通过logcat打印。您的实际应用程序不会有任何问题。