我正在尝试读取 url 页面的内容并将其写入日志。我正在关注本教程阅读 Android 中 URL 的内容,但我收到错误并且应用程序崩溃。我创建了一个类并添加了当前方法:
public void readStartParameters(Activity start) throws IOException{
URL yahoo = new URL("http://it.yahoo.com");
BufferedReader in = new BufferedReader(
new InputStreamReader(
yahoo.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
// System.out.println(inputLine);
Log.i("INPUTLINE",""+inputLine);
in.close();
}
在活动类中,我在 Resume 方法中有以下代码:
try {
webCommunication.readStartParameters(this);
} catch (IOException e) {
Log.i("","ERROR");
}
当我启动应用程序时它崩溃并给我这个错误:
09-17 14:50:40.815: E/AndroidRuntime(2203): FATAL EXCEPTION: main
09-17 14:50:40.815: E/AndroidRuntime(2203): java.lang.RuntimeException: Unable to resume activity {com.example.dilandprints2/com.example.dilandprints2.DiLandPrints}: android.os.NetworkOnMainThreadException
09-17 14:50:40.815: E/AndroidRuntime(2203): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
09-17 14:50:40.815: E/AndroidRuntime(2203): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
09-17 14:50:40.815: E/AndroidRuntime(2203): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
09-17 14:50:40.815: E/AndroidRuntime(2203): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-17 14:50:40.815: E/AndroidRuntime(2203): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-17 14:50:40.815: E/AndroidRuntime(2203): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 14:50:40.815: E/AndroidRuntime(2203): at android.os.Looper.loop(Looper.java:137)
09-17 14:50:40.815: E/AndroidRuntime(2203): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-17 14:50:40.815: E/AndroidRuntime(2203): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 14:50:40.815: E/AndroidRuntime(2203): at java.lang.reflect.Method.invoke(Method.java:511)
09-17 14:50:40.815: E/AndroidRuntime(2203): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-17 14:50:40.815: E/AndroidRuntime(2203): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-17 14:50:40.815: E/AndroidRuntime(2203): at dalvik.system.NativeStart.main(Native Method)
09-17 14:50:40.815: E/AndroidRuntime(2203): Caused by: android.os.NetworkOnMainThreadException
09-17 14:50:40.815: E/AndroidRuntime(2203): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-17 14:50:40.815: E/AndroidRuntime(2203): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-17 14:50:40.815: E/AndroidRuntime(2203): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-17 14:50:40.815: E/AndroidRuntime(2203): at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-17 14:50:40.815: E/AndroidRuntime(2203): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
09-17 14:50:40.815: E/AndroidRuntime(2203): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
09-17 14:50:40.815: E/AndroidRuntime(2203): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
09-17 14:50:40.815: E/AndroidRuntime(2203): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
09-17 14:50:40.815: E/AndroidRuntime(2203): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
09-17 14:50:40.815: E/AndroidRuntime(2203): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
09-17 14:50:40.815: E/AndroidRuntime(2203): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
09-17 14:50:40.815: E/AndroidRuntime(2203): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
09-17 14:50:40.815: E/AndroidRuntime(2203): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
09-17 14:50:40.815: E/AndroidRuntime(2203): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
09-17 14:50:40.815: E/AndroidRuntime(2203): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
09-17 14:50:40.815: E/AndroidRuntime(2203): at java.net.URL.openStream(URL.java:462)
09-17 14:50:40.815: E/AndroidRuntime(2203): at com.example.dilandprints2.WebCommunication.readStartParameters(WebCommunication.java:19)
09-17 14:50:40.815: E/AndroidRuntime(2203): at com.example.dilandprints2.DiLandPrints.onResume(DiLandPrints.java:53)
09-17 14:50:40.815: E/AndroidRuntime(2203): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
09-17 14:50:40.815: E/AndroidRuntime(2203): at android.app.Activity.performResume(Activity.java:5182)
09-17 14:50:40.815: E/AndroidRuntime(2203): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)
知道发生了什么吗?我在我的代码中遗漏了什么吗?
谢谢