encode Url from Bundle - Android Android OS

Android examples for Android OS:Bundle Get

Description

encode Url from Bundle

Demo Code


//package com.java2s;
import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;

import android.os.Bundle;

public class Main {

    public static String encodeUrl(Bundle parameters) {
        if (parameters == null) {
            return "";
        }/*from w w  w. ja v  a 2 s  .  c o  m*/
        ArrayList<String> list = new ArrayList<String>();
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (String key : parameters.keySet()) {
            list.add(key);
        }
        Collections.sort(list);
        for (String key : list) {
            if (first)
                first = false;
            else
                sb.append("&");
            if (parameters.getString(key) != null) {
                try {
                    sb.append(URLEncoder.encode(key, "UTF-8")
                            + "="
                            + URLEncoder.encode(parameters.getString(key),
                                    "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }

        return sb.toString();
    }
}

Related Tutorials