convert To HashMap to Query String - Java java.net

Java examples for java.net:URL Query

Description

convert To HashMap to Query String

Demo Code

//package com.java2s;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class Main {
    public static String convertToHashMaptoQueryString(
            HashMap<String, String> params) throws Exception {

        StringBuilder sb = new StringBuilder();

        Iterator<?> iter = params.entrySet().iterator();
        while (iter.hasNext()) {
            if (sb.length() > 0) {
                sb.append('&');
            }//w  w  w. j ava2s .  c o m
            Entry<?, ?> entry = (Entry<?, ?>) iter.next();
            sb.append(entry.getKey()).append("=").append(entry.getValue());
        }

        return sb.toString();
    }

    public static String convertToHashMaptoQueryString(
            HashMap<String, Object> params, String get) throws Exception {

        StringBuilder sb = new StringBuilder();

        Iterator<?> iter = params.entrySet().iterator();
        while (iter.hasNext()) {
            if (sb.length() > 0) {
                sb.append('&');
            }
            Entry<?, ?> entry = (Entry<?, ?>) iter.next();
            sb.append(entry.getKey()).append("=").append(entry.getValue());
        }

        return sb.toString();
    }
}

Related Tutorials