Reputation: 389
I am trying to read a text file from an URL
into a String
in Android. I have tried multiple codes, and I am currently using this one:
BufferedReader in = new BufferedReader(new InputStreamReader(new URL("https://wordpress.org/plugins/about/readme.txt").openStream()));
Every single code has given me an IllegalStateException
in this line.
I have the following permissions:
<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" />
I hope someone can give me an explanation why this is happening. Here is the full error:
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
Upvotes: 0
Views: 434
Reputation: 68975
Issue is exactly what exception states
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1166)
I think from Android Honeycomb version you cannot perform network operation on main UI thread. Consider using something like AsyncTask
instead for your network operations (Docs).
You can also turn off Strict mode
as a workaround but it is not recommended.
StrictMode.ThreadPolicy newPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(newPolicy );
Upvotes: 3