How to check a specific URL is valid or not in Android? -


i creating class check whether given url valid or not, not getting proper idea how call class other class, because in project, have check url response in every class. code is:`

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     connectivitymanager cm = (connectivitymanager) getsystemservice(context.connectivity_service);     networkinfo netinfo = cm.getactivenetworkinfo();     if (netinfo != null && netinfo.isconnected()) {         mytask task = new mytask();         task.execute();     } }  private class mytask extends asynctask<void, void, boolean> {      @override     protected boolean doinbackground(void... params) {          try {              url url = new url("http://www.google.com");             httpurlconnection urlc = (httpurlconnection) url.openconnection();             urlc.setconnecttimeout(10 * 1000);             urlc.connect();             if (urlc.getresponsecode() == 200) {                 log.wtf("connection", "success !");                 return true;             } else {                 return false;             }         } catch (malformedurlexception e1) {             return false;         } catch (ioexception e) {             return false;         }     }      @override     protected void onpostexecute(boolean result) {         boolean bresponse = result;     } } 

your there!

private class mytask extends asynctask<void, void, boolean> {     private url murl;      public mytask(url url) {         murl = url;     }      @override     protected boolean doinbackground(void... params) {         try {             httpurlconnection urlc = (httpurlconnection) murl.openconnection();             urlc.setconnecttimeout(10 * 1000);             urlc.connect();             if (urlc.getresponsecode() == 200) {                 log.wtf("connection", "success !");                 return true;             } else {                 return false;             }         } catch (malformedurlexception e1){             return false;         } catch (ioexception e) {             return false;         }     } } 

when need check respons:

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     connectivitymanager cm = (connectivitymanager) getsystemservice(context.connectivity_service);      networkinfo netinfo = cm.getactivenetworkinfo();     if (netinfo != null && netinfo.isconnected()) {         url url = new url("http://www.google.com");         mytask task = new mytask(url){             @override             protected void onpostexecute(boolean result) {                if (result) {                    // work here                }             }         };         task.execute();     } } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -