极度郁闷的一个读取文本的问题?求高手!
为什么同样一段代码放在 android模拟器中 和放在一般的java main方法中执行的效果不一样?! 在main方法中能正确读取到文本信息,而在模拟器中“getInputStream”不能输出! 报错的地方是 buffer.close(); (NullPointException)
Activity: http://hi.csdn.net/space-10185993-do-album-picid-960848-goto-down.html
download:http://hi.csdn.net/space-10185993-do-album-picid-960848.html
public String download(String urlStr) {
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try {
URL url = new URL("http://baidu.com");
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
System.out.println("urlConn........."+urlConn); System.out.println("getInputStream:"+urlConn.getInputStream());
buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
while( (line = buffer.readLine()) != null){
sb.append(line);
System.out.println("line:"+line);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try {
buffer.close();
} catch (
IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
------解决方案--------------------
你用scanner试试
------解决方案--------------------