Android, Open URL in a WebView after checking that URL exists -
hello writing simple code in order open webpage on android device. fine when trying check whether url exists or not getting same error url not exist. appreciated. many thanks.
so here code :
public class mainactivity extends activity { public static final string siteurl = "google.com"; @override public void oncreate(bundle savedinstancestate) { final context context = this; super.oncreate(savedinstancestate); setcontentview(r.layout.main); // check internet connectivity if (!exists(siteurl)) { toast.maketext(getapplicationcontext(), "this site doesnt exist", toast.length_long).show(); timedexit(); } else { // launch site startactivity(new intent(context, webviewactivity.class)); } } public static boolean exists(string urlname) { try { httpurlconnection.setfollowredirects(false); // note : may need // httpurlconnection.setinstancefollowredirects(false) httpurlconnection con = (httpurlconnection) new url(urlname).openconnection(); con.setrequestmethod("head"); return (con.getresponsecode() == httpurlconnection.http_ok); } catch (exception e) { e.printstacktrace(); return false; } } public void timedexit() { timertask exitapp = new timertask() { @override public void run() { system.exit(0); } }; timer timer = new timer(); timer.schedule(exitapp, new date(system.currenttimemillis() + 7000)); } } and webviewactivity:
public class webviewactivity extends activity { private webview webview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.webview); webview = (webview) findviewbyid(r.id.webview1); webview.getsettings().setjavascriptenabled(true); // todo: check warning webview.loadurl(mainactivity.siteurl); webview.setwebviewclient(new webviewclient()); } } so problem here everytime program runs returns url not exist. grateful. thank you.
if trying gain internet access within app should add proper permission tag in manifest file, preferably before application tag.
in case:
<uses-permission android:name="android.permission.internet" /> as showed in this thread.
i know late, hope helps.
Comments
Post a Comment