0
我想從URL
讀取文本文件到String
在Android中。我曾嘗試多個代碼,和我目前正在使用這一個:從URL讀取文本文件給出IllegalStateException
BufferedReader in = new BufferedReader(new InputStreamReader(new URL("https://wordpress.org/plugins/about/readme.txt").openStream()));
每一個代碼給了我一個IllegalStateException
在這條線上。 我有以下權限:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我希望有人能給我爲什麼發生這種情況的解釋。 以下是完整的錯誤:
FATAL EXCEPTION: main
Process: com.example.MyApp, PID: 15407
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3996)
at android.view.View.performClick(View.java:4710)
at android.view.View$PerformClick.run(View.java:19510)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5679)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3991)
... 11 more
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1166)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:409)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:362)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:308)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)
at java.net.URL.openStream(URL.java:470)
at com.example.MyApp.MainActivity.search(MainActivity.java:60)
... 14 more
謝謝,關閉嚴格模式仍然無效,但我會嘗試使用AsyncTask。 – user2988879