<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>INFO-2051 Object-oriented programming on mobile devices Archives - Tom Barbette</title>
	<atom:link href="https://perso.uclouvain.be/tom.barbette/category/courses/info2051/feed/" rel="self" type="application/rss+xml" />
	<link>https://perso.uclouvain.be/tom.barbette/category/courses/info2051/</link>
	<description></description>
	<lastBuildDate>Fri, 20 Feb 2015 13:49:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://perso.uclouvain.be/tom.barbette/wp-content/uploads/2022/04/cropped-logo-uclouvain-2021-barbette-32x32.png</url>
	<title>INFO-2051 Object-oriented programming on mobile devices Archives - Tom Barbette</title>
	<link>https://perso.uclouvain.be/tom.barbette/category/courses/info2051/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Get a map (dictionnary) of arguments-&gt;value from an URI in Android with any API Level requirements</title>
		<link>https://perso.uclouvain.be/tom.barbette/get-a-map-dictionnary-of-arguments-value-from-an-uri-in-android-with-any-api-level-requirements/</link>
		
		<dc:creator><![CDATA[Tom Barbette]]></dc:creator>
		<pubDate>Fri, 20 Feb 2015 13:49:05 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[INFO-2051 Object-oriented programming on mobile devices]]></category>
		<guid isPermaLink="false">http://queen.run.montefiore.ulg.ac.be/~barbette/?p=415</guid>

					<description><![CDATA[<p>If someone wants to use the URI class to parse an URL in Android, and get the given parameters the normal way to do it is : String url = "http://www.example.com/?argument=value&#38;argument2=value2&#38;..."; Uri uri = Uri.parse(url); //To get the value of known parameters String argument = uri.getQueryParameter("argument"); String argument2 = uri.getQueryParameter("argument2"); ... //To look at all &#8230; </p>
<p class="link-more"><a href="https://perso.uclouvain.be/tom.barbette/get-a-map-dictionnary-of-arguments-value-from-an-uri-in-android-with-any-api-level-requirements/" class="more-link">Continue reading<span class="screen-reader-text"> "Get a map (dictionnary) of arguments->value from an URI in Android with any API Level requirements"</span></a></p>
<p>The post <a href="https://perso.uclouvain.be/tom.barbette/get-a-map-dictionnary-of-arguments-value-from-an-uri-in-android-with-any-api-level-requirements/">Get a map (dictionnary) of arguments-&gt;value from an URI in Android with any API Level requirements</a> appeared first on <a href="https://perso.uclouvain.be/tom.barbette">Tom Barbette</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>If someone wants to use the URI class to parse an URL in Android, and get the given parameters the normal way to do it is :</p>
<pre>String url = "http://www.example.com/?argument=value&amp;argument2=value2&amp;...";
Uri uri = Uri.parse(url);

//To get the value of known parameters
String argument = uri.getQueryParameter("argument");
String argument2 = uri.getQueryParameter("argument2");
...

//To look at all parameters if you don't know what you're waiting for
for (String key : uri.getQueryParameterNames()) {
   String value = uri.getQueryParameter(key);
   //Do something with value  key, like using a switch/case
}</pre>
<p><code></code><br />
The problem with the first method is that :</p>
<ul>
<li>You need to know the arguments, and you cannot check if there is &#8220;unknow&#8221; arguments, like a malicious client trying to pass &#8220;include=X&#8221;</li>
<li>The query part of the URI is parsed each time you call setQueryParameter from the beginning !</li>
</ul>
<p>The problem with the second method  is that :</p>
<ul>
<li>When you call getQueryParameterNames() the whole query is parsed once</li>
<li>The query part of the URI is parsed each time you call setQueryParameter from the beginning !</li>
<li>getQueryParameterNames() is only available starting with Android 3.0 (API Level 11)</li>
</ul>
<p>So I wrote the code below, which gives you a map of argument-&gt;value in one reading. You can then call any map-related function like containsKey(key) to know if an argument is in the URL, entrySet() to iterate through all argument/value, keySet() to get all the arguments, and values() to get all values. This only parse the URL once and is much more convenient. This code is in distributed in any GPL variant, take the one you prefer.</p>
<pre>	/**
	 * Return a map of argument-&gt;value from a query in a URI
	 * @param uri The URI
	 */
	private Map&lt;String,String&gt; getQueryParameter(Uri uri) {
	    if (uri.isOpaque()) {
	    	return Collections.emptyMap();
	    }

	    String query = uri.getEncodedQuery();
	    if (query == null) {
	        return Collections.emptyMap();
	    }

	    Map&lt;String,String&gt; parameters = new LinkedHashMap&lt;String,String&gt;();
	    int start = 0;
	    do {
	        int next = query.indexOf('&amp;', start);
	        int end = (next == -1) ? query.length() : next;

	        int separator = query.indexOf('=', start);
	        if (separator &gt; end || separator == -1) {
	            separator = end;
	        }

	        String name = query.substring(start, separator);
	        String value;
	        if (separator &lt; end)
	        	value = query.substring(separator + 1, end);
	        else
	        	value = "";
	        
	        parameters.put(Uri.decode(name),Uri.decode(value));

	        // Move start to end of name.
	        start = end + 1;
	    } while (start &lt; query.length());

	    return Collections.unmodifiableMap(parameters);
	}
</pre>
<p>The post <a href="https://perso.uclouvain.be/tom.barbette/get-a-map-dictionnary-of-arguments-value-from-an-uri-in-android-with-any-api-level-requirements/">Get a map (dictionnary) of arguments-&gt;value from an URI in Android with any API Level requirements</a> appeared first on <a href="https://perso.uclouvain.be/tom.barbette">Tom Barbette</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
