Search in sources :

Example 26 with NetworkEvent

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

the class TwitterRESTService method readResponse.

/**
 * {@inheritDoc}
 */
protected void readResponse(InputStream input) throws IOException {
    InputStreamReader i = new InputStreamReader(input, "UTF-8");
    parseTree = new JSONParser().parse(i);
    fireResponseListener(new NetworkEvent(this, parseTree));
}
Also used : InputStreamReader(java.io.InputStreamReader) NetworkEvent(com.codename1.io.NetworkEvent) JSONParser(com.codename1.io.JSONParser)

Example 27 with NetworkEvent

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

the class NetworkManager method fireProgressEvent.

void fireProgressEvent(ConnectionRequest c, int type, int length, int sentReceived) {
    // progressListeners might be made null by a separate thread
    EventDispatcher d = progressListeners;
    if (d != null) {
        NetworkEvent n = new NetworkEvent(c, type);
        n.setLength(length);
        n.setSentReceived(sentReceived);
        d.fireActionEvent(n);
    }
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Example 28 with NetworkEvent

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

the class NetworkManager method handleException.

private boolean handleException(ConnectionRequest r, Exception o) {
    if (errorListeners != null) {
        ActionEvent ev = new NetworkEvent(r, o);
        errorListeners.fireActionEvent(ev);
        return ev.isConsumed();
    }
    return false;
}
Also used : ActionEvent(com.codename1.ui.events.ActionEvent)

Example 29 with NetworkEvent

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

the class NetworkManager method addErrorListener.

/**
 * Adds a generic listener to a network error that is invoked before the exception is propagated.
 * Notice that this doesn't apply to server error codes!
 * Consume the event in order to prevent it from propagating further.
 *
 * @param e callback will be invoked with the Exception as the source object
 */
public void addErrorListener(ActionListener<NetworkEvent> e) {
    if (errorListeners == null) {
        errorListeners = new EventDispatcher();
        errorListeners.setBlocking(true);
    }
    errorListeners.addListener(e);
}
Also used : EventDispatcher(com.codename1.ui.util.EventDispatcher)

Example 30 with NetworkEvent

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

the class FacebookShare method share.

/**
 * {@inheritDoc}
 */
public void share(String text, final String image, final String mime) {
    final ShareForm[] f = new ShareForm[1];
    if (image == null) {
        f[0] = new ShareForm(getOriginal(), "Post on My Wall", null, text, new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                try {
                    InfiniteProgress inf = new InfiniteProgress();
                    final Dialog progress = inf.showInifiniteBlocking();
                    FaceBookAccess.getInstance().addResponseCodeListener(new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            NetworkEvent ne = (NetworkEvent) evt;
                            int code = ne.getResponseCode();
                            FaceBookAccess.getInstance().removeResponseCodeListener(this);
                            progress.dispose();
                            Dialog.show("Failed to Share", "for some reason sharing has failed, try again later.", "Ok", null);
                            finish();
                        }
                    });
                    FaceBookAccess.getInstance().postOnWall("me", f[0].getMessage(), new ActionListener() {

                        public void actionPerformed(ActionEvent evt) {
                            progress.dispose();
                            finish();
                        }
                    });
                } catch (IOException ex) {
                    Log.e(ex);
                    System.out.println("failed to share " + ex.getMessage());
                }
            }
        });
        f[0].show();
    } else {
        f[0] = new ShareForm(getOriginal(), "Post on My Wall", null, text, image, new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                InfiniteProgress inf = new InfiniteProgress();
                final Dialog progress = inf.showInifiniteBlocking();
                FaceBookAccess.getInstance().addResponseCodeListener(new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        NetworkEvent ne = (NetworkEvent) evt;
                        int code = ne.getResponseCode();
                        FaceBookAccess.getInstance().removeResponseCodeListener(this);
                        progress.dispose();
                        Dialog.show("Failed to Share", "for some reason sharing has failed, try again later.", "Ok", null);
                        finish();
                    }
                });
                MultipartRequest req = new MultipartRequest();
                req.addResponseListener(new ActionListener() {

                    public void actionPerformed(ActionEvent evt) {
                        progress.dispose();
                        finish();
                    }
                });
                final String endpoint = "https://graph.facebook.com/me/photos?access_token=" + token;
                req.setUrl(endpoint);
                req.addArgumentNoEncoding("message", f[0].getMessage());
                InputStream is = null;
                try {
                    is = FileSystemStorage.getInstance().openInputStream(image);
                    req.addData("source", is, FileSystemStorage.getInstance().getLength(image), mime);
                    NetworkManager.getInstance().addToQueue(req);
                } catch (IOException ioe) {
                    Log.e(ioe);
                }
            }
        });
        f[0].show();
    }
}
Also used : ActionListener(com.codename1.ui.events.ActionListener) InfiniteProgress(com.codename1.components.InfiniteProgress) ActionEvent(com.codename1.ui.events.ActionEvent) Dialog(com.codename1.ui.Dialog) InputStream(java.io.InputStream) NetworkEvent(com.codename1.io.NetworkEvent) IOException(java.io.IOException) MultipartRequest(com.codename1.io.MultipartRequest)

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