Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.net.Uri;
import android.text.TextUtils;

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

public class Main {

    public static final String getRawQueryParameter(Uri uri, String queryParameterName) {
        String ret = null;

        if ((uri != null) && (!TextUtils.isEmpty(queryParameterName))) {
            final String query = uri.getEncodedQuery();
            if (query == null) {
                return null;
            }

            final String encodedKey = Uri.encode(queryParameterName, null);
            final int length = query.length();
            int start = 0;
            do {
                int nextAmpersand = query.indexOf('&', start);
                int end = nextAmpersand != -1 ? nextAmpersand : length;

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

                if (separator - start == encodedKey.length()
                        && query.regionMatches(start, encodedKey, 0, encodedKey.length())) {
                    if (separator == end) {
                        ret = "";
                    } else {
                        ret = query.substring(separator + 1, end);
                    }
                    break;
                }

                // Move start to end of name.
                if (nextAmpersand != -1) {
                    start = nextAmpersand + 1;
                } else {
                    break;
                }
            } while (true);
        }

        return ret;
    }

    private static String encode(final String content, final String encoding) {
        try {
            return URLEncoder.encode(content, encoding != null ? encoding : "UTF-8");
        } catch (UnsupportedEncodingException problem) {
            throw new IllegalArgumentException(problem);
        }
    }
}