Decode HTML Escape Characters [duplicate] - java

This question already has answers here:
How to do URL decoding in Java?
(11 answers)
Closed 7 years ago.
I'm downloading a JSON from the Google Directions API. For the HTML_Instructions field, representing the actual instruction needed for navigation, here is the format:
"Head \u003cb\u003esoutheast\u003c/b\u003e on \u003cb\u003eMinor Ave\u003c/b\u003e toward \u003cb\u003eMadison St\u003c/b\u003e",
Is there a way to decode/remove the escape characters from the String that is downloaded in Java/an Android application.
Thanks for the help.

In Java use
String result = java.net.URLDecoder.decode(url, "UTF-8");
In JS use decodeURIComponent
document.write(decodeURIComponent("Head \u003cb\u003esoutheast\u003c/b\u003e on \u003cb\u003eMinor Ave\u003c/b\u003e toward \u003cb\u003eMadison St\u003c/b\u003e"))

Related

Read a text file with Java [duplicate]

This question already has answers here:
Reading text file always returns 0 - Java
(2 answers)
Closed 7 years ago.
I have a game client jar that has an updater. Its client.jar and it downloads a cache which has all the game files. There is no need to update the client.jar just the cache, but the problem is the version number is hardcoded into the client.
What I've been trying to do ALL day is change the way the version number is obtained, and I just can't get it.
http://pastebin.com/Z5urTUVw
Line 31 is the version number, I need that to read a version number from a dropbox link. Found here - https://dl.dropboxusercontent.com/u/87363031/version.txt
Just 1 simple number, I've been trying to do this for the better part of 6 hours now and I'm bout to lose my cool. Can someone please help me with this?
If you can use Apache Commons IO, there's a very simple way to achieve this using IOUtils:
InputStream in = new URL("https://dl.dropboxusercontent.com/u/87363031/version.txt").openStream();
try {
String version = IOUtils.toString(in));
System.out.println(version);
} finally {
IOUtils.closeQuietly(in);
}

Check String is URL in android code? [duplicate]

This question already has answers here:
How to check if URL is valid in Android
(12 answers)
Closed 8 years ago.
I allow user to input text string in my EditView. When I get Editview text, I want to validate if it is URL link or not? Can any one give Android code function? Thanks
Use WEB_URL pattern in Patterns (android.util.Patterns) Class
Patterns.WEB_URL.matcher(inputTextForURLEdittext.toLowerCase()).matches();
It will return true if URL is valid and false if URL is invalid.
For more info refer to this link
http://developer.android.com/reference/android/util/Patterns.html
You can check it by using URLUtil .
if (URLUtil.isValidUrl(urlString)) {
// URL is valid
}
Hope it will be work for you.

Simplest way to make and parse json request android [duplicate]

This question already has answers here:
Retrieving JSON from URL on Android
(6 answers)
Closed 8 years ago.
I need to use a JSON api, but don't know how to do so. All the tutorials i have found are about parsing json from a file.
I need to make a request like this: http://expandurl.appspot.com/expand?url=http%3A%2F%2Ft.co%2FgLP0Ucg
and then it will return this
{"status": "OK", "end_url": "http://www.systemcomic.com/2011/06/27/the-system-506-office-forecast/", "redirects": 3, "urls": ["http://t.co/gLP0Ucg", "http://www.notquitewrong.com/rosscottinc/2011/06/27/the-system-506-office-forecast/", "http://www.systemcomic.com//2011/06/27/the-system-506-office-forecast/", "http://www.systemcomic.com/2011/06/27/the-system-506-office-forecast/"], "start_url": "http://t.co/gLP0Ucg"}
I just need the end_url string. How would i do this? I am also going to be doing this in a ASyncTask
For getting the JSON you need to extract it using http request and after you get the response you then parse it to get the end_url.
try this to parse end_url:
final Pattern pattern = Pattern.compile("\"end_url\": \"(.+?)\",");
final Matcher matcher = pattern.matcher(You_json_string);
matcher.find();
Log.d("end_url" , matcher.group(1));

Java URL Encode how to encode Hello World as Hello%20World [duplicate]

This question already has answers here:
URLEncoder not able to translate space character
(19 answers)
Closed 9 years ago.
Hi Um quite new to Java and I want to encode Hello World it to Hello%20World . But when I use URLEncoder.encode it will encode the string as Hello+World and when we try to pass it via a Request to IIS it ll consider as a threat. How to encode as i mentioned ? Thank you in advance.
This only works for real URLs
URL u = new URL("Hello World");
String path = u.getPath();

How to validate URL in java using regex? [duplicate]

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 9 years ago.
I want to validate url started with http/https/www/ftp and checks for /\ slashes and checks for .com,.org etc at the end of URL using regular expression. Is there any regex patttern for URL validation?
This works:
Pattern p = Pattern.compile("(#)?(href=')?(HREF=')?(HREF=\")?(href=\")?(http://)?[a-zA-Z_0-9\\-]+(\\.\\w[a-zA-Z_0-9\\-]+)+(/[#&\\n\\-=?\\+\\%/\\.\\w]+)?");
Matcher m = p.matcher("your url here");
I am use the following code for that
String lRegex = "^(https?|ftp|file)://[-a-zA-Z0-9+&##/%?=~_|!:,.;]*[-a-zA-Z0-9+&##/%=~_|]";
btw a search in google and you would find the solution by yourself.

Categories