Search in sources :

Example 1 with NetworkEvent

use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.

the class ConnectionRequest method addResponseListener.

/**
 * Adds a listener that would be notified on the CodenameOne thread of a response from the server.
 * This event is specific to the connection request type and its firing will change based on
 * how the connection request is read/processed
 *
 * @param a listener
 */
public void addResponseListener(ActionListener<NetworkEvent> a) {
    if (actionListeners == null) {
        actionListeners = new EventDispatcher();
        actionListeners.setBlocking(false);
    }
    actionListeners.addListener(a);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Example 2 with NetworkEvent

use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.

the class ConnectionRequest method addExceptionListener.

/**
 * Adds a listener that would be notified on the CodenameOne thread of an exception
 * in this connection request
 *
 * @param a listener
 */
public void addExceptionListener(ActionListener<NetworkEvent> a) {
    if (exceptionListeners == null) {
        exceptionListeners = new EventDispatcher();
        exceptionListeners.setBlocking(false);
    }
    exceptionListeners.addListener(a);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Example 3 with NetworkEvent

use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.

the class NetworkManager method addProgressListener.

/**
 * Adds a listener to be notified when progress updates
 *
 * @param al action listener
 */
public void addProgressListener(ActionListener<NetworkEvent> al) {
    if (progressListeners == null) {
        progressListeners = new EventDispatcher();
        progressListeners.setBlocking(false);
    }
    progressListeners.addListener(al);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Example 4 with NetworkEvent

use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.

the class RequestBuilder method getAsBytesAsync.

/**
 * Executes the request asynchronously and writes the response to the provided
 * Callback
 * @param callback writes the response to this callback
 */
public void getAsBytesAsync(final Callback<Response<byte[]>> callback) {
    ConnectionRequest request = createRequest(false);
    request.addResponseListener(new ActionListener<NetworkEvent>() {

        @Override
        public void actionPerformed(NetworkEvent evt) {
            Response res = null;
            res = new Response(evt.getResponseCode(), evt.getConnectionRequest().getResponseData(), evt.getMessage());
            callback.onSucess(res);
        }
    });
    CN.addToQueue(request);
}
Also used : GZConnectionRequest(com.codename1.io.gzip.GZConnectionRequest) ConnectionRequest(com.codename1.io.ConnectionRequest) NetworkEvent(com.codename1.io.NetworkEvent)

Example 5 with NetworkEvent

use of com.codename1.io.NetworkEvent in project CodenameOne by codenameone.

the class RequestBuilder method getAsJsonMap.

/**
 * Executes the request asynchronously and writes the response to the provided
 * Callback
 * @param callback writes the response to this callback
 * @param onError the error callback
 * @return returns the Connection Request object so it can be killed if necessary
 */
public ConnectionRequest getAsJsonMap(final SuccessCallback<Response<Map>> callback, final FailureCallback<? extends Object> onError) {
    ConnectionRequest request = createRequest(true);
    request.addResponseListener(new ActionListener<NetworkEvent>() {

        @Override
        public void actionPerformed(NetworkEvent evt) {
            if (onError != null) {
                // this is an error response code and should be handled as an error
                if (evt.getResponseCode() > 310) {
                    return;
                }
            }
            Response res = null;
            Map response = (Map) evt.getMetaData();
            res = new Response(evt.getResponseCode(), response, evt.getMessage());
            callback.onSucess(res);
        }
    });
    bindOnError(request, onError);
    CN.addToQueue(request);
    return request;
}
Also used : GZConnectionRequest(com.codename1.io.gzip.GZConnectionRequest) ConnectionRequest(com.codename1.io.ConnectionRequest) NetworkEvent(com.codename1.io.NetworkEvent) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

NetworkEvent (com.codename1.io.NetworkEvent)21 ActionEvent (com.codename1.ui.events.ActionEvent)10 ActionListener (com.codename1.ui.events.ActionListener)10 Vector (java.util.Vector)8 IOException (java.io.IOException)7 Hashtable (java.util.Hashtable)7 EventDispatcher (com.codename1.ui.util.EventDispatcher)6 EncodedImage (com.codename1.ui.EncodedImage)4 FileEncodedImage (com.codename1.components.FileEncodedImage)3 StorageImage (com.codename1.components.StorageImage)3 ConnectionRequest (com.codename1.io.ConnectionRequest)3 GZConnectionRequest (com.codename1.io.gzip.GZConnectionRequest)3 Image (com.codename1.ui.Image)3 InputStreamReader (java.io.InputStreamReader)3 InfiniteProgress (com.codename1.components.InfiniteProgress)1 BufferedInputStream (com.codename1.io.BufferedInputStream)1 CharArrayReader (com.codename1.io.CharArrayReader)1 JSONParser (com.codename1.io.JSONParser)1 MultipartRequest (com.codename1.io.MultipartRequest)1 ParseException (com.codename1.l10n.ParseException)1