com.karura.framework.utils.UrlUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.karura.framework.utils.UrlUtils.java

Source

/**
    
 ============== GPL License ==============
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    
 ============== Commercial License==============
 https://github.com/karuradev/licenses/blob/master/toc.txt
 */

package com.karura.framework.utils;

import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

import android.net.Uri.Builder;
import android.net.UrlQuerySanitizer;

public class UrlUtils {

    public static UrlQuerySanitizer sanitizeQuery(String query, String[] parameters) {
        UrlQuerySanitizer sanitizer = new UrlQuerySanitizer();

        sanitizer.registerParameters(parameters, UrlQuerySanitizer.getSpaceLegal());
        sanitizer.parseUrl(query);
        return sanitizer;
    }

    public static String jsonToQuery(String jsonString, String scheme, String authority) throws JSONException {
        Builder builder = new Builder();
        builder.scheme(scheme);
        builder.authority(authority);

        JSONObject json = new JSONObject(jsonString.trim());
        Iterator<?> keys = json.keys();
        String key = null;
        String value = null;
        while (keys.hasNext()) {
            key = String.valueOf(keys.next());
            value = String.valueOf(json.get(key));
            builder.appendQueryParameter(key, value);
        }
        return builder.build().toString();
    }
}