java - how to use try catch instead force close? -
i have code in android , java. want show dialog user when internet connection has problems or website inaccessible. try put code try-catch
block yet when internet connection has problem app closed. want show message user, not close app.
httppost ht = new httppost("http://yahoo.com"); httpclient hc = new defaulthttpclient(); httpresponse hr = null; try { hr = hc.execute(ht); } catch (clientprotocolexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } string htmltemp = ""; try { httpentity = hr.getentity(); htmltemp = new string(entityutils.tostring(he)); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); }
you can try add unknownhostexception
catch, or can use method before calling request :
public static boolean hasconnection() { connectivitymanager cm = (connectivitymanager) mbridgeapp.getcontext().getsystemservice( context.connectivity_service); networkinfo wifinetwork = cm.getnetworkinfo(connectivitymanager.type_wifi); if (wifinetwork != null && wifinetwork.isconnected()) { return true; } networkinfo mobilenetwork = cm.getnetworkinfo(connectivitymanager.type_mobile); if (mobilenetwork != null && mobilenetwork.isconnected()) { return true; } networkinfo activenetwork = cm.getactivenetworkinfo(); if (activenetwork != null && activenetwork.isconnected()) { return true; } return false; }
Comments
Post a Comment