Take a base url and a {@link Map} of parameters to build a valid url : Url « Network « Android






Take a base url and a {@link Map} of parameters to build a valid url

 
import java.util.Map;

class Main {
  /**
   * Take a base url and a {@link Map} of parameters to build a valid url (eg
   * : http://example.com?param1=value1&param2=value2)
   * 
   * @param baseUrl
   *            the base url (eg : http://example.com)
   * @param parameters
   *            the {@link Map} of parameters (eg : {param1=value1,
   *            param2=value2})
   * @return the builded url
   */
  public static String buildUrl(String baseUrl, Map<String, Object> parameters) {
    StringBuilder strBuilderUrl = new StringBuilder(baseUrl);
    if (parameters != null && parameters.size() > 0) {
      int i = 0;
      for (Map.Entry<String, Object> entry : parameters.entrySet()) {
        if (i == 0) {
          strBuilderUrl.append('?');
        } else {
          strBuilderUrl.append('&');
        }
        strBuilderUrl.append(entry.getKey());
        strBuilderUrl.append('=');
        strBuilderUrl.append(entry.getValue());
        i++;
      }

    }
    return strBuilderUrl.toString();
  }
}

   
  








Related examples in the same category

1.Using Intent to open a URL
2.Process xml document from a Url
3.Suggest Url Provider
4.Showing android:autoLink property, which linkifies things like URLs and phone numbers found in the text.
5.extends ArrayAdapter to create URL history
6.Used to compress URL using the bit.ly service.
7.URL encode and decode
8.Is valid URL
9.Download from URLConnection
10.Request from an URL
11.Get Url From Img Tag
12.Returns the contents of the given URL as an array of strings
13.Read from a URL
14.Pull the raw text content of the given URL.
15.Get Video from URL
16.Gets data from URL
17.get Url Response
18.URL Encode Utils
19.Downloads a file given URL to specified destination
20.get Host From Url
21.encode Url
22.decode Url
23.Convert a byte array to a URL encoded string
24.get Url content with max retries
25.get Url By Post