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.HashMap;
import java.util.Map;

public class Main {
    public static String getPostDataString(HashMap<String, String> params) {
        try {
            StringBuilder result = new StringBuilder();
            boolean first = true;

            for (Map.Entry<String, String> entry : params.entrySet()) {
                if (first)
                    first = false;
                else
                    result.append("&");

                result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
                result.append("=");
                if (entry.getValue() == null) {
                    result.append(URLEncoder.encode("", "UTF-8"));
                } else {
                    result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
                }

            }
            return result.toString();
        } catch (UnsupportedEncodingException e) {
            System.out.print(e.getMessage());
        }
        return "";
    }
}