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.
Related
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.
I get an Address via Geocode which looks like this e.g.: "Downing Street, London" and I build an URL String with this address String and other parameters.
This is how i build the url string:
String url = "http://www.friendlyride.at/...?...&enterstring="+enterstring+"&enterlng="+enterLng+"&enterlat="+enterLat+"&exitstring="+exitstring+"&exitlng="+exitLng+"&exitlat="+exitLat+"&info="+infoinput;
enterstring = "Downing Street, London"
Now when I log the URL (which I then call in a JSON AsyncTask with an HTTPDataHandler), the URL is a link (blue) until an 'ß', space or ',' is in the String.
This is the URL in the Android Monitor (Log.d):
http://www.friendlyride.at/...?...&enterstring=Downing Street, London&enterlng=-0.1272206&enterlat=51.5032077&exitstring=Abbey Rd, London&exitlng=-0.1830032&exitlat=51.5367909&info=info
The whole url should be a link (here it breaks at a space). If I enter the Url manually in the browser, it works with spaces and everything.
So how can i use the whole string as url?
If you need any code, please tell me, I'm not sure what code I should include. :)
Your url needs to be encoded to be valid.
For that purpose and greatly improving your code quality as well, I strongly encourage you using Uri.Builder to build your urls :
Uri.Builder builder = new Uri.Builder();
builder.scheme("http")
.authority("www.friendlyride.at")
.appendQueryParameter("enterstring", enterstring)
.appendQueryParameter("enterlng", Long.toString(enterLng))
.appendQueryParameter("enterlat", Long.toString(enterLat));
//Append all your other parameters
String myUrl = builder.build().toString();
This will construct a valid encoded url, like : http://www.friendlyride.at?enterstring=Downing%20Street%2C%20London&enterlng=-0.1272206&enterlat=51.5032077
Note : I do not know what the ...?... part ment in your url, I haven't put it in my answer.
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");
I am working with special character in URL for android
However, I encounter two problem
1) empty space
If I have a query inside url
e.g. test.php?test=aaa bbbb cccc
Then the query will not include bbbb and cccc, I learnt that I should replace the " " to %20, however, instead of using replace(" ","%20"), how can I do it in more standard way?
2) traditional chinese in url
I have an image url like this:
http://oshc.zizsoft.com/wp-content/uploads/2014/04/1-職安健資訊產品目錄2013-220x300.png
If I directly pass to android, it fail. but if I copy the link to my desktop browser , it change to like this, then I paste it on android, it works
"http://oshc.zizsoft.com/wp-content/uploads/2014/04/1-%E8%81%B7%E5%AE%89%E5%81%A5%E8%B3%87%E8%A8%8A%E7%94%A2%E5%93%81%E7%9B%AE%E9%8C%842013-220x300.png";
What should I do to encode to this?
Thanks for helping
Update:
it change to
http%3A%2F%2Foshc.zizsoft.com%2Fwp-content%2Fuploads%2F2013%2F12%2FSQ1-351x300.jpg
How can I fix that? Thanks
1) Try trimming the url, it wil remove the spaces from url. I guess its bit more staanddard way to solve this issue.
2) This problem is eactly due to the encoding issues. Our Android default encoding is cp1252...and those strings will not be encoded in the same way so when it used in the code its automatically changes to some other symbols. So try changing the encoding of the project and string to the same like UTF-8 or something. You can change project encoding by
Properties> Resources> Encoding
Hope my suggesions will help you a bit.
Try this links also
1. Trimming
2. Encoding
If you want to programmatically encode and decode URLs then use URLEncoder and URLDecoder class available in Java as well as Android.
// To encode URL
URLEncoder.encode(url, charset);
// to decode url
URLDecoder.decode(url, charset);
I am going to get the text in an EditText and then display the text in a WebView. The following code works for ASCII characters. For non-ASCII characters, the text in WebView becomes garbage characters.
String input = mEditText.getText().toString();
String html = makeHTML(input); // append HTML elements and headers including MIME and ENCODING header
mWebView.loadData(html, "text/html", "utf-8");
I thought that I was doing something wrong with my HTML, so I try to display the text directly in the WebView without modify the text. However, the result was the same.
String input = mEditText.getText().toString();
mWebView.loadData(input, "text/html", "utf-8");
The makeText() of Toast which displays non-ASCII text in EditText without any problem.
Does anyone know the answer?
WebView might not be able to load certain "unsafe" HTML characters. Try using:
String input = mEditText.getText().toString();
String html = makeHTML(input);
String encodedHtml = URLEncoder.encode(html,"UTF-8");
mWebView.loadData(encodedHtml, "text/html", "utf-8");
The URLEncoder.decode(encodedHtml,"UTF-8") method might also be useful.
Finally, I solve the problem by using loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl) of WebView
mWebView.loadData(data, mimeType, encoding)
mWebView.loadDataWithBaseURL("", data, mimeType, encoding, "")
seems to be same but actually does not.
In my case, loadData() failed to encode the characters properly and failed to load images saved in the asset folder.