2015-04-25 45 views
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 

回答

3

的問題是什麼異常狀態

Caused by: android.os.NetworkOnMainThreadException 
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1166) 

我想從Android的Honeycomb版本,你不能在主UI線程進行網絡操作。考慮使用類似AsyncTask的替代方法代替您的網絡操作(Docs)。

您也可以關閉Strict mode作爲解決方法,但不推薦。

StrictMode.ThreadPolicy newPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
StrictMode.setThreadPolicy(newPolicy); 
+0

謝謝,關閉嚴格模式仍然無效,但我會嘗試使用AsyncTask。 – user2988879