java - Correct URL conditions -


in app have got edittext, user can put link show photo.

in activity have got button, when user clicks it, can see website (url put in edittext).

but there 1 problem: application can open link when has got "http://www.".. else app crashed

i think can 4 situations:

  1. user puts www.vebsite.com without http://

  2. user puts corrective url this: http://www.website.com

  3. user puts site domain this: website.com without http://www.
  4. user puts space in url (it may mistake)

now have conditions this:

if (string.valueof(text_of_url).contains("www")) { string full_url = string.valueof(text_of_url).replace("www.", http://www.");      }  else if (string.valueof(text_of_url).contains(".")) {  string full_url = string.valueof(text_of_url).replace(" ", ""); } 

but works not correctly (i'm new in java).

how can correctly conditions?

you can use this:

patterns.web_url.matcher(yoururl).matches() 

if url correct, return true. else, return false

try method:

private boolean isurlvalid(string url){     if(patterns.web_url.matcher(url).matches()){         return true;     }     //show warning message or whatever want     return false; } 

you cannot check every possibility if conditions.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -