Android URL Create buildBodyParameterString( List> parameters)

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

Description

Shorthand for building a parameter string used in the body of POST requests

Declaration

public static String buildBodyParameterString(
        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 body of POST requests */
    public static String buildBodyParameterString(
            List<Pair<String, String>> parameters)
            throws UnsupportedEncodingException {
        return buildParameterString(parameters, ",", true);
    }/*  w w w.  j a v 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. buildQueries(String baseQuery, List datesByQueryIndex)
  2. buildEmailUri(String email, String subject, CharSequence body)
  3. url(String baseUrl, String relativePath)
  4. url(String baseUrl, String relativePath)
  5. createUrl(final String rootUrl, final String indivisualHost, final String featurePath, final int index, List pairs)
  6. buildParameterString( List> parameters, String delimiter, boolean quote)
  7. buildUrlParameterString( List> parameters)
  8. appendParameter(String url, String name, String value)
  9. appendParameter(String url, String name, String value)