Android URL Create buildUrlParameterString( List> parameters)

Here you can find the source of buildUrlParameterString( List> parameters)

Description

Shorthand for building a parameter string used in the url of GET requests;

Declaration

public static String buildUrlParameterString(
        List<Pair<String, String>> parameters)
        throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;

import android.util.Pair;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import java.util.List;

public class Main {
    /** Shorthand for building a parameter string used in the url of GET requests; */
    public static String buildUrlParameterString(
            List<Pair<String, String>> parameters)
            throws UnsupportedEncodingException {
        return buildParameterString(parameters, "&", false);
    }//from w ww.jav  a 2  s.c o m

    /** Builds a parameter string based on the given parameters, separated by given delimiter, and optionally quoted values */
    public static String buildParameterString(
            List<Pair<String, String>> parameters, String delimiter,
            boolean quote) throws UnsupportedEncodingException {

        if (parameters == null || parameters.isEmpty())
            return "";
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < parameters.size(); i++) {
            String key = parameters.get(i).first;
            String value = parameters.get(i).second;
            // skip 'null' values
            if (value == null)
                continue;
            String a = URLEncoder.encode(key, "UTF-8");
            String b = URLEncoder.encode(value, "UTF-8");
            builder.append(a + "="); // append key + "="
            if (quote)
                builder.append("\""); // optionally append quote
            builder.append(b); // append value
            if (quote)
                builder.append("\""); // optionally append quote
            if (i != parameters.size() - 1)
                builder.append(delimiter);
        }
        return builder.toString();
    }
}

Related

  1. url(String baseUrl, String relativePath)
  2. url(String baseUrl, String relativePath)
  3. createUrl(final String rootUrl, final String indivisualHost, final String featurePath, final int index, List pairs)
  4. buildBodyParameterString( List> parameters)
  5. buildParameterString( List> parameters, String delimiter, boolean quote)
  6. appendParameter(String url, String name, String value)
  7. appendParameter(String url, String name, String value)
  8. geoCoderUrlBuilder(double lat, double lng)
  9. makeUrl(String inBaseUrl, String inUrl)