gpt4 book ai didi

java - 读取远程txt文件android

转载 作者:行者123 更新时间:2023-11-29 03:38:53 24 4
gpt4 key购买 nike

我有这段代码可以读取远程 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 stack trace,但我认为是下面的,如果我错了告诉我

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

最佳答案

由于您没有向我们提供堆栈跟踪信息,因此我在给出这个答案时进行了一些猜测。

我猜你得到的是 NetworkOnMainThreadException .当您尝试在主 UI 线程上使用网络时,此异常发生在 Android 3.0 及更高版本上。这也解释了为什么在 Gingerbread (Android 2.3) 上看不到异常。

要解决此问题,请将所有网络代码移至 ThreadAsyncTask .

关于java - 读取远程txt文件android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14104516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com