Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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);
        }
        return builder.toString();
    }
}