java - Reading text file from url gives IllegalStateException -
i trying read text file url
string
in android. have tried multiple codes, , using one:
bufferedreader in = new bufferedreader(new inputstreamreader(new url("https://wordpress.org/plugins/about/readme.txt").openstream()));
every single code has given me illegalstateexception
in line. have 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 can give me explanation why happening. here full error:
fatal exception: main process: com.example.myapp, pid: 15407 java.lang.illegalstateexception: not execute method of activity @ android.view.view$1.onclick(view.java:3996) @ android.view.view.performclick(view.java:4710) @ android.view.view$performclick.run(view.java:19510) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:146) @ android.app.activitythread.main(activitythread.java:5679) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1291) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1107) @ dalvik.system.nativestart.main(native method) caused by: java.lang.reflect.invocationtargetexception @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ android.view.view$1.onclick(view.java:3991) ... 11 more caused by: android.os.networkonmainthreadexception @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1166) @ java.net.inetaddress.lookuphostbyname(inetaddress.java:385) @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236) @ java.net.inetaddress.getallbyname(inetaddress.java:214) @ com.android.okhttp.internal.dns$1.getallbyname(dns.java:28) @ com.android.okhttp.internal.http.routeselector.resetnextinetsocketaddress(routeselector.java:216) @ com.android.okhttp.internal.http.routeselector.next(routeselector.java:122) @ com.android.okhttp.internal.http.httpengine.connect(httpengine.java:409) @ com.android.okhttp.internal.http.httpengine.sendsocketrequest(httpengine.java:362) @ com.android.okhttp.internal.http.httpengine.sendrequest(httpengine.java:308) @ com.android.okhttp.internal.http.httpurlconnectionimpl.execute(httpurlconnectionimpl.java:345) @ com.android.okhttp.internal.http.httpurlconnectionimpl.getresponse(httpurlconnectionimpl.java:296) @ com.android.okhttp.internal.http.httpurlconnectionimpl.getinputstream(httpurlconnectionimpl.java:179) @ com.android.okhttp.internal.http.httpsurlconnectionimpl.getinputstream(httpsurlconnectionimpl.java:246) @ java.net.url.openstream(url.java:470) @ com.example.myapp.mainactivity.search(mainactivity.java:60) ... 14 more
issue exception states
caused by: android.os.networkonmainthreadexception @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1166)
i think android honeycomb version cannot perform network operation on main ui thread. consider using asynctask
instead network operations (docs).
you can turn off strict mode
workaround not recommended.
strictmode.threadpolicy newpolicy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(newpolicy );
Comments
Post a Comment