get HTML code of website from android app -
i developing first android application.
i try html source website, use code:
httpclient client = new defaulthttpclient(); httpget request = new httpget(urlweb); httpresponse response = client.execute(request); string html = ""; inputstream in = response.getentity().getcontent(); bufferedreader reader = new bufferedreader(new inputstreamreader(in)); stringbuilder str = new stringbuilder(); string line = null; while((line = reader.readline()) != null) { str.append(line); } in.close(); html = str.tostring();
also add permissions internet in manifest.
but obtain error:
05-02 18:56:56.967: e/androidruntime(17659): fatal exception: main 05-02 18:56:56.967: e/androidruntime(17659): java.lang.illegalstateexception: not execute method of activity 05-02 18:56:56.967: e/androidruntime(17659): @ android.view.view$1.onclick(view.java:3597) 05-02 18:56:56.967: e/androidruntime(17659): @ android.view.view.performclick(view.java:4202) 05-02 18:56:56.967: e/androidruntime(17659): @ android.view.view$performclick.run(view.java:17340) 05-02 18:56:56.967: e/androidruntime(17659): @ android.os.handler.handlecallback(handler.java:725) 05-02 18:56:56.967: e/androidruntime(17659): @ android.os.handler.dispatchmessage(handler.java:92) 05-02 18:56:56.967: e/androidruntime(17659): @ android.os.looper.loop(looper.java:137) 05-02 18:56:56.967: e/androidruntime(17659): @ android.app.activitythread.main(activitythread.java:5191) 05-02 18:56:56.967: e/androidruntime(17659): @ java.lang.reflect.method.invokenative(native method) 05-02 18:56:56.967: e/androidruntime(17659): @ java.lang.reflect.method.invoke(method.java:511) 05-02 18:56:56.967: e/androidruntime(17659): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:795) 05-02 18:56:56.967: e/androidruntime(17659): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:562) 05-02 18:56:56.967: e/androidruntime(17659): @ dalvik.system.nativestart.main(native method) 05-02 18:56:56.967: e/androidruntime(17659): caused by: java.lang.reflect.invocationtargetexception 05-02 18:56:56.967: e/androidruntime(17659): @ java.lang.reflect.method.invokenative(native method) 05-02 18:56:56.967: e/androidruntime(17659): @ java.lang.reflect.method.invoke(method.java:511) 05-02 18:56:56.967: e/androidruntime(17659): @ android.view.view$1.onclick(view.java:3592) 05-02 18:56:56.967: e/androidruntime(17659): ... 11 more 05-02 18:56:56.967: e/androidruntime(17659): caused by: android.os.networkonmainthreadexception 05-02 18:56:56.967: e/androidruntime(17659): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1117) 05-02 18:56:56.967: e/androidruntime(17659): @ java.net.inetaddress.lookuphostbyname(inetaddress.java:385) 05-02 18:56:56.967: e/androidruntime(17659): @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236) 05-02 18:56:56.967: e/androidruntime(17659): @ java.net.inetaddress.getallbyname(inetaddress.java:214) 05-02 18:56:56.967: e/androidruntime(17659): @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:137) 05-02 18:56:56.967: e/androidruntime(17659): @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 05-02 18:56:56.967: e/androidruntime(17659): @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 05-02 18:56:56.967: e/androidruntime(17659): @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) 05-02 18:56:56.967: e/androidruntime(17659): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) 05-02 18:56:56.967: e/androidruntime(17659): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 05-02 18:56:56.967: e/androidruntime(17659): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) 05-02 18:56:56.967: e/androidruntime(17659): @ com.example.icpcgb.mainactivity.btn_show(mainactivity.java:72)
wich problem?
thank much
caused by: android.os.networkonmainthreadexception
you trying perform network operation on ui thread, android 3+ devices don't allow network operation on ui thread. throw networkonmainthreadexception
, use asynctask
or separate thread
perform network operations
Comments
Post a Comment