java.lang.Object | |
↳ | com.neomades.io.http.HttpRequest |
HttpRequest object represents an HTTP/HTTPS request.
To use this class, the INTERNET
permission must be declared in
the URS file.
Example: basic request to neomades.com
HttpRequest request = new HttpRequest("http://www.neomades.com").setMethodGet(); request.executeAsync(this);
By default, the HTTP method used is the GET.
HTTP connections in the application must follow the ATS requirements:
If a connection to a back end server cannot fulfill all the requirements, ATS can be bypassed by configuring the URS of the project. However, in that case, you will have to justify this in iTunes Connect when the application will be prepared for publication and Apple may reject the application.
Full documentation about ATS and network security are available at NeoMAD documentation.
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
HttpRequest(String url)
Creates a new HTTP/HTTPS request.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
void |
cancel()
Cancels or Closes the current opened/opening connection.
| ||||||||||
HttpResponse |
execute()
Opens the connection requested.
| ||||||||||
void |
executeAsync()
Opens the connection requested.
| ||||||||||
void |
executeAsync(HttpListener listener)
Opens the connection requested.
| ||||||||||
String |
getEncoding()
Returns the specified encoding or null.
| ||||||||||
int |
getTimeout()
Returns the timeout.
| ||||||||||
String |
getUrl()
Returns the URL of this request.
| ||||||||||
HttpRequest |
setDownloadProgressListener(HttpProgressListener progressListener)
Sets a download progress listener.
| ||||||||||
HttpRequest |
setEncoding(String encoding)
Sets the encoding that will be used when sending data with the POST method.
| ||||||||||
HttpRequest |
setHeader(String name, String value)
Insert HTTP header property.
| ||||||||||
HttpRequest |
setMethod(String method)
Set the method for the URL request, one of:
| ||||||||||
HttpRequest |
setMethodGet()
Enables the HTTP GET method.
| ||||||||||
HttpRequest |
setMethodHead()
Enables the HTTP HEAD method.
| ||||||||||
HttpRequest |
setMethodPost()
Enables the HTTP POST method.
| ||||||||||
HttpRequest |
setPostContent(InputStream content)
Insert HTTP POST content.
| ||||||||||
HttpRequest |
setPostContent(byte[] content)
Insert HTTP POST content.
| ||||||||||
HttpRequest |
setPostParameter(String name, String value)
Insert or replace an existing HTTP POST parameter.
| ||||||||||
HttpRequest |
setTimeout(int timeout)
Sets a timeout for the connection (milliseconds) for waiting for data.
| ||||||||||
HttpRequest |
setUploadProgressListener(HttpProgressListener progressListener)
Sets an upload progress listener.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Creates a new HTTP/HTTPS request.
After creating the request, you could set options of the request by calling
setXXXXX methods. When the request object is ready, call the
execute()
, executeAsync()
or
executeAsync(HttpListener)
methods, to create connection and begin
to connect to the server.
url | an HTTP/HTTPS URL |
---|
Cancels or Closes the current opened/opening connection.
Opens the connection requested. This method is synchronous.
Using this method is not recommended. Prefer executeAsync()
or
executeAsync(HttpListener)
instead.
Since Android API level 11 this method cannot be called in the main-thread, a CrossThreadException will be thrown.
CrossThreadException | if the method is called in the main-thread in Android |
---|---|
SecurityException | if the INTERNET permission is not declared in the
URS File
|
RuntimeException |
Opens the connection requested. This method is asynchronous.
SecurityException | if the INTERNET permission is not declared in the
URS File
|
---|
Opens the connection requested. This method is asynchronous.
listener | the HTTP response listener |
---|
SecurityException | if the INTERNET permission is not declared in the
URS File
|
---|
Returns the specified encoding or null.
Returns the timeout.
Sets a download progress listener.
Be careful : The progress download listener could work only if a
Content-Length
is set by the server and could be read by the
Http client.
progressListener | a listener called when the download has progressed. |
---|
Sets the encoding that will be used when sending data with the POST method. It is recommended to call this method, or else the data will be send with the platform's default encoding. This varies from one platform to another.
encoding | the encoding to use |
---|
Insert HTTP header property. If the header name is already defined, we
replace the previous value with the given parameter.
Headers with null
name or value are ignored.
name | name of the HTTP head property |
---|---|
value | value of the HTTP head property |
Set the method for the URL request, one of:
method | String: the HTTP method |
---|
ProtocolException | if the requested method isn't valid for HTTP. |
---|
Insert HTTP POST content.
This method is NOT mixed with setPostParameter(String, String)
or
setPostContent(byte[])
. Calling this method will erase the post
parameters already inserted.
If the current method is GET, the content will be ignored.
content | InputStream with the data to post |
---|
Insert HTTP POST content.
This method is NOT mixed with setPostParameter(String, String)
or
setPostContent(InputStream)
. Calling this method will erase the post
parameters already inserted.
If the current method is GET, the content will be ignored.
content | byte array with the data to post |
---|
Insert or replace an existing HTTP POST parameter.
This method can be call several times to insert multiple POST parameters.
This method is NOT mixed with setPostContent(byte[])
or
setPostContent(InputStream)
. Calling this method will erase the post
content already inserted.
If the current method is GET, the parameters will be ignored.
name | name of the HTTP POST parameter |
---|---|
value | value of the HTTP POST parameter |
Sets a timeout for the connection (milliseconds) for waiting for data.
By default, a timeout of 10 seconds is set.
A timeout value of zero is interpreted as an infinite timeout.
If the timeout has been reached, the HttpResponse
will be returned
(depends on synchronous or asynchronous methods).
timeout | time out in milliseconds |
---|
Sets an upload progress listener.
The upload is realized with POST HTTP requests.
progressListener | a listener called when the upload has progressed. |
---|