我正在開發一個Android應用程序,它將在流中獲取大塊JSON數據。調用Web服務是好的,但我有一點問題。在我以前的版本中,我使用Gson讀取數據流,然後嘗試將數據插入數據庫,除了性能以外,沒有任何問題。所以我試圖改變加載數據的方法,我試圖先讀取char[]
的數據,然後將它們插入數據庫。從HttpGet的web服務讀取char [] - 奇怪的行爲
這是我的新代碼:
HttpEntity responseEntity = response.getEntity();
final int contentLength = (int) responseEntity.getContentLength();
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
int readCount = 10 * 1024;
int hasread = 0;
char[] buffer = new char[contentLength];
int mustWrite = 0;
int hasread2 = 0;
while (hasread < contentLength) {
// problem is here
hasread += reader.read(buffer, hasread, contentLength - hasread);
}
Reader reader2 = new CharArrayReader(buffer);
的問題是,讀者正確啓動,但在讀取流結束的臨近,hasread
變量值下降(由1
)而不是增加。我很奇怪,然後while
循環永遠不會結束。這段代碼有什麼問題?
看了你的文章。請給我點你的解決方法。 – 2016-01-05 15:18:09