public class RequestParams
extends java.lang.Object
implements java.io.Serializable
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[0]=Java&languages[1]=C" String[] colors = { "blue", "yellow" }; // Ordered collection params.put("colors", colors); // url params: "colors[0]=blue&colors[1]=yellow" File[] files = { new File("pic.jpg"), new File("pic1.jpg") }; // Ordered collection params.put("files", files); // url params: "files[]=pic.jpg&files[]=pic1.jpg" 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("https://myendpoint.com", params, responseHandler);
Modifier and Type | Class and Description |
---|---|
static class |
RequestParams.FileWrapper |
static class |
RequestParams.StreamWrapper |
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
APPLICATION_JSON |
static java.lang.String |
APPLICATION_OCTET_STREAM |
protected boolean |
autoCloseInputStreams |
protected java.lang.String |
contentEncoding |
protected java.lang.String |
elapsedFieldInJsonStreamer |
protected java.util.concurrent.ConcurrentHashMap<java.lang.String,java.util.List<RequestParams.FileWrapper>> |
fileArrayParams |
protected java.util.concurrent.ConcurrentHashMap<java.lang.String,RequestParams.FileWrapper> |
fileParams |
protected boolean |
forceMultipartEntity |
protected boolean |
isRepeatable |
protected static java.lang.String |
LOG_TAG |
protected java.util.concurrent.ConcurrentHashMap<java.lang.String,RequestParams.StreamWrapper> |
streamParams |
protected java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.String> |
urlParams |
protected java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.Object> |
urlParamsWithObjects |
protected boolean |
useJsonStreamer |
Constructor and Description |
---|
RequestParams()
Constructs a new empty
RequestParams instance. |
RequestParams(java.util.Map<java.lang.String,java.lang.String> source)
Constructs a new RequestParams instance containing the key/value string params from the
specified map.
|
RequestParams(java.lang.Object... keysAndValues)
Constructs a new RequestParams instance and populate it with multiple initial key/value
string param.
|
RequestParams(java.lang.String key,
java.lang.String value)
Constructs a new RequestParams instance and populate it with a single initial key/value
string param.
|
Modifier and Type | Method and Description |
---|---|
void |
add(java.lang.String key,
java.lang.String value)
Adds string value to param which can have more than one value.
|
cz.msebera.android.httpclient.HttpEntity |
getEntity(ResponseHandlerInterface progressHandler)
Returns an HttpEntity containing all request parameters.
|
protected java.util.List<cz.msebera.android.httpclient.message.BasicNameValuePair> |
getParamsList() |
protected java.lang.String |
getParamString() |
boolean |
has(java.lang.String key)
Check if a parameter is defined.
|
void |
put(java.lang.String key,
java.io.File file)
Adds a file to the request.
|
void |
put(java.lang.String key,
java.io.File[] files)
Adds files array to the request.
|
void |
put(java.lang.String key,
java.io.File[] files,
java.lang.String contentType,
java.lang.String customFileName)
Adds files array to the request with both custom provided file content-type and files name
|
void |
put(java.lang.String key,
java.io.File file,
java.lang.String contentType)
Adds a file to the request with custom provided file content-type
|
void |
put(java.lang.String key,
java.io.File file,
java.lang.String contentType,
java.lang.String customFileName)
Adds a file to the request with both custom provided file content-type and file name
|
void |
put(java.lang.String key,
java.io.InputStream stream)
Adds an input stream to the request.
|
void |
put(java.lang.String key,
java.io.InputStream stream,
java.lang.String name)
Adds an input stream to the request.
|
void |
put(java.lang.String key,
java.io.InputStream stream,
java.lang.String name,
java.lang.String contentType)
Adds an input stream to the request.
|
void |
put(java.lang.String key,
java.io.InputStream stream,
java.lang.String name,
java.lang.String contentType,
boolean autoClose)
Adds an input stream to the request.
|
void |
put(java.lang.String key,
int value)
Adds a int value to the request.
|
void |
put(java.lang.String key,
long value)
Adds a long value to the request.
|
void |
put(java.lang.String key,
java.lang.Object value)
Adds param with non-string value (e.g.
|
void |
put(java.lang.String key,
java.lang.String value)
Adds a key/value string pair to the request.
|
void |
put(java.lang.String key,
java.lang.String customFileName,
java.io.File file)
Adds a file to the request with custom provided file name
|
void |
remove(java.lang.String key)
Removes a parameter from the request.
|
void |
setAutoCloseInputStreams(boolean flag)
Set global flag which determines whether to automatically close input streams on successful
upload.
|
void |
setContentEncoding(java.lang.String encoding)
Sets content encoding for return value of
getParamString() and createFormEntity() |
void |
setElapsedFieldInJsonStreamer(java.lang.String value)
Sets an additional field when upload a JSON object through the streamer
to hold the time, in milliseconds, it took to upload the payload.
|
void |
setForceMultipartEntityContentType(boolean force)
If set to true will force Content-Type header to `multipart/form-data`
even if there are not Files or Streams to be send
|
void |
setHttpEntityIsRepeatable(boolean flag) |
void |
setUseJsonStreamer(boolean flag) |
java.lang.String |
toString() |
public static final java.lang.String APPLICATION_OCTET_STREAM
public static final java.lang.String APPLICATION_JSON
protected static final java.lang.String LOG_TAG
protected final java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.String> urlParams
protected final java.util.concurrent.ConcurrentHashMap<java.lang.String,RequestParams.StreamWrapper> streamParams
protected final java.util.concurrent.ConcurrentHashMap<java.lang.String,RequestParams.FileWrapper> fileParams
protected final java.util.concurrent.ConcurrentHashMap<java.lang.String,java.util.List<RequestParams.FileWrapper>> fileArrayParams
protected final java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.Object> urlParamsWithObjects
protected boolean isRepeatable
protected boolean forceMultipartEntity
protected boolean useJsonStreamer
protected java.lang.String elapsedFieldInJsonStreamer
protected boolean autoCloseInputStreams
protected java.lang.String contentEncoding
public RequestParams()
RequestParams
instance.public RequestParams(java.util.Map<java.lang.String,java.lang.String> source)
source
- the source key/value string map to add.public RequestParams(java.lang.String key, java.lang.String value)
key
- the key name for the intial param.value
- the value string for the initial param.public RequestParams(java.lang.Object... keysAndValues)
keysAndValues
- a sequence of keys and values. Objects are automatically converted to
Strings (including the value null
).java.lang.IllegalArgumentException
- if the number of arguments isn't even.public void setContentEncoding(java.lang.String encoding)
getParamString()
and createFormEntity()
Default encoding is "UTF-8"
encoding
- String constant from HTTP
public void setForceMultipartEntityContentType(boolean force)
Default value is false
force
- boolean, should declare content-type multipart/form-data even without files or streams presentpublic void put(java.lang.String key, java.lang.String value)
key
- the key name for the new param.value
- the value string for the new param.public void put(java.lang.String key, java.io.File[] files) throws java.io.FileNotFoundException
key
- the key name for the new param.files
- the files array to add.java.io.FileNotFoundException
- if one of passed files is not found at time of assembling the requestparams into requestpublic void put(java.lang.String key, java.io.File[] files, java.lang.String contentType, java.lang.String customFileName) throws java.io.FileNotFoundException
key
- the key name for the new param.files
- the files array to add.contentType
- the content type of the file, eg. application/jsoncustomFileName
- file name to use instead of real file namejava.io.FileNotFoundException
- throws if wrong File argument was passedpublic void put(java.lang.String key, java.io.File file) throws java.io.FileNotFoundException
key
- the key name for the new param.file
- the file to add.java.io.FileNotFoundException
- throws if wrong File argument was passedpublic void put(java.lang.String key, java.lang.String customFileName, java.io.File file) throws java.io.FileNotFoundException
key
- the key name for the new param.file
- the file to add.customFileName
- file name to use instead of real file namejava.io.FileNotFoundException
- throws if wrong File argument was passedpublic void put(java.lang.String key, java.io.File file, java.lang.String contentType) throws java.io.FileNotFoundException
key
- the key name for the new param.file
- the file to add.contentType
- the content type of the file, eg. application/jsonjava.io.FileNotFoundException
- throws if wrong File argument was passedpublic void put(java.lang.String key, java.io.File file, java.lang.String contentType, java.lang.String customFileName) throws java.io.FileNotFoundException
key
- the key name for the new param.file
- the file to add.contentType
- the content type of the file, eg. application/jsoncustomFileName
- file name to use instead of real file namejava.io.FileNotFoundException
- throws if wrong File argument was passedpublic void put(java.lang.String key, java.io.InputStream stream)
key
- the key name for the new param.stream
- the input stream to add.public void put(java.lang.String key, java.io.InputStream stream, java.lang.String name)
key
- the key name for the new param.stream
- the input stream to add.name
- the name of the stream.public void put(java.lang.String key, java.io.InputStream stream, java.lang.String name, java.lang.String contentType)
key
- the key name for the new param.stream
- the input stream to add.name
- the name of the stream.contentType
- the content type of the file, eg. application/jsonpublic void put(java.lang.String key, java.io.InputStream stream, java.lang.String name, java.lang.String contentType, boolean autoClose)
key
- the key name for the new param.stream
- the input stream to add.name
- the name of the stream.contentType
- the content type of the file, eg. application/jsonautoClose
- close input stream automatically on successful uploadpublic void put(java.lang.String key, java.lang.Object value)
key
- the key name for the new param.value
- the non-string value object for the new param.public void put(java.lang.String key, int value)
key
- the key name for the new param.value
- the value int for the new param.public void put(java.lang.String key, long value)
key
- the key name for the new param.value
- the value long for the new param.public void add(java.lang.String key, java.lang.String value)
key
- the key name for the param, either existing or new.value
- the value string for the new param.public void remove(java.lang.String key)
key
- the key name for the parameter to remove.public boolean has(java.lang.String key)
key
- the key name for the parameter to check existence.public java.lang.String toString()
toString
in class java.lang.Object
public void setHttpEntityIsRepeatable(boolean flag)
public void setUseJsonStreamer(boolean flag)
public void setElapsedFieldInJsonStreamer(java.lang.String value)
To disable this feature, call this method with null as the field value.
value
- field name to add elapsed time, or null to disablepublic void setAutoCloseInputStreams(boolean flag)
flag
- boolean whether to automatically close input streamspublic cz.msebera.android.httpclient.HttpEntity getEntity(ResponseHandlerInterface progressHandler) throws java.io.IOException
progressHandler
- HttpResponseHandler for reporting progress on entity submitHttpEntityEnclosingRequestBase
java.io.IOException
- if one of the streams cannot be readprotected java.util.List<cz.msebera.android.httpclient.message.BasicNameValuePair> getParamsList()
protected java.lang.String getParamString()