com.loopj.android.http.RequestParams Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of android-async-http Show documentation
Show all versions of android-async-http Show documentation
An Asynchronous HTTP Library for Android
/*
Android Asynchronous Http Client
Copyright (c) 2011 James Smith
http://loopj.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.loopj.android.http;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* A collection of string request parameters or files to send along with requests made from an
* {@link AsyncHttpClient} instance.
For example:
*
* RequestParams params = new RequestParams();
* params.put("username", "james");
* params.put("password", "123456");
* params.put("email", "my@email.com");
* params.put("profile_picture", new File("pic.jpg")); // Upload a File
* params.put("profile_picture2", someInputStream); // Upload an InputStream
* params.put("profile_picture3", new ByteArrayInputStream(someBytes)); // Upload some bytes
*
* Map<String, String> map = new HashMap<String, String>();
* map.put("first_name", "James");
* map.put("last_name", "Smith");
* params.put("user", map); // url params: "user[first_name]=James&user[last_name]=Smith"
*
* Set<String> set = new HashSet<String>(); // unordered collection
* set.add("music");
* set.add("art");
* params.put("like", set); // url params: "like=music&like=art"
*
* List<String> list = new ArrayList<String>(); // Ordered collection
* list.add("Java");
* list.add("C");
* params.put("languages", list); // url params: "languages[]=Java&languages[]=C"
*
* String[] colors = { "blue", "yellow" }; // Ordered collection
* params.put("colors", colors); // url params: "colors[]=blue&colors[]=yellow"
*
* List<Map<String, String>> listOfMaps = new ArrayList<Map<String,
* String>>();
* Map<String, String> user1 = new HashMap<String, String>();
* user1.put("age", "30");
* user1.put("gender", "male");
* Map<String, String> user2 = new HashMap<String, String>();
* user2.put("age", "25");
* user2.put("gender", "female");
* listOfMaps.add(user1);
* listOfMaps.add(user2);
* params.put("users", listOfMaps); // url params: "users[][age]=30&users[][gender]=male&users[][age]=25&users[][gender]=female"
*
* AsyncHttpClient client = new AsyncHttpClient();
* client.post("http://myendpoint.com", params, responseHandler);
*
*/
public class RequestParams {
public final static String APPLICATION_OCTET_STREAM =
"application/octet-stream";
public final static String APPLICATION_JSON =
"application/json";
protected final static String LOG_TAG = "RequestParams";
protected boolean isRepeatable;
protected boolean useJsonStreamer;
protected boolean autoCloseInputStreams;
protected final ConcurrentHashMap urlParams = new ConcurrentHashMap();
protected final ConcurrentHashMap streamParams = new ConcurrentHashMap();
protected final ConcurrentHashMap fileParams = new ConcurrentHashMap();
protected final ConcurrentHashMap urlParamsWithObjects = new ConcurrentHashMap();
protected String contentEncoding = HTTP.UTF_8;
/**
* Sets content encoding for return value of {@link #getParamString()} and {@link
* #createFormEntity()}
Default encoding is "UTF-8"
*
* @param encoding String constant from {@link org.apache.http.protocol.HTTP}
*/
public void setContentEncoding(final String encoding) {
if (encoding != null)
this.contentEncoding = encoding;
else
Log.d(LOG_TAG, "setContentEncoding called with null attribute");
}
/**
* Constructs a new empty {@code RequestParams} instance.
*/
public RequestParams() {
this((Map) null);
}
/**
* Constructs a new RequestParams instance containing the key/value string params from the
* specified map.
*
* @param source the source key/value string map to add.
*/
public RequestParams(Map source) {
if (source != null) {
for (Map.Entry entry : source.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
}
/**
* Constructs a new RequestParams instance and populate it with a single initial key/value
* string param.
*
* @param key the key name for the intial param.
* @param value the value string for the initial param.
*/
public RequestParams(final String key, final String value) {
this(new HashMap() {{
put(key, value);
}});
}
/**
* Constructs a new RequestParams instance and populate it with multiple initial key/value
* string param.
*
* @param keysAndValues a sequence of keys and values. Objects are automatically converted to
* Strings (including the value {@code null}).
* @throws IllegalArgumentException if the number of arguments isn't even.
*/
public RequestParams(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);
}
}
/**
* Adds a key/value string pair to the request.
*
* @param key the key name for the new param.
* @param value the value string for the new param.
*/
public void put(String key, String value) {
if (key != null && value != null) {
urlParams.put(key, value);
}
}
/**
* Adds a file to the request.
*
* @param key the key name for the new param.
* @param file the file to add.
* @throws java.io.FileNotFoundException throws if wrong File argument was passed
*/
public void put(String key, File file) throws FileNotFoundException {
put(key, file, null);
}
/**
* Adds a file to the request.
*
* @param key the key name for the new param.
* @param file the file to add.
* @param contentType the content type of the file, eg. application/json
* @throws java.io.FileNotFoundException throws if wrong File argument was passed
*/
public void put(String key, File file, String contentType) throws FileNotFoundException {
if (file == null || !file.exists()) {
throw new FileNotFoundException();
}
if (key != null) {
fileParams.put(key, new FileWrapper(file, contentType));
}
}
/**
* Adds an input stream to the request.
*
* @param key the key name for the new param.
* @param stream the input stream to add.
*/
public void put(String key, InputStream stream) {
put(key, stream, null);
}
/**
* Adds an input stream to the request.
*
* @param key the key name for the new param.
* @param stream the input stream to add.
* @param name the name of the stream.
*/
public void put(String key, InputStream stream, String name) {
put(key, stream, name, null);
}
/**
* Adds an input stream to the request.
*
* @param key the key name for the new param.
* @param stream the input stream to add.
* @param name the name of the stream.
* @param contentType the content type of the file, eg. application/json
*/
public void put(String key, InputStream stream, String name, String contentType) {
put(key, stream, name, contentType, autoCloseInputStreams);
}
/**
* Adds an input stream to the request.
*
* @param key the key name for the new param.
* @param stream the input stream to add.
* @param name the name of the stream.
* @param contentType the content type of the file, eg. application/json
* @param autoClose close input stream automatically on successful upload
*/
public void put(String key, InputStream stream, String name, String contentType, boolean autoClose) {
if (key != null && stream != null) {
streamParams.put(key, StreamWrapper.newInstance(stream, name, contentType, autoClose));
}
}
/**
* Adds param with non-string value (e.g. Map, List, Set).
*
* @param key the key name for the new param.
* @param value the non-string value object for the new param.
*/
public void put(String key, Object value) {
if (key != null && value != null) {
urlParamsWithObjects.put(key, value);
}
}
/**
* Adds a int value to the request.
*
* @param key the key name for the new param.
* @param value the value int for the new param.
*/
public void put(String key, int value) {
if (key != null) {
urlParams.put(key, String.valueOf(value));
}
}
/**
* Adds a long value to the request.
*
* @param key the key name for the new param.
* @param value the value long for the new param.
*/
public void put(String key, long value) {
if (key != null) {
urlParams.put(key, String.valueOf(value));
}
}
/**
* Adds string value to param which can have more than one value.
*
* @param key the key name for the param, either existing or new.
* @param value the value string for the new param.
*/
public void add(String key, String value) {
if (key != null && value != null) {
Object params = urlParamsWithObjects.get(key);
if (params == null) {
// Backward compatible, which will result in "k=v1&k=v2&k=v3"
params = new HashSet();
this.put(key, params);
}
if (params instanceof List) {
((List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy