Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.util.Map;

public class Main {
    public static String encodeParam(Map<String, String> params) {
        if (params == null)
            return "";

        StringBuilder sb = new StringBuilder();

        for (String key : params.keySet()) {
            Object val = params.get(key);
            if (sb.length() != 0)
                sb.append("&");

            if (val instanceof Long[]) {
                Long[] longval = (Long[]) val;

                try {
                    for (int i = 0; i < longval.length; i++) {
                        String valStr = (val == null) ? "" : URLEncoder.encode(longval[i].toString(), "utf-8");

                        if (i != longval.length - 1)
                            sb.append(key + "=" + valStr + "&");
                        else
                            sb.append(key + "=" + valStr);
                    }

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    String valStr = (val == null) ? "" : URLEncoder.encode(val.toString(), "utf-8");
                    sb.append(key + "=" + valStr);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }

        }
        return sb.toString();
    }
}