java–StringBuilder在转换为String时丢失了数据

java–StringBuilder在转换为String时丢失了数据,第1张

概述这有什么问题当我尝试从StringBuilder中获取String时遇到问题BufferedReaderreader=newBufferedReader(newInputStreamReader(response.getEntity().getContent()),128*1024);StringBuilderdataResponseSB=newStringBuilder();Stringline;while((line=reade

这有什么问题

当我尝试从StringBuilder中获取String时遇到问题

BufferedReader reader = new BufferedReader(new inputStreamReader(response.getEntity().getContent()), 128 * 1024);StringBuilder dataResponseSB = new StringBuilder();String line ;while ((line = reader.readline()) != null) {    dataResponseSB.append(line);    if (DataFactory.DEBUG_MODE) {        // all data here are complete        Log.i("===LoadDataActivity","line: "+line);    }}String rawdata = new String(dataResponseSB); // dataResponseSB.toString(); also not workif (DataFactory.DEBUG_MODE) {    // data here are lost    Log.i("===LoadDataActivity","rawdata: "+rawdata);}

( – )我从BufferedReader .readline()收到一个巨大的数据

( – )我使用Log来检查并确保每行有大约5行8000缓冲区大小,我非常确定我已正确接收所有数据

(1)我将每行附加到StringBuilder这里

( – )我将所有行追加到StringBuilder之后

(2)我尝试将其转换回String

( – )现在,问题是,当我在这里检查新的String时,数据只有8192(它应该包含至少30,000或更多)

问题是什么 ?当它追加到StringBuilder(1)时它不确定它丢失了,或者当它转换回String(2)时它丢失了

我在下面添加了我尝试过的代码,我尝试了UTF8和没有UTF8

            httpParams params = new BasichttpParams();            httpProtocolParams.setVersion(params, httpVersion.http_1_1);            //params.setParameter(CoreProtocolPnames.PROTOCol_VERSION, );            params.setParameter(CoreConnectionPnames.soCKET_BUFFER_SIZE, 128 * 1024);            httpClIEnt clIEnt = new DefaulthttpClIEnt(params);        //  httpClIEnt clIEnt = new DefaulthttpClIEnt(new BasichttpParams());            httpPost httppost = new httpPost(DataFactory.REQUEST_API_URL + "?ID=" + DataFactory.USER_ID );            // Depends on your web service            httpconnectionParams.setConnectionTimeout(clIEnt.getParams(), 10000); //Timeout limit            httpconnectionParams.setSocketBufferSize(clIEnt.getParams(), 128 * 1024);            httpResponse response = clIEnt.execute(httppost);              //response.setParams(clIEnt.getParams().setParameter(CoreConnectionPnames.soCKET_BUFFER_SIZE, 128 * 1024));            //String rawdata = IoUtils.toString(response.getEntity().getContent(), "UTF-8");           // String rawdata = EntityUtils.toString(response.getEntity());            String rawdata = getResponseBody(response.getEntity());            //Scanner s = new Scanner(response.getEntity().getContent()).useDelimiter("\A");            //String rawdata = s.hasNext() ? s.next() : "";            /*            //BufferedReader reader = new BufferedReader(new inputStreamReader(response.getEntity().getContent()));            // ===================            BufferedReader reader = new BufferedReader(new inputStreamReader(response.getEntity().getContent()), 128 * 1024);            StringBuilder dataResponseSB = new StringBuilder();            String line ;            while ((line = reader.readline()) != null) {                dataResponseSB.append(line);                if (DataFactory.DEBUG_MODE) {                    Log.i("===LoadDataActivity","line: "+line);                }            }            dataResponseSB.trimToSize();            String rawdata = new String(dataResponseSB);            /*            inputStreamReader reader = new inputStreamReader(response.getEntity().getContent());            StringBuffer sb = new StringBuffer();            int c;            while ((c = reader.read()) != -1) {                sb.append((char)c);                if (DataFactory.DEBUG_MODE) {                    //Log.i("===LoadDataActivity","line: "+line);                }            }            */

解决方法:

我很确定这是问题所在:

Log.i("===LoadDataActivity","rawdata: "+rawdata);

您假设日志条目可以包含所有数据 – 我相信每个日志条目限制为8192个字符.

我建议你记录rawdata.length(),你会发现它实际上得到了所有的数据 – 它只是记录它失败了.

总结

以上是内存溢出为你收集整理的java – StringBuilder在转换为String时丢失了数据全部内容,希望文章能够帮你解决java – StringBuilder在转换为String时丢失了数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1107401.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存