我正在尝试阅读 URL 的内容。BufferedReader 的初始化不断导致崩溃。
我已经尝试查找人们的解决方案,但无论我做什么,我都会收到此错误。
这是我的代码:
try {
String sUrl = "http://www.google.com";
System.out.println("About to create URL.");
URL url = new URL(sUrl);
System.out.println("Created URL");
System.out.println("About to create URLConnection");
URLConnection urlc = url.openConnection();
System.out.println("Created URLConnection");
System.out.println("About to create the BufferedReader");
BufferedReader bR = new BufferedReader(new
InputStreamReader(urlc.getInputStream()));
System.out.println("Created the BufferedReader");
System.out.println("About to create a StringBuilder");
StringBuilder sBuilder = new StringBuilder();
System.out.println("Created StringBuilder");
int byteRead;
System.out.println("About to enter while loop and build string");
while ((byteRead = bR.read()) != -1)
{
sBuilder.append((char) byteRead);
}
System.out.println("Built string");
bR.close();
System.out.println("Buffered reader is closed");
String text = sBuilder.toString();
System.out.println(text);
}
catch (IOException e)
{
//
}
这是我得到的(一些)日志:
07-24 13:56:17.546: I/System.out(1554): About to create URL.
07-24 13:56:17.557: I/System.out(1554): Created URL
07-24 13:56:17.557: I/System.out(1554): About to create URLConnection
07-24 13:56:17.557: I/System.out(1554): Created URLConnection
07-24 13:56:17.557: I/System.out(1554): About to create the BufferedReader
07-24 13:56:17.576: D/AndroidRuntime(1554): Shutting down VM
07-24 13:56:17.576: W/dalvikvm(1554): threadid=1: thread exiting with uncaught exception
(group=0x40a13300)
07-24 13:56:17.606: E/AndroidRuntime(1554): FATAL EXCEPTION: main
07-24 13:56:17.606: E/AndroidRuntime(1554): java.lang.RuntimeException: Unable to start
activity ComponentInfo{com. ...
提前致谢。