How to set a toast to java.net.UnknownHostException in android -
in emulator, error when internet connection turned off:
java.net.unknownhostexception: unable resolve host "url"; no address associated hostname.
but want show toast instead of revealing hosting url upon onclick of login button prevent security issues. please :)
public class newloginactivity extends appcompatactivity implements view.onclicklistener { private edittext edittextusername, edittextpassword; private button buttonlogin; private progressdialog progressdialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.new_login); if (sharedprefmanager.getinstance(this).isloggedin()) { finish(); startactivity(new intent(this, admingmenuactivity.class)); return; } edittextusername = (edittext)findviewbyid(r.id.edittextusername); edittextpassword = (edittext)findviewbyid(r.id.edittextpassword); buttonlogin = (button)findviewbyid(r.id.buttonlogin); progressdialog = new progressdialog(this); progressdialog.setmessage("please wait..."); buttonlogin.setonclicklistener(this); } private void userlogin() { final string username = edittextusername.gettext().tostring().trim(); final string password = edittextpassword.gettext().tostring().trim(); progressdialog.show(); stringrequest stringrequest = new stringrequest( request.method.post, constants.url_login, new response.listener<string>() { @override public void onresponse(string response) { progressdialog.dismiss(); try { jsonobject obj = new jsonobject(response); if (!obj.getboolean("error")) { sharedprefmanager.getinstance(getapplicationcontext()) .userlogin( obj.getint("id"), obj.getstring("username"), obj.getstring("email") ); startactivity(new intent(getapplicationcontext(), admingmenuactivity.class)); finish(); } else { toast.maketext( getapplicationcontext(), obj.getstring("message"), toast.length_long ).show(); } } catch (jsonexception e) { e.printstacktrace(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { progressdialog.dismiss(); toast.maketext( getapplicationcontext(), error.getmessage(), toast.length_long ).show(); } } ) { @override protected map<string, string> getparams() throws authfailureerror { map<string, string> params = new hashmap<>(); params.put("username", username); params.put("password", password); return params; } }; requesthandler.getinstance(this).addtorequestqueue(stringrequest); } @override public void onclick(view view) { if (view == buttonlogin) { userlogin(); } } }
use try-catch block catch exception
try { //your code login goes here.... } catch (unknownhostexception e) { toast.maketext(getapplicationcontext(), "unable connect!", toast.length_short).show(); }
Comments
Post a Comment