Returns a set of the unique names of all query parameters. - Android Network

Android examples for Network:Uri

Description

Returns a set of the unique names of all query parameters.

Demo Code


//package com.java2s;
import android.net.Uri;

import java.util.Collections;
import java.util.LinkedHashSet;

import java.util.Set;

public class Main {
    /**/*from   w w w.j a  v  a 2  s  .co  m*/
     * Returns a set of the unique names of all query parameters. Iterating
     * over the set will return the names in order of their first occurrence.
     *
     * @return a set of decoded names
     * @throws UnsupportedOperationException if this isn't a hierarchical URI
     */
    public static Set<String> getQueryParameterNames(Uri uri) {
        if (uri.isOpaque()) {
            return Collections.emptySet();
        }

        String query = uri.getEncodedQuery();
        if (query == null) {
            return Collections.emptySet();
        }

        Set<String> names = new LinkedHashSet<String>();
        int start = 0;
        do {
            int next = query.indexOf('&', start);
            int end = (next == -1) ? query.length() : next;

            int separator = query.indexOf('=', start);
            if (separator > end || separator == -1) {
                separator = end;
            }

            String name = query.substring(start, separator);
            names.add(name);
            // Move start to end of name.
            start = end + 1;
        } while (start < query.length());

        return Collections.unmodifiableSet(names);
    }
}

Related Tutorials