我有这段代码来读取远程txt文件的最后一行:
try {
URL url = new URL("http://intram.cluny.ensam.fr/meteo/downld02.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
while(str != null) {
ligne=str;
str = in.readLine();
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
此代码在我的LG Optimus L3(Android 2.3.6)上工作得非常好,但在我的Nexus 7(Android 4.2.1)上开始崩溃。为什么?如何解决?
编辑: 谢谢你的回答
我不知道什么是logcat堆栈跟踪,但我认为它是以下,告诉我,如果我错了
12-31 18:54:07.469: W/ActivityThread(29021): Application fr.ensam.cluny can be debugged on port 8100...
12-31 18:54:07.969: D/AndroidRuntime(29021): Shutting down VM
12-31 18:54:07.969: W/dalvikvm(29021): threadid=1: thread exiting with uncaught exception (group=0x41f6c930)
12-31 18:54:07.969: E/AndroidRuntime(29021): FATAL EXCEPTION: main
12-31 18:54:07.969: E/AndroidRuntime(29021): java.lang.RuntimeException: Unable to start activity ComponentInfo{fr.ensam.cluny/fr.ensam.cluny.MainActivity}: android.os.NetworkOnMainThreadException
12-31 18:54:07.969: E/AndroidRuntime(29021): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-31 18:54:07.969: E/AndroidRuntime(29021): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-31 18:54:07.969: E/AndroidRuntime(29021): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-31 18:54:07.969: E/AndroidRuntime(29021): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-31 18:54:07.969: E/AndroidRuntime(29021): at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 18:54:07.969: E/AndroidRuntime(29021): at android.os.Looper.loop(Looper.java:137)
12-31 18:54:07.969: E/AndroidRuntime(29021): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-31 18:54:07.969: E/AndroidRuntime(29021): at java.lang.reflect.Method.invokeNative(Native Method)
12-31 18:54:07.969: E/AndroidRuntime(29021): at java.lang.reflect.Method.invoke(Method.java:511)
12-31 18:54:07.969: E/AndroidRuntime(29021): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-31 18:54:07.969: E/AndroidRuntime(29021): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-31 18:54:07.969: E/AndroidRuntime(29021): at dalvik.system.NativeStart.main(Native Method)
12-31 18:54:07.969: E/AndroidRuntime(29021): Caused by: android.os.NetworkOnMainThreadException
12-31 18:54:07.969: E/AndroidRuntime(29021): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-31 18:54:07.969: E/AndroidRuntime(29021): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
12-31 18:54:07.969: E/AndroidRuntime(29021): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
12-31 18:54:07.969: E/AndroidRuntime(29021): at java.net.InetAddress.getAllByName(InetAddress.java:214)
12-31 18:54:07.969: E/AndroidRuntime(29021): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
12-31 18:54:07.969: E/AndroidRuntime(29021): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
12-31 18:54:07.969: E/AndroidRuntime(29021): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
12-31 18:54:07.969: E/AndroidRuntime(29021): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
12-31 18:54:07.969: E/AndroidRuntime(29021): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
12-31 18:54:07.969: E/AndroidRuntime(29021): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
12-31 18:54:07.969: E/AndroidRuntime(29021): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
12-31 18:54:07.969: E/AndroidRuntime(29021): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
12-31 18:54:07.969: E/AndroidRuntime(29021): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
12-31 18:54:07.969: E/AndroidRuntime(29021): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
12-31 18:54:07.969: E/AndroidRuntime(29021): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
12-31 18:54:07.969: E/AndroidRuntime(29021): at java.net.URL.openStream(URL.java:462)
12-31 18:54:07.969: E/AndroidRuntime(29021): at fr.ensam.cluny.MainActivity.onCreate(MainActivity.java:61)
12-31 18:54:07.969: E/AndroidRuntime(29021): at android.app.Activity.performCreate(Activity.java:5104)
12-31 18:54:07.969: E/AndroidRuntime(29021): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-31 18:54:07.969: E/AndroidRuntime(29021): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-31 18:54:07.969: E/AndroidRuntime(29021): ... 11 more
答案 0 :(得分:4)
由于你没有给我们一个堆栈跟踪,我给出了一个猜测的答案。
我的猜测是你得到NetworkOnMainThreadException
。当您尝试在主UI线程上使用网络时,会在Android 3.0及更高版本上发生此异常。这也解释了为什么你没有在Gingerbread(Android 2.3)上看到例外。