So, I need to encode my url.
For that I use Uri.encode():
private const val CHARS= "##&=*+-_.,:!?()/~'%"
if (query != null) {
query = Uri.encode(query, Chars)
}
But, it encodes weirdly... [ is %255B, when it should be and ] is %255D, when it should be %5D
Update: Turns out Uri.encode() works just fine. The problem is how I build the url. I do it by using HttpUrl and after I encode query I do HttpUrl.build() which encodes the url second time?
URLEncoder.encode(query, "UTF-8");
The URL encoding is can be Percent-encoding which encodes with %. Provide "UTF-8" in chars it will work. Hope this will work for you.
You seems to be calling android.net.Uri's:
public static String encode (String s, String allow)
From the documentation, this:
Encodes characters in the given string as '%'-escaped octets using the
UTF-8 scheme. Leaves letters ("A-Z", "a-z"), numbers ("0-9"), and
unreserved characters ("_-!.~'()*") intact. Encodes all other
characters with the exception of those specified in the allow
argument.
So you need pass the URL string as the s parameter. And it will return an encoded version of the url suitable for use as a URI component.
Related
I have a very simple WebView that needs to display text as HTML. I retrieve data from a server that is in a byte array. I need to display that byte array as readable text.
var wv = FindViewById<WebView>(Resource.Id.webview);
var container = AutoFac.Container;
_routinesService = container.Resolve<IRoutinesService>();
byte[] documentHtml = _routinesService.GetFragment(documentId);
string mimeType = "text/html";
string encoding = "utf-8";
var html = System.Text.Encoding.UTF8.GetString(documentHtml);
wv.LoadDataWithBaseURL("", html, mimeType, encoding, "");
I try to convert the byte array to a String, and the variable html becomes \"U3RlcCAxOiBZb3UgYXJlIGEgd2lubmVyLg==\"
This looks like Base64 to me, and when I use an online converter (https://www.base64decode.org/), it does convert to the correct text. But the output on the screen in the WebView just looks like "U3RlcCAxOiBZb3UgYXJlIGEgd2lubmVyLg=="
Any ideas what I might be missing?
Just for more information, this is in Xamarin.Android so the code is C# instead of Java (but there is not much difference).
Have you tried javascript
atob()
method that decodes a string of data which has been encoded using base-64 encoding before posting it to webview.
I have this string which I'm trying to format:
String url = "http://api/doSomething.json?params%5Bemail%5D=%s"
String.format(url,email).
The idea is that it ends up looking like this:
http://api/doSomething.json?params[email]=aValue;
I'm currently getting a MissingFormatArgumentException, Format specifier: 5D exception.
Has anyone had issues with this before?
String.format() doesn't like the %5D placeholder - %5D has to be %5d.
Reference: http://developer.android.com/reference/java/util/Formatter.html
... if it was about placeholders.
Anyway, it seems you just want the square brackets.
Therefore, change this
String url = "http://api/doSomething.json?params%5Bemail%5D=%s"
to
String url = "http://api/doSomething.json?params[email]=%s"
In the end i was able to resolve this using a URLEncoder.
This post was particularly helpful -> URL encoding in Android
String queryPart = String.format(PARAM_STRING,
email);
return baseUrl + URLEncoder.encode(queryPart, "utf-8");
My Android app needs to connect to a webservice that will decode a unicode string using utf8_decode. How can I encode my string in my application in a similar way as php utf8_encode?
I have found CharsetEncoder but am not sure how to use it.
Thanks for your advice!
You can do this using just the String class, but just a note about converting text to/from UTF and ISO. When decoding from UTF-8 to ISO-8859-1 will cause "replacement characters" (�) to appear in your text when unsupported characters are found.
To encode texts:
byte[] utf8 = new String(latin1, "ISO-8859-1").getBytes("UTF-8");
or
byte[] latin1 = new String(utf8, "UTF-8").getBytes("ISO-8859-1");
I am trying to open an URL with androids MediaPlayer Class by using:
MediaPlayer.create(pContext, Uri.parse(m_sUrl));
i have allready replaced all the Spaces in m_sUrl String with %20 by using:
m_sUrl = m_sUrl.replace(" ", "%20");
But the MediaPlayer.Create Method return null to me. So there seems still to be something wrong by parsing the m_sUrl String.
Thats the URL string i am trying to stream:
http://www.se.hs-heilbronn.de/~poneu/files/musik/oeffentlich/2007-03-10/01%20-%20L.v.%20Beethoven-%20Ouvertüre%20Nr.%203%20zur%20Oper%20-Leonore-%20op.%2072a.mp3
so as you can see, it seems to be the ü character. Anyone know what i have to use in an valid url for ä ö ü ß and so on?
Use URLEncoder and you have to keep in mind to encode only unsafe character:
String query = URLEncoder.encode("depeche mode", "utf-8");
String url = "http://stackoverflow.com/search?q=" + query;
in the exemple that i mentioned below the unsafe character is the space that will be transformed to %20
hope this will help
use URLEncoder instead of manually replacing instances of single characters.
In my app I receive a URL such as
http://www.wassersportlotse.de/php/lib/smart_image_resizer/image.php/Mühlendammschleuse.jpg?image=/media/images/uploads/Mühlendammschleuse.jpg
When there are no German characters in the fullurl I can just use it without encoding and it works fine. However if I receive a URL such as the one above it doesn't work (the ü is causing the problem). Below I have tried to encode the seperate parts of the URI to no avail. As alway advice is very much appreciated.
public ImageDownloader(String fullurl) throws URISyntaxException{
URI uri = new URI(fullurl);
path = uri.getPath();
path = URLEncoder.encode(path);
query = uri.getQuery();
query = URLEncoder.encode(query);
auth = uri.getAuthority();
url = "http://" + auth + path + query;
}
Maybe the encoder das encode the Umlaut as UTF-8 characters (so ü would be encoded with two characters) and they are not put back together properly at the server (for us it didn't work with Tomcat). To solve this situation we used URLEncoder.encode(param, "ISO-8859-1") to encode the parameters.
There's no simple answer, because it depends on the server serving that URI which encoding is expected.
Usually it's UTF-8.
In that case: use String.getBytes, specifying the UTF-8 encoding, and obtain a byte array from that. Re-encode that byte array as string by taking all bytes <= 127 as-is, and substituting all others by the %hh form. (percent sign, then two hex digits). See http://greenbytes.de/tech/webdav/rfc3986.html#rfc.section.2.1.
You can use Android's Uri class to help you out. That class has an encode() method which will use UTF-8 to encode your string.
I recently had a problem with URLs for images whose names included umlauts and German special characters, and I lost a day looking for the solution. The images simply did not appear if there was an ä or and ü in the file name or the directory name. I thought it might be spring, or some other Java technology I am working with, or in the browser. And strangely enough, even with the url encoded, it failed to find the image. But in the end, the solution was in my tomcat server.xml configuration. In your server.xml file, find your connector and add these two lines:
URIEncoding="UTF-8"
useBodyEncodingForURI="true"
At the end, it should look something like this:
<Connector connectionTimeout="20000"
port="8080"
protocol="HTTP/1.1"
redirectPort="8443"
URIEncoding="UTF-8"
useBodyEncodingForURI="true"/>
Now I do not need to url-encode the url. This is a help to my clients, because they can see the German words in the urls spelled correctly.
Here is another tip: if you are coding in eclipse and starting and stopping your server from inside eclipse, then the configuration file (server.xml) could be in your eclipse workspace in the Servers folder. It must be changed here for it to work with eclipse. This can be maddening, when you have made the change in your principal tomcat configuration, and the urls work there, but they are still broken when running the server in eclipse.
That did it for me. I hope it helps someone out there! :-)
Have your tried unsing:
android.net.Uri.encode(urlString, ":/");
It encodes the string but skips ":" and "/".