问题描述:

I have this code to read the last line of a distant txt file :

 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) {

}

This code is working very fine on my LG Optimus L3 (Android 2.3.6) but crashes at start on my Nexus 7 (Android 4.2.1). Why ? How to fix it ?

EDIT:

Thanks for your answers

I don't know what is logcat stack trace, but I think it's the following, tell me if I'm wrong

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

网友答案:

Since you've refrained from giving us a stack trace, I'm giving this answer with a bit of guessing.

My guess is you're getting a NetworkOnMainThreadException. This exception occurs on Android 3.0 and above, when you try to use the network on the main UI thread. This also explains why you don't see the exception on Gingerbread (Android 2.3).

To fix this, move all networking code into a Thread or an AsyncTask.

相关阅读:
Top