上一篇是自己写的网络Stream读取并转换成String的源代码,今天研究了一下新浪的写法,下面贴出来供大家参考>>:
/** * Returns the response body as string.<br> * Disconnects the internal HttpURLConnection silently. * @return response body * @throws WeiboException */ public String asString() throws WeiboException{ if(null == responseAsString){ BufferedReader br; try { InputStream stream = asStream(); if (null == stream) { return null; } br = new BufferedReader(new InputStreamReader(stream, "UTF-8")); StringBuffer buf = new StringBuffer(); String line; while (null != (line = br.readLine())) { buf.append(line).append("\n"); } this.responseAsString = buf.toString(); if(Configuration.isDalvik()){ this.responseAsString = unescape(responseAsString); } log(responseAsString); stream.close(); con.disconnect(); streamConsumed = true; } catch (NullPointerException npe) { // don't remember in which case npe can be thrown throw new WeiboException(npe.getMessage(), npe); } catch (IOException ioe) { throw new WeiboException(ioe.getMessage(), ioe); } } return responseAsString; }Java读取网络数据(新浪网址)InputStream的数据流操作实录笔记(二) 分享出来供大家参考!(...
最新推荐文章于 2023-03-20 21:16:16 发布
本文介绍了一种从网络Stream中读取数据并将其转换为String的方法。通过使用BufferedReader与InputStreamReader配合,能够有效地读取数据并指定字符集进行解码。此方法适用于处理HTTP响应等内容。
4222

被折叠的 条评论
为什么被折叠?



