您的位置:首页 > 理论基础 > 计算机网络

Java读取网络数据(新浪网址)InputStream的数据流操作实录笔记(二) 分享出来供大家参考!(

2011-08-12 15:29 751 查看
上一篇是自己写的网络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 stream string null
相关文章推荐