Android URL Create generateQueryString(Map params)

Here you can find the source of generateQueryString(Map params)

Description

generate Query String

Declaration

public static String generateQueryString(Map<String, String> params) 

Method Source Code

//package com.java2s;

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

public class Main {
    public static String generateQueryString(Map<String, String> params) {
        if (params == null)
            return "";
        String aQueryParam = "";
        if (params.size() > 0) {
            Set<String> aKeySet = params.keySet();
            Iterator<String> aKeyIterator = aKeySet.iterator();
            while (aKeyIterator.hasNext()) {
                String aParamName = aKeyIterator.next();
                String aParamValue = encode(params.get(aParamName));
                aQueryParam += aParamName + "=" + aParamValue + "&";
            }/*from   w ww .j a va  2  s.c  om*/
            aQueryParam = aQueryParam
                    .substring(0, aQueryParam.length() - 1);
        }
        return aQueryParam;
    }

    public static String encode(String value) {
        String encoded = "";
        try {
            encoded = URLEncoder.encode(value, "UTF-8");
        } catch (UnsupportedEncodingException ignore) {
        }
        StringBuffer buf = new StringBuffer(encoded.length());
        char focus;
        for (int i = 0; i < encoded.length(); i++) {
            focus = encoded.charAt(i);
            if (focus == '*') {
                buf.append("%2A");
            } else if (focus == '+') {
                buf.append("%20");
            } else if (focus == '%' && (i + 1) < encoded.length()
                    && encoded.charAt(i + 1) == '7'
                    && encoded.charAt(i + 2) == 'E') {
                buf.append('~');
                i += 2;
            } else {
                buf.append(focus);
            }
        }
        return buf.toString();

    }
}

Related

  1. generateQueryJson(Map params)
  2. mapToQueryString( HashMap queryString)
  3. buildQueries(String baseQuery, List datesByQueryIndex)
  4. buildEmailUri(String email, String subject, CharSequence body)
  5. url(String baseUrl, String relativePath)