FileDocCategorySizeDatePackage
URLEncodedUtils.javaAPI DocAndroid 1.5 API7603Wed May 06 22:41:10 BST 2009org.apache.http.client.utils

URLEncodedUtils

public class URLEncodedUtils extends Object
A collection of utilities for encoding URLs.

Fields Summary
public static final String
CONTENT_TYPE
private static final String
PARAMETER_SEPARATOR
private static final String
NAME_VALUE_SEPARATOR
Constructors Summary
Methods Summary
private static java.lang.Stringdecode(java.lang.String content, java.lang.String encoding)

        try {
            return URLDecoder.decode(content, 
                    encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
        } catch (UnsupportedEncodingException problem) {
            throw new IllegalArgumentException(problem);
        }
    
private static java.lang.Stringencode(java.lang.String content, java.lang.String encoding)

        try {
            return URLEncoder.encode(content, 
                    encoding != null ? encoding : HTTP.DEFAULT_CONTENT_CHARSET);
        } catch (UnsupportedEncodingException problem) {
            throw new IllegalArgumentException(problem);
        }
    
public static java.lang.Stringformat(java.util.List parameters, java.lang.String encoding)
Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

param
parameters The parameters to include.
param
encoding The encoding to use.

        final StringBuilder result = new StringBuilder();
        for (final NameValuePair parameter : parameters) {
            final String encodedName = encode(parameter.getName(), encoding);
            final String value = parameter.getValue();
            final String encodedValue = value != null ? encode(value, encoding) : "";
            if (result.length() > 0)
                result.append(PARAMETER_SEPARATOR);
            result.append(encodedName);
            result.append(NAME_VALUE_SEPARATOR);
            result.append(encodedValue);
        }
        return result.toString();
    
public static booleanisEncoded(org.apache.http.HttpEntity entity)
Returns true if the entity's Content-Type header is application/x-www-form-urlencoded.

        final Header contentType = entity.getContentType();
        return (contentType != null && contentType.getValue().equalsIgnoreCase(CONTENT_TYPE));
    
public static java.util.Listparse(java.net.URI uri, java.lang.String encoding)
Returns a list of {@link NameValuePair NameValuePairs} as built from the URI's query portion. For example, a URI of http://example.org/path/to/file?a=1&b=2&c=3 would return a list of three NameValuePairs, one for a=1, one for b=2, and one for c=3.

This is typically useful while parsing an HTTP PUT.

param
uri uri to parse
param
encoding encoding to use while parsing the query


                                                                                             
               
        List <NameValuePair> result = Collections.emptyList();
        final String query = uri.getRawQuery();
        if (query != null && query.length() > 0) {
            result = new ArrayList <NameValuePair>();
            parse(result, new Scanner(query), encoding);
        }
        return result;
    
public static java.util.Listparse(org.apache.http.HttpEntity entity)
Returns a list of {@link NameValuePair NameValuePairs} as parsed from an {@link HttpEntity}. The encoding is taken from the entity's Content-Encoding header.

This is typically used while parsing an HTTP POST.

param
entity The entity to parse
throws
IOException If there was an exception getting the entity's data.

        List <NameValuePair> result = Collections.emptyList();
        if (isEncoded(entity)) {
            final String content = EntityUtils.toString(entity);
            final Header encoding = entity.getContentEncoding();
            if (content != null && content.length() > 0) {
                result = new ArrayList <NameValuePair>();
                parse(result, new Scanner(content), 
                        encoding != null ? encoding.getValue() : null);
            }
        }
        return result;
    
public static voidparse(java.util.List parameters, java.util.Scanner scanner, java.lang.String encoding)
Adds all parameters within the Scanner to the list of parameters, as encoded by encoding. For example, a scanner containing the string a=1&b=2&c=3 would add the {@link NameValuePair NameValuePairs} a=1, b=2, and c=3 to the list of parameters.

param
parameters List to add parameters to.
param
scanner Input that contains the parameters to parse.
param
encoding Encoding to use when decoding the parameters.

        scanner.useDelimiter(PARAMETER_SEPARATOR);
        while (scanner.hasNext()) {
            final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR);
            if (nameValue.length == 0 || nameValue.length > 2)
                throw new IllegalArgumentException("bad parameter");

            final String name = decode(nameValue[0], encoding);
            String value = null;
            if (nameValue.length == 2)
                value = decode(nameValue[1], encoding);
            parameters.add(new BasicNameValuePair(name, value));
        }