com.mike.aframe.http.KJStringParams.java Source code

Java tutorial

Introduction

Here is the source code for com.mike.aframe.http.KJStringParams.java

Source

/*
 * Copyright (c) 2014, KJFrameForAndroid  (kymjs123@gmail.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mike.aframe.http;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;

import com.mike.aframe.MKException;

/**
 * http?http????KJFileParamskjh.urlPost()
 * ?<br>
 * 
 * 
 * <b></b> ?????kjh.urlPost()?
 * ?kjh.urlPost()KJStringParams <br>
 * <b></b> ConcurrentHashMap(String, String)??<br>
 * <b></b> 2014-8-7
 * 
 * @author kymjs(kymjs123@gmail.com)
 * @version 1.1
 */
public class KJStringParams implements I_HttpParams {
    protected ConcurrentHashMap<String, String> urlParams;

    protected void init() {
        urlParams = new ConcurrentHashMap<String, String>(6);
    }

    public KJStringParams() {
        init();
    }

    public KJStringParams(Map<String, String> source) {
        init();
        for (Map.Entry<String, String> entry : source.entrySet()) {
            put(entry.getKey(), entry.getValue());
        }
    }

    public KJStringParams(String key, String value) {
        init();
        put(key, value);
    }

    /**
     * kvnullMKException
     * 
     * @param key
     * @param value
     */
    public void put(String key, String value) {
        if (key != null && value != null) {
            urlParams.put(key, value);
        } else {
            throw new MKException("key or value is NULL");
        }
    }

    /**
     * keyvalue
     */
    public void remove(String key) {
        if (key == null) {
            throw new MKException("key or value is NULL");
        } else {
            urlParams.remove(key);
        }
    }

    @Override
    public String toString() {
        StringBuilder str = new StringBuilder();
        Iterator<Entry<String, String>> it = urlParams.entrySet().iterator();
        while (it.hasNext()) {
            Entry<String, String> entry = it.next();
            String key = entry.getKey();
            String value;
            try {
                value = URLEncoder.encode(entry.getValue(), "utf-8");
            } catch (UnsupportedEncodingException e) {
                value = entry.getValue();
            }
            str.append(key).append("=").append(value).append("&");
        }
        return str.toString();
    }

    /**
     * ???httpClientHttpUrlConnectiontoString()
     */
    @Override
    public HttpEntity getEntity() {
        HttpEntity entity = null;
        try {
            entity = new UrlEncodedFormEntity(getParamsList(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return entity;
    }

    protected List<BasicNameValuePair> getParamsList() {
        List<BasicNameValuePair> lparams = new LinkedList<BasicNameValuePair>();
        for (ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) {
            lparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        return lparams;
    }
}