我最近一直在使用BufferedReader遇到问题。它部分地从流中读取(部分意思是它从输入流中读取的内容少于服务器在输出流中实际写入的内容。响应有很多行)。以下是代码
while ((inputLine = in.readLine()) != null){ //<- Reads partially
buffer.append(inputLine);
// Buffer Is an object of StringBuffer
// And in is bufferedreader.
}
我在谷歌搜索过这个,发现这可能是因为服务器和客户端在阅读和写作时失误了。所以我尝试了下面的代码。
int counter =0;
while(counter <5){
while ((inputLine = in.readLine()) != null){
buffer.append(inputLine);
counter = 0;
}
counter++;
if(counter==4){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
仍然没有改善。我正在使用Google的api文本搜索来进行一些返回大量文本的查询。但奇怪的是它总是返回相同长度的响应。当我读到这篇文章时,我意识到了这一点
http://tiku.io/questions/4155487/android-httpget-incomplete-response-bufferedreader
在上面的链接(第3个答案)中,他提到这不是缓冲读卡器的问题,但Java String有一些限制。难以置信!这可能是原因吗?从技术上讲,字符串的长度返回一个整数,因此字符串的最大长度不能超过2 ^ 32-1(超过效率,但是它不是字符串的实际绑定。但响应是没有那么大,以至于它会耗尽内存。而且没有OutOfMemory异常)。那么有人能说出这里的实际问题是什么吗?
应用程序读取的样本部分响应
{ "html_attributions" : [ "Listings by \u003ca href=\"http://www.indiacom.com/\"\u003eIndiacom Yellow Pages\u003c/a\u003e" ], "next_page_token" : "CuQB3QAAAA9LWxl0Zihy1r0NqXIJ8MsCZ9JVHrvm13-PtAz_6E59zAcnd7fKnEVg8LXgm30ACPG2sBUqx2_hGIxuZXpE4phQBR_SB8JdTJFJKTJ7VKoQNU9iQXPbS-0MAxW2lqrsJzAj7ZLKnlwYVXEYi7Vcw-qskrJweeJKU0mtz1s1NgkrwjPMoi78VHuw3-i79xTlQIwFCmHpNyGXiG4cAWXm4T8uBqERZZ7p3SDv-dmoqZceLlWYzZheRcllGEtkfTbCAVwLCw168mbM_mzNcxWbx-96GyQIjlSIYiyJuxM6_S5xEhC3ekHSAg-E3fVFuvqIiEfFGhR9SpyAw0_t-FAir6mJn9ElV3vAnA", "results" : [ { "formatted_address" : "No. 313, Mettupalayam Road, Saibaba Koil, Coimbatore, Tamil Nadu 641043, India", "geometry" : { "location" : { "lat" : 11.026208, "lng" : 76.95259799999999 } }, "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png", "id" : "ef736016c44cf960d4c7d4d9d32ac221c8255f5a", "name" : "Ganga Hospital", "place_id" : "ChIJ9-xr2e9YqDsRLnG40S1Ndrk", "rating" : 4.4, "reference" : "CoQBcQAAANcLCLGKHLEbTSwQdcbhRiJcrtKJqJbl9H5umesRvtlXWCYUwsTxbvVRHfaVh-l7RRje1HVsuklQ_XGnmc7OXHW4ZUfPYIBIH6tB5XLs6_5lj9W9hT6Q1EH4zK5k7Cbf6Wu52qFyj93CWLmuj5qeoLwgptNCWf3-ujmbVDrcHDLyEhD-2Bja75Fa8FeLBJbY032GGhSuCw2uExbtV_YBx-3pyRCizTalXA", "types" : [ "hospital", "establishment" ] }, { "formatted_address" : "Govt Arts College Road, Gopalapuram, Coimbatore, Tamil Nadu 641018, India", "geometry" : { "location" : { "lat" : 11.000514, "lng" : 76.971526 } }, "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png", "id" : "d6f82ccc732044cc3deb9fa6c5a4283707cd03f1", "name" : "KG Hospital", "place_id" : "ChIJAQBAtaVZqDsRtFzd-7QKtpA", "rating" : 3.7, "reference" : "CnRuAAAAmpE68INB08dOsxIyKg_Ys9qaZjIjelNkA32E4zu963JCILPN94cTM_7b5-s6lp9ElnmwcMyO689bsn_1ax40Y_VI8WiXkk65mHAqUEoh7awcEEtgx-HjI3rLdZPEXDTjRBj-7SohkyH-s-QogvHKxxIQ3lAGHhT_HyZV-Yo8W0f8RxoUv8GPvTTXu8SFUawpMgSSEE8C0BA", "types" : [ "hospital", "establishment" ] }, { "formatted_address" : "Nethaji Road,Pappanaickenpalayam, Coimbatore, Tamil Nadu 641037, India", "geometry" : { "location" : { "lat" : 11.01277, "lng" : 76.980508 } }, "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png", "id" : "7e57fe692d9273faba387bfbf10e0d6ad48dd893", "name" : "G Kuppuswamy Naidu Memorial Hospital", "photos" : [ { "height" : 935, "html_attributions" : [], "photo_reference" : "CnRnAAAAhUxLjdksHg289yJS08LGuNefM_CDKjCYHbLcVItrUnFNylD7YpgFPVyGSjg3FOADXhrz-nXkEC82gBLyzOIuMq0xXJapoJzmtq9rZmVFaUz9iaM33VxRt9qY2oP46AXwmi0g2ic67Um7iYNbQb1bcBIQqjM9-sR6kM-WeVRKB5wljhoUcDt52QaV_U6D8KGhysYTdUq7yno", "width" : 1809 } ], "place_id" : "ChIJH8pncLNZqDsRJE3drBfyqC4", "rating" : 4.3, "reference" : "CpQBhgAAAIYMMgOuddpbNbU2OnTafr2WmqwLoy9VMe1JCgAdcpv84bwHCdZLDmsD0cDsDVfkOxxFnBchCca-O8zTvFl6ZqWPLIG9Sia7H6fpFQl-8BqM9na9FKiY-6M0OgdPn493nseIRidQGZAz8h121zKb4ks--IvdpSn3khR6G3-j6mrod9DSSfa5MwDIjTGxJYaDzBIQPMJyhDwCM9ASoRtBXFXdhRoUtSURW5KmhN9n6NcwjMJObp4on2w", "types" : [ "hospital", "health", "establishment" ] }, { "formatted_address" : "No. 120, West Periasamy Road, R.S.Puram, Coimbatore, Tamil Nadu 641002, India", "geometry" : { "location" : { "lat" : 11.013005, "lng" : 76.948144 } }, "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png", "id" : "1fc1beb232ca7fb02555df76889cad33eb20d060", "name" : "Rao Hospital", "place_id" : "ChIJIzoxVRxZqDsRF7rY3dvXP5o", "reference" : "CnRvAAAA7_4DD-Df4zKXujSMTj-aEkR_gazKz__hfBPqYJOJmMHAbX-IaaeWNDc6e-6YW
部分读取响应的长度在19,000 - 20000之间变化。