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.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.apache.http.NameValuePair;

import android.content.ContentValues;

public class Main {
    public static String buildString(Map<String, String> params) {
        if (params == null || params.size() == 0)
            return null;

        StringBuilder sb = new StringBuilder();
        Set<Entry<String, String>> setEntry = params.entrySet();

        for (Entry<String, String> entry : setEntry) {
            sb.append(entry.getKey());
            sb.append("=");
            try {
                sb.append(getUrlEncode(entry.getValue(), "utf-8") + "&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }

        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    public static String buildString(ContentValues params) {
        if (params == null || params.size() == 0)
            return null;

        Set<Entry<String, Object>> setEntry = params.valueSet();
        StringBuilder sb = new StringBuilder();

        for (Entry<String, Object> entry : setEntry) {
            sb.append(entry.getKey());
            sb.append("=");
            sb.append(entry.getValue());
        }

        return sb.toString();
    }

    public static String buildString(List<NameValuePair> params) {
        if (params == null || params.size() == 0)
            return null;

        StringBuilder sb = new StringBuilder();

        for (NameValuePair pair : params) {
            sb.append(pair.getName());
            sb.append("=");
            sb.append(pair.getValue());
        }

        return sb.toString();
    }

    public static String getUrlEncode(String str, String codeType) throws UnsupportedEncodingException {
        if (str == null || "".equals(str))
            return null;

        String resultString = null;
        if (codeType == null || "".equals(codeType))
            resultString = URLEncoder.encode(str, "UTF-8");
        else
            resultString = URLEncoder.encode(str, codeType);

        return resultString.toLowerCase();
    }
}