我使用Java爲Android設備編寫此代碼。我只想讀取RSS提要中的所有數據,然後準備好解析器來處理數據。我遇到的麻煩是隻有一小部分數據被我的代碼讀取。然後它表示已到達閱讀器的末尾,並繼續將部分數據返回給解析器。閱讀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
[email protected]</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
如果檢查飼料(http://feeds2.feedburner.com/blogspot/Dcni),那麼你會發現這只是內容的一小部分。我想,因爲我收到堆消息,說手機可能沒有堆空間,因此退出,但我發現一個RSS TTS項目,它有自己的http代碼:talkingrssreader,當我將它們拔出並與我一起嘗試代碼它仍然有同樣的問題。嘗試使用該Feed的談話RSS閱讀器應用程序不存在此問題。
我現在不知道還有什麼可以嘗試的。我可以在沒有問題的情況下獲得並解析較短的RSS提要,但似乎在一定的長度之後,它拒絕收集所有數據。我已經瀏覽了Android市場上的很多RSS閱讀器,並沒有看到其他人將Feed縮短,所以這讓我相信這是我的代碼中的問題,而不是設備內存不足。我的代碼很容易讀取119kb xml文件,但在僅有32kb的Google RSS提要上會失敗,這一點進一步證明了這一點。
任何人都可以給我一些什麼問題的想法?
編輯: 添加由the100rabh和艾薩克建議的修改後,代碼現在看起來是這樣的:
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();
的問題仍然存在,雖然。它將數據簡化爲與之前代碼完全相同的地方。
另外,您使用的字符串連接技術非常浪費。將'data'定義爲'StringBuffer',並使用'StringBuffer.append(char [] str,int offset,int len)'而不是你現在正在做的事情。 – Isaac 2010-10-21 09:06:16
謝謝你的幫助。我現在更改了代碼以添加兩個建議,但問題仍然存在。輸出中的GORW_HEAP和GC_FOR_MALLOC消息少得多,但數據仍然在與之前完全相同的位置截斷。我會在我原來的問題結尾處發佈新代碼,因爲它不適合這裏。 – Teario 2010-10-21 10:11:31