MissingFormatArgumentException with square brackets on android - android

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");

Related

Uri.encode() encodes wrong

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.

Use special characters like 'ß' or space in URL

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.

JsonArrayRequest, url parameter maximum size?

I'm new to android,
I'm using a Json webservice in my app to update some database fields, but I have an issue that I can't figure out.
With this one it's working :
String url2 = "http://www.xxxxxx.com/ParserUpdateUserAction.do?test=[{\"Mail\":\"xxxxxx#hotmail.fr\",\"Nationality\":\"Spain\",\"City\":\"nimes\",\"Quote\":\"b\"}]";
JsonArrayRequest jor = new JsonArrayRequest(url2, new Response.Listener<JSONArray>().....
Not working with this :
String url2 = "http://www.xxxxxx.com/ParserUpdateUserAction.do?test=[{\"Mail\":\"xxxxxx#hotmail.fr\",\"Nationality\":\"Spain\",\"City\":\"nimes\",\"Quote\":\"bla bla bla\"}]";
JsonArrayRequest jor = new JsonArrayRequest(url2, new Response.Listener<JSONArray>().....
Could the URL parameter size be the problem ?
Thanks a lot for your help.
Could be a problem with the spaces - try to use %20 instead of a space
String url2 = "http://www.xxxxxx.com/ParserUpdateUserAction.do?test=[{\"Mail\":\"xxxxxx#hotmail.fr\",\"Nationality\":\"Spain\",\"City\":\"nimes\",\"Quote\":\"bla%20bla%20bla\"}]";
Edit:
There actually is a character limit on GET parameters, but it is about 512, so it shouldn't be a problem here - but you should definitly think of a better solution for longer mails
(Source: Max size of URL parameters in _GET)

Mystery URL decoding of certain characters - Android URI / Google App Engine

I am having a curious problem that perhaps someone has insight into. I encode a query string into a URL on Android using the following code:
request = REQUEST_BASE + "?action=loadauthor&author=" + URLEncoder.encode(author, "UTF-8");
I then add a few other parameters to the string and create a URI like this:
uri = new URI(request);
At a certain point, I pull out the query string to make a checksum:
uri.getRawQuery().getBytes();
Then I send it on its way with:
HttpGet get = new HttpGet(uri);
On the Appengine server, I then retrieve the string and try to match the checksum:
String query = req.getQueryString();
Normally, this works fine. However, there are a few characters that seem to get unencoded on the way to the server. For example,
action=loadauthor&author=Charles+Alexander+%28Ohiyesa%29+Eastman&timestamp=1343261225838&user=1479845600
shows up in the server logs (and in the GAE app) as:
action=loadauthor&author=Charles+Alexander+(Ohiyesa)+Eastman&timestamp=1343261226837&user=1479845600
This only happens to a few characters (like parentheses). Other characters remain encoded all the way through. Does anyone have a thought about what I might be doing wrong? Any feedback is appreciated.
I never did find a solution for this problem. I worked around it by unencoding certain characters on the client before sending things to the server:
request = request.replace("%28", "(");
request = request.replace("%29", ")");
request = request.replace("%27", "'");
If anyone has a better solution, I am sure that I (and others) would be interested!
URLEncoder does not encode parentheses and certain other characters, as they are supposed to be "safe" for most servers. See URLEncoder. You will have to replace these yourself if necessary.
Example:
URI uri = new URI(request.replace("(","%28"));
If a lot of replacements are needed, you can try request.replaceAll(String regularExpression, String replacement). This, of course, requires knowledge of regular expressions.

Open an URL in Android MediaPlayer

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.

Categories

Resources