package com.derson.pumelo.network.volley.toolbox; import android.util.Log; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import java.io.Serializable; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** * Created by chengli on 15/7/9. */ public class HttpRequestParams implements Serializable{ protected final static String LOG_TAG = "HttpRequestParams"; protected String contentEncoding = HTTP.UTF_8; public final ConcurrentHashMap<String, String> urlParams = new ConcurrentHashMap<String, String>(); protected ConcurrentHashMap<String, List<String>> urlParamsWithArray = new ConcurrentHashMap<String,List<String>>(); public void setContentEncoding(final String encoding) { if (encoding != null) { this.contentEncoding = encoding; } else { Log.d(LOG_TAG, "setContentEncoding called with null attribute"); } } public HttpRequestParams() { this((Map<String, String>) null); } public HttpRequestParams(Map<String, String> source) { if (source != null) { for (Map.Entry<String, String> entry : source.entrySet()) { put(entry.getKey(), entry.getValue()); } } } public HttpRequestParams(final String key, final String value) { this(new HashMap<String, String>() {{ put(key, value); }}); } public HttpRequestParams(Object... keysAndValues) { int len = keysAndValues.length; if (len % 2 != 0) throw new IllegalArgumentException("Supplied arguments must be even"); for (int i = 0; i < len; i += 2) { String key = String.valueOf(keysAndValues[i]); String val = String.valueOf(keysAndValues[i + 1]); put(key, val); } } public void put(String key, String value) { if (key != null && value != null) { urlParams.put(key, value); } } public void put(String key, int value) { if (key != null) { urlParams.put(key, String.valueOf(value)); } } public void put(String key, long value) { if (key != null) { urlParams.put(key, String.valueOf(value)); } } public void put(String key, List<String> values) { if(key != null && values != null) { urlParamsWithArray.put(key, values); } } public void add(String key, String value) { if(key != null && value != null) { List<String> paramArray = urlParamsWithArray.get(key); if (paramArray == null) { paramArray = new ArrayList<String>(); this.put(key, paramArray); } paramArray.add(value); } } public void remove(String key) { urlParams.remove(key); } public boolean has(String key) { return urlParams.get(key) != null; } @Override public String toString() { StringBuilder result = new StringBuilder(); for (ConcurrentHashMap.Entry<String, String> entry : urlParams.entrySet()) { if (result.length() > 0) result.append("&"); result.append(entry.getKey()); result.append("="); result.append(entry.getValue()); } for(ConcurrentHashMap.Entry<String, List<String>> entry : urlParamsWithArray.entrySet()) { if(result.length() > 0) result.append("&"); List<String> values = entry.getValue(); for (int i = 0; i < values.size(); i++) { if (i != 0) result.append("&"); result.append(entry.getKey()); result.append("="); result.append(values.get(i)); } } return result.toString(); } 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; } private List<BasicNameValuePair> getParamsList(String key, Object value) { List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>(); if (value instanceof Map) { Map map = (Map) value; List list = new ArrayList<Object>(map.keySet()); // Ensure consistent ordering in query string if (list.size() > 0 && list.get(0) instanceof Comparable) { Collections.sort(list); } for (Object nestedKey : list) { if (nestedKey instanceof String) { Object nestedValue = map.get(nestedKey); if (nestedValue != null) { params.addAll(getParamsList(key == null ? (String) nestedKey : String.format("%s[%s]", key, nestedKey), nestedValue)); } } } } else if (value instanceof List) { List list = (List) value; int listSize = list.size(); for (int nestedValueIndex = 0; nestedValueIndex < listSize; nestedValueIndex++) { params.addAll(getParamsList(String.format("%s[%d]", key, nestedValueIndex), list.get(nestedValueIndex))); } } else if (value instanceof Object[]) { Object[] array = (Object[]) value; int arrayLength = array.length; for (int nestedValueIndex = 0; nestedValueIndex < arrayLength; nestedValueIndex++) { params.addAll(getParamsList(String.format("%s[%d]", key, nestedValueIndex), array[nestedValueIndex])); } } else if (value instanceof Set) { Set set = (Set) value; for (Object nestedValue : set) { params.addAll(getParamsList(key, nestedValue)); } } else { params.add(new BasicNameValuePair(key, value.toString())); } return params; } public String getParamString() { return URLEncodedUtils.format(getParamsList(), contentEncoding); } }