I want to check if the input received is url using REGEX. How can i do it ? Is there any standard REGEX available that works for checking url?
my requirement is to check if the input text is a url or not, but not if the text contains a url.
you can check without regex in android
android.util.Patterns.WEB_URL.matcher(linkUrl).matches();
I would strongly recommend not using a Regex in this situation as there are a lot of different cases that can constitute a valid URL (see the top voted answer to this question for an example of an RFC valid regex). If working on Android I would recommend using the Uri class;
String urlToValidate = "http://google.co.uk/search";
Uri uri = Uri.parse(urlToValidate);
You can then look at the different parts of the URL to ensure that you are willing to accept the URL input. For example;
uri.getScheme(); // Returns "http", if you only want an HTTP url
uri.getHost(); // Returns "google.co.uk", if you only want a specific domain
uri.getPath(); // Returns "/search"
Related
I'm using Kotlin and retrofit in my Android App to call API's, but some of the API URLs have these characters in them: ^, #, ~.
I'm using the GET API method. My host URL does not have special / strange characters in them nor does my API method names, but the API methods that I've set up; have a few parameters; and sometimes the arguments I'm sending, when calling the API methods, have these special characters in them.
In those cases I'm not getting a response back and the API call fails.
I've noticed that retrofit changes my URL. It replaces any special character with 3 other characters. I've tested my URLs directly in a browser and in Postman, and they work fine.
Is there something specific I have to do in Kotlin to make retrofit be OK with my special characters?
try this
import java.net.URLEncoder
fun main(args: Array<String>) {
val url = "http://foo bar/"
println(URLEncoder.encode(url, "utf-8")) // note: encodes space to + not %20
}
Output:
http%3A%2F%2Ffoo+bar%2F
You should avoid Special character in Api
URL encoding is often required to convert special characters (such as "/", "&", "#", ...), because special characters:
1. Have special meaning in some contexts, or
2. Are not a valid character for an URL, or
3. could be altered during the transfer.
For instance, the "#" character needs to be encoded because it has a special meaning of that of an HTML anchor.
The character needs to be encoded because it is not a valid URL character. Also, some characters, such as "~" might not transport properly across the internet. Instead of proceeding with the complex process you should focus on correcting the old one.
More you can read here.
I send an SMS saying "Click on https://www.example.com/?"
When I receive this SMS on an Android phone, only the https://www.example.com is clickable. The "?" is visible in the SMS, but is not part of the clickable URL.
Is there some way to escape the ? or do some other workaround so that click on the link goes to https://www.example.com/?
EDIT: Forget Android - even on this page on stackoverflow - the "?" is not part of the clickable link
Can you replace the question mark (?) by %3F
www.example.com/%3F
See:
https://www.w3schools.com/tags/ref_urlencode.asp
This appears to be correct behaviour (as far as I have observed on other systems) - an empty query string (effectively a dangling question mark) is not necessarily part of the URL so appears as punctuation.
As a workaround my suggestion is to ensure you always provide a parameter:
https://www.example.com/?a=a
Ideally you can find something that makes the link enticing to the user (rather than only confusing):
https://www.example.com/?vip=yes
Because these are not query parameters you are actually using, they should not affect your page processing
Hi you can send by converting to short url by https://goo.gl/ and you can get same as expected .
Okay I wasn't really sure how to word this question, but basically what I want to do is, I got a url from a webView in android, and I need to put part of that url into a string, the url will look something like this: http://localhost/?code=4/3pakksajdfASDFwek.4nsKfAYN7XQVshQV0ieZDAp-PrgEcAI and I only want the part after code=, is that possible? Thanks
int start = my_string.indexOf("=");
String suffix = my_string.substring(start + 1);
If other parameters can be on the URL, or code is not always first parameter:
String url = "http://localhost/?code=4/3pakksajdfASDFwek.4nsKfAYN7XQVshQV0ieZDAp-PrgEcAI";
String code = url.replaceAll(".*(?:[?]|[&])code=([^&]+)","\1");
tests here: http://fiddle.re/nxfv
If you are totally sure the URL never has other form, you can use indexOf and substring.
Otherwise, it is better if you use URI class to extract out the query part of the URL (use getRawQuery just to be safe), then tokenize it with split along & character and find the correct key-value pair to obtain the correct value. This method is not as brittle as indexOf method above.
Hi Everyone,
I am fetching a text from my DB and before inserting the text into db i know that the encoding of text is ISO-8859-1 , but after fetching from db and before loading this text i am checking the encoding through this code
InputStreamReader is = new InputStreamReader(new ByteArrayInputStream(body.getBytes()));
is.getEncoding();
Log.v("encoding", ""+is.getEncoding());
// String body = fetched from db
and i am getting in the log for the encoding of the text is UTF-8. And this text is not getting loaded on the webview with this method :
mailView.loadDataWithBaseURL(null, body, "text/html", "UTF-8", null);
please suggest me a correct way to solve this problem.
This reply is terribly late, but I stumbled on the question via Google and so thought I'd answer.
As described in the JavaDoc, new InputStreamReader(InputStream) will create a reader with the system default (apparently UTF-8). is.getEncoding() is simply returning that default which may or may not match your stored data.
In general, it is a good idea to specify the encoding of your stream explicitly. The implication is that you need to store the encoding along with the content. You can use out of band knowledge (e.g., my application only uses ISO-8859-1) but this will be brittle in the event that you change your chosen encoding in the future.
Since the world isn't always a nice place, and strings get separated from their charsets, you might look into a charset detector. See http://userguide.icu-project.org/conversion/detection as an example.
All I want to do is to send a URL String into my RESTFUL web service with some kind of code like this
URL someURL= new URL("http://myWebService:port/service/"+CharSequence.getText());
Its all going well until I found error with space character in my URL. I found some solution about replacing the space character with %20 which is I already defined with something like this :
URL someURL= new URL("http://myWebService:port/service/"+CharSequence.getText().replace(" ", "%20"));
Everything, again, seems going well until i found that the replace(Char oldChara, Char newChara) function can only replace ONE space character, and not two.
For brief example when I send the CharSequence.getText() with values "We won" there will be no error, but when I change the values into "We won the battles" there will be an error issuing that there are some illegal character sent to my RESTFUL web service.
Any kind of answer will come up with my great thanks and big salute
~Regards~
Use replaceAll instead of replace.
Although, you should really be doing proper URL encoding. You can use URLEncoder.encode
for example.