Android URL Create buildParameterString( List> parameters, String delimiter, boolean quote)

Here you can find the source of buildParameterString( List> parameters, String delimiter, boolean quote)

Description

Builds a parameter string based on the given parameters, separated by given delimiter, and optionally quoted values

Declaration

public static String buildParameterString(
        List<Pair<String, String>> parameters, String delimiter,
        boolean quote) 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 {
    /** 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);
        }//  www.  ja  v  a 2 s . c  om
        return builder.toString();
    }
}

Related

  1. buildEmailUri(String email, String subject, CharSequence body)
  2. url(String baseUrl, String relativePath)
  3. url(String baseUrl, String relativePath)
  4. createUrl(final String rootUrl, final String indivisualHost, final String featurePath, final int index, List pairs)
  5. buildBodyParameterString( List> parameters)
  6. buildUrlParameterString( List> parameters)
  7. appendParameter(String url, String name, String value)
  8. appendParameter(String url, String name, String value)
  9. geoCoderUrlBuilder(double lat, double lng)