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:
user puts www.vebsite.com without http://
user puts corrective url this: http://www.website.com
- user puts site domain this: website.com without http://www.
- 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
Post a Comment