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.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

public class Main {
    public static UrlEncodedFormEntity buildUrlEncodedFormEntity(Map<String, String> keyValuePairs,
            String encoding) {
        if (encoding == null) {
            encoding = HTTP.UTF_8;
        }

        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();

        if (keyValuePairs != null) {
            Set<String> keys = keyValuePairs.keySet();

            for (String key : keys) {
                String value = keyValuePairs.get(key);
                BasicNameValuePair param = new BasicNameValuePair(key, value);
                params.add(param);
            }
        }

        try {
            return new UrlEncodedFormEntity(params, encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static HttpEntity buildUrlEncodedFormEntity(Map<String, String> keyValuePairs) {
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();

        if (keyValuePairs != null) {
            Set<String> keys = keyValuePairs.keySet();

            for (String key : keys) {
                String value = keyValuePairs.get(key);
                BasicNameValuePair param = new BasicNameValuePair(key, value);
                params.add(param);
            }
        }

        try {
            return new UrlEncodedFormEntity(params);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}