public class NetworkManager extends Object
Main entry point for managing the connection requests, this is essentially a threaded queue that makes sure to route all connections via the network thread while sending the callbacks through the Codename One EDT.
The sample
code below fetches a page of data from the nestoria housing listing API.
You can see instructions on how to display the data in the InfiniteScrollAdapter
class. You can read more about networking in Codename One here
Modifier and Type | Field and Description |
---|---|
static int |
ACCESS_POINT_TYPE_CABLE
Indicates an access point based on a cable
|
static int |
ACCESS_POINT_TYPE_CORPORATE
Indicates a corporate routing server access point type (e.g.
|
static int |
ACCESS_POINT_TYPE_NETWORK2G
Indicates a 2g network access point type
|
static int |
ACCESS_POINT_TYPE_NETWORK3G
Indicates a 3g network access point type
|
static int |
ACCESS_POINT_TYPE_UNKNOWN
Indicates an unknown access point type
|
static int |
ACCESS_POINT_TYPE_WLAN
Indicates a wlan (802.11b/c/g/n) access point type
|
Modifier and Type | Method and Description |
---|---|
void |
addDefaultHeader(String key,
String value)
Adds a header to the global default headers, this header will be implicitly added
to all requests going out from this point onwards.
|
void |
addErrorListener(ActionListener<NetworkEvent> e)
Adds a generic listener to a network error that is invoked before the exception is propagated.
|
void |
addProgressListener(ActionListener<NetworkEvent> al)
Adds a listener to be notified when progress updates
|
void |
addToQueue(ConnectionRequest request)
Adds the given network connection to the queue of execution
|
void |
addToQueueAndWait(ConnectionRequest request)
Identical to add to queue but waits until the request is processed in the queue,
this is useful for completely synchronous operations.
|
AsyncResource<ConnectionRequest> |
addToQueueAsync(ConnectionRequest request)
Identical to add to queue but returns an AsyncResource object that will resolve to
the ConnectionRequest.
|
void |
assignToThread(Class requestType,
int offset)
Makes sure the given class (subclass of ConnectionRequest) is always assigned
to the given thread number.
|
Enumeration |
enumurateQueue()
This method returns all pending ConnectioRequest connections.
|
String[] |
getAPIds()
Returns the ids of the access points available if supported
|
String |
getAPName(String id)
Returns the user displayable name for the given access point
|
int |
getAPType(String id)
Returns the type of the access point
|
static String |
getAutoDetectURL()
This URL is used to check whether an Internet connection is available
|
String |
getCurrentAccessPoint()
Returns the id of the current access point
|
static NetworkManager |
getInstance()
Returns the singleton instance of this class
|
int |
getThreadCount()
The number of threads
|
int |
getTimeout()
Returns the timeout duration
|
boolean |
isAPSupported()
Indicates whether looking up an access point is supported by this device
|
boolean |
isQueueIdle()
Indicates that the network queue is idle
|
void |
killAndWait(ConnectionRequest request)
Kills the given request and waits until the request is killed if it is
being processed by one of the threads.
|
void |
removeErrorListener(ActionListener<NetworkEvent> e)
Removes the given error listener
|
void |
removeProgressListener(ActionListener<NetworkEvent> al)
Adds a listener to be notified when progress updates
|
static void |
setAutoDetectURL(String aAutoDetectURL)
This URL is used to check whether an Internet connection is available
|
void |
setCurrentAccessPoint(String id)
Returns the id of the current access point
|
void |
setThreadCount(int threadCount)
Deprecated.
since the network is always running in Codename One this method is quite confusing
unfortunately fixing it will probably break working code. You should migrate the code to use
updateThreadCount(int) |
void |
setTimeout(int t)
Sets the timeout in milliseconds for network connections, a timeout may be "faked"
for platforms that don't support the notion of a timeout such as MIDP
|
void |
shutdown()
Deprecated.
This method is for internal use only
|
void |
shutdownSync()
Shuts down the network thread and waits for shutdown to complete
|
void |
start()
There is no need to invoke this method since the network manager is started
implicitly.
|
void |
updateThreadCount(int threadCount)
Sets the number of network threads and restarts the network threads
|
public static final int ACCESS_POINT_TYPE_UNKNOWN
public static final int ACCESS_POINT_TYPE_WLAN
public static final int ACCESS_POINT_TYPE_CABLE
public static final int ACCESS_POINT_TYPE_NETWORK3G
public static final int ACCESS_POINT_TYPE_NETWORK2G
public static final int ACCESS_POINT_TYPE_CORPORATE
public static String getAutoDetectURL()
public static void setAutoDetectURL(String aAutoDetectURL)
aAutoDetectURL
- the autoDetectURL to setpublic int getThreadCount()
public void setThreadCount(int threadCount)
updateThreadCount(int)
threadCount
- the threadCount to setpublic void updateThreadCount(int threadCount)
threadCount
- the new number of threadspublic void start()
public void shutdown()
public void shutdownSync()
public static NetworkManager getInstance()
public void addDefaultHeader(String key, String value)
key
- the key of the headervalue
- the value of the headerpublic AsyncResource<ConnectionRequest> addToQueueAsync(ConnectionRequest request)
request
- the request object to add.public void addToQueueAndWait(ConnectionRequest request)
request
- the request object to addpublic void addToQueue(ConnectionRequest request)
request
- network request for executionpublic void killAndWait(ConnectionRequest request)
request
- public void setTimeout(int t)
t
- the timeout durationpublic int getTimeout()
public void addErrorListener(ActionListener<NetworkEvent> e)
e
- callback will be invoked with the Exception as the source objectpublic void removeErrorListener(ActionListener<NetworkEvent> e)
e
- callback to removepublic void addProgressListener(ActionListener<NetworkEvent> al)
al
- action listenerpublic void removeProgressListener(ActionListener<NetworkEvent> al)
al
- action listenerpublic void assignToThread(Class requestType, int offset)
requestType
- the class of the specific connection requestoffset
- the offset of the thread starting from 0 and smaller than thread countpublic Enumeration enumurateQueue()
public boolean isQueueIdle()
public boolean isAPSupported()
public String[] getAPIds()
public int getAPType(String id)
id
- access point idpublic String getAPName(String id)
id
- the id of the access pointpublic String getCurrentAccessPoint()
public void setCurrentAccessPoint(String id)
id
- id of the current access point