public abstract class AbstractMultipartRestOperation extends AbstractRestOperation
AbstractMultipartRestOperation
is an extension of
AbstractRestOperation
that adds support for HTTP multipart POST or
PUT requests. This is especially helpful for example to upload documents,
pictures, etc. to HTTP backends.
When using AbstractMultipartRestOperation
s you need to add "parts" to
before calling AbstractRestOperation.execute()
. A part can be either
byte[]
in
addBytePart(byte[], String, String, boolean)
addFilePart(String, String, String, boolean)
Image
in
addImageFilePart(Image, String, String, String, boolean)
Image img = ...; UpdatePhotoRequest request = new UpdatePhotoRequest() request.addOperationDelegate(this); // Base64 encoding is optional request.addQueryParameter("isBase64Encoded", "true"); StringBuffer xmlData = new StringBuffer(); xmlData.append("<platform><record>\r\n"); xmlData.append("<photo>photo.png</photo>\r\n"); xmlData.append("</record></platform>\r\n"); request.addBytePart(xmlData.toString().getBytes(), "__xml_data__", "application/xml", false); request.addImageFilePart(img, "photo.png", "photo", "image/png", true); request.execute();This will create a multipart request with the following request body:
--------JcMVA2WSWXI4NYVuxny0nYd6XW81h_N6pD\r\n Content-Disposition: form-data; name="__xml_data__"\r\n Content-Type: application/xml\r\n \r\n <platform><record>\r\n <photo>photo.png</photo>\r\n </record></platform>\r\n --------JcMVA2WSWXI4NYVuxny0nYd6XW81h_N6pD Content-Disposition: form-data; name="photo"; filename="photo.png" Content-Type: image/png \r\n iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAABQUlEQVR42g....\r\n --------JcMVA2WSWXI4NYVuxny0nYd6XW81h_N6pD--\r\n
AbstractMultipartRestOperation
will automatically generate a
boundary. You can set a custom boundary by using
setBoundaryString(String)
or setBoundaryPrefix(String)
and
setBoundaryLength(int)
. OPERATION_FAILED, OPERATION_SUCCESSFUL
Constructor and Description |
---|
AbstractMultipartRestOperation() |
Modifier and Type | Method and Description |
---|---|
boolean |
addBytePart(byte[] content,
java.lang.String fieldName,
java.lang.String contentType,
boolean encodeBase64)
Adds a new part to the multipart request body by reading the data from a
given
content byte array of raw data. |
boolean |
addFilePart(byte[] content,
java.lang.String fileName,
java.lang.String fieldName,
java.lang.String contentType,
boolean encodeBase64)
Adds a new part to the multipart request body by reading the data from a
given
content byte array of raw data. |
boolean |
addFilePart(java.lang.String fileName,
java.lang.String fieldName,
java.lang.String contentType,
boolean encodeBase64)
Adds a new part to the multipart request body by reading the data from a file
that is part of the applications resources.
|
boolean |
addImageFilePart(javax.microedition.lcdui.Image image,
java.lang.String fileName,
java.lang.String fieldName,
java.lang.String contentType,
boolean encodeBase64)
Adds a new part to the multipart request body by reading the data from an
Image instance. |
int |
getBoundaryLength()
Returns the total length of the boundary String.
|
java.lang.String |
getBoundaryPrefix()
Returns the prefix used for generating the request boundary.
|
java.lang.String |
getBoundaryString()
Returns the boundary used by this
AbstractMultipartRequest . |
void |
setBoundaryLength(int boundaryLength)
Sets the new total length of the boundary for auto generation.
|
void |
setBoundaryPrefix(java.lang.String boundaryPrefix)
Sets a new prefix used for generating boundaries.
|
void |
setBoundaryString(java.lang.String boundaryString)
Sets the boundary to a new value.
|
addHeaderParameter, addHeaderParameter, addHeaderParameter, addHeaderParameter, addQueryParameter, addQueryParameter, addQueryParameter, addQueryParameter, execute, getHeader, getHeaderParameters, getHTTPMethod, getHttpResponse, getPostData, getQueryParameters, getUrl, isExecuteAsynchronous, onResponse, reset, setExecuteAsynchronous, setHttpMethod, setPostData, setPostData, setResponse, setUrl, toString
addOperationDelegate, getOperationDelegates, getResponse, removeOperationDelegate, setCacheHandler, setResponse
public boolean addFilePart(java.lang.String fileName, java.lang.String fieldName, java.lang.String contentType, boolean encodeBase64) throws java.io.IOException
fileName
- the name of the file to read from the applications resourcesfieldName
- the name of the field used in Content-Disposition
name
attributecontentType
- the content type of the data contained in the part being addedencodeBase64
- true
if the data must be added Base64 encoded,
false
if the data will be transferred raw (as is)true
if the part was successfully added to the HTTP
request body, false
if there was an error.java.io.IOException
- if writing to the request body fails.public boolean addImageFilePart(javax.microedition.lcdui.Image image, java.lang.String fileName, java.lang.String fieldName, java.lang.String contentType, boolean encodeBase64) throws java.io.IOException
Image
instance. contentType
before added to the request body. Currently only
image/png
is supported by default. If you need to do other image
encodings, encode the image data and use
addFilePart(byte[], String, String, String, boolean)
Image
a fileName
must be provided as it
is expected that we are doing a file upload. image
- the Image
instance to be encoded and added to the request
bodyfileName
- the name of the image file used in Content-Disposition
filename
attributefieldName
- the name of the field used in Content-Disposition
name
attributecontentType
- the content type of the data contained in the part being added.
Currently only image/png
is supportedencodeBase64
- true
if the data must be added Base64 encoded,
false
if the data will be transferred raw (as is)true
if the part was successfully added to the HTTP
request body, false
if there was an error.java.io.IOException
- if writing to the request body fails.public boolean addFilePart(byte[] content, java.lang.String fileName, java.lang.String fieldName, java.lang.String contentType, boolean encodeBase64) throws java.io.IOException
content
byte array of raw data. A fileName
must be provided as the data is added as a file. content
- the raw data byte array to be added to the request bodyfileName
- the name of the file used in Content-Disposition
filename
attributefieldName
- the name of the field used in Content-Disposition
name
attributecontentType
- the content type of the data contained in the part being added.
Currently only image/png
is supportedencodeBase64
- true
if the data must be added Base64 encoded,
false
if the data will be transferred raw (as is)true
if the part was successfully added to the HTTP
request body, false
if there was an error.java.io.IOException
- if writing to the request body fails.public boolean addBytePart(byte[] content, java.lang.String fieldName, java.lang.String contentType, boolean encodeBase64) throws java.lang.Exception
content
byte array of raw data. The data represents the
value of the given fieldName
attribute and is not considered to
be a file. content
- the raw data byte array to be added to the request bodyfieldName
- the name of the field used in Content-Disposition
name
attributecontentType
- the content type of the data contained in the part being added.
Currently only image/png
is supportedencodeBase64
- true
if the data must be added Base64 encoded,
false
if the data will be transferred raw (as is)true
if the part was successfully added to the HTTP
request body, false
if there was an error.java.lang.Exception
- if writing to the request body fails.public java.lang.String getBoundaryString()
AbstractMultipartRequest
. If
the boundary is null
a new random boundary will be created and
returned. You can use setBoundaryString(String)
to set a custom
boundary. The generation of a boundary uses getBoundaryPrefix()
and
getBoundaryLength()
. public void setBoundaryString(java.lang.String boundaryString)
boundaryString
- new boundary to be used in the request and must be HTTP conformpublic java.lang.String getBoundaryPrefix()
public void setBoundaryPrefix(java.lang.String boundaryPrefix)
boundaryPrefix
- the new prefix used to generated the request boundary as Stringpublic int getBoundaryLength()
public void setBoundaryLength(int boundaryLength)
boundaryLength
- the new total length of the auto generated boundary including
prefix