Search in sources :

Example 46 with URL

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

the class HTTPUtil method requiresUpdate.

public static boolean requiresUpdate(URL u, File destFile, boolean forceCheck) throws IOException {
    // https://github.com/AdoptOpenJDK/openjdk11-binaries/releases/download
    if (destFile.exists() && "github.com".equals(u.getHost()) && u.getPath().contains("/releases/download/")) {
        logger.fine("Github release assets should not require updating.");
        return false;
    }
    if (!forceCheck && getExpiryDate(destFile) >= System.currentTimeMillis()) {
        return false;
    }
    File etagFile = new File(destFile.getParentFile(), destFile.getName() + ".etag");
    if (destFile.exists() && etagFile.exists()) {
        String etag;
        try (InputStream is = new FileInputStream(etagFile)) {
            etag = readToString(is).trim();
        }
        DownloadResponse resp = new DownloadResponse();
        if (doesETagMatch(resp, u, etag)) {
            saveExpires(resp, destFile);
            return false;
        }
        saveExpires(resp, destFile);
    }
    return true;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOUtil.readToString(com.codename1.samples.IOUtil.readToString) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 47 with URL

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

the class WebPushTest method push.

@Override
public void push(String value) {
    PushContent data = PushContent.get();
    if (data != null) {
        System.out.println("Image URL: " + data.getImageUrl());
        System.out.println("Category: " + data.getCategory());
        System.out.println("Action: " + data.getActionId());
        System.out.println("Text Response: " + data.getTextResponse());
    } else {
        System.out.println("PushContent is null");
    }
    System.out.println("Push " + value);
    Display.getInstance().callSerially(() -> {
        Dialog.show("Push received", value, "OK", null);
    });
}
Also used : PushContent(com.codename1.push.PushContent)

Example 48 with URL

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

the class WebSocketReconnectTest method start.

public void start() {
    if (current != null) {
        current.show();
        return;
    }
    String uniqueId = "1234";
    Form hi = new Form("WebSocket Test", BoxLayout.y());
    hi.getToolbar().addCommandToRightBar("Start Test", null, evt -> {
        hi.removeAll();
        String url = "wss://weblite.ca/ws/cn1-websocket-demo/chat";
        if (!"HTML5".equals(CN.getPlatformName())) {
            url = "wss://chat.mydissent.net/wsMsg";
        }
        WebSocket.setDebugLoggingEnabled(true);
        final WebSocket sock = new WebSocket(url) {

            @Override
            protected void onOpen() {
                Log.p("WebSocket onOpen - Sending uniqueId: " + uniqueId);
                hi.add("WebSocket onOpen - Sending uniqueId: " + uniqueId);
                hi.animateLayout(400);
                send(uniqueId);
            }

            @Override
            protected void onClose(int statusCode, String reason) {
                Log.p("WebSocket onClose");
                hi.add("WebSocket onClose");
                hi.animateLayout(400);
            }

            @Override
            protected void onMessage(String message) {
                Log.p("WebSocket onMessage: " + message);
                hi.add(new SpanLabel("WebSocket onMessage: " + message));
                hi.animateLayout(400);
            }

            @Override
            protected void onMessage(byte[] message) {
                Log.p("WebSocket onMessage (byte[]): " + message.toString());
            }

            @Override
            protected void onError(Exception ex) {
                Log.e(ex);
                hi.add(new SpanLabel("WebSocket onError:\n" + ex.getMessage()));
            }
        };
        sock.connect();
        sock.autoReconnect(5000);
        UITimer.timer(5000, false, hi, () -> {
            sock.close();
        });
    });
    hi.show();
}
Also used : Form(com.codename1.ui.Form) SpanLabel(com.codename1.components.SpanLabel) IOException(java.io.IOException) WebSocket(com.codename1.io.websocket.WebSocket)

Example 49 with URL

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

the class CodenameOneImplementation method downloadImageToFileSystem.

/**
 * Downloads an image to file system. This will *not* first check to see if the file exists already.
 * It will download and overwrite any existing image at the provided location.
 *
 * <p>Some platforms may override this method to use platform-level caching.  E.g. Javascript will use
 * the browser cache for downloading the image.</p>
 *
 * @param url The URL of the image to download.
 * @param fileName The storage key to be used to store the image.
 * @param onSuccess Callback on success.  Will be executed on EDT.
 * @param onFail Callback on failure.  Will be executed on EDT.
 */
public void downloadImageToFileSystem(String url, String fileName, SuccessCallback<Image> onSuccess, FailureCallback<Image> onFail) {
    ConnectionRequest cr = new ConnectionRequest();
    cr.setPost(false);
    cr.setFailSilently(true);
    cr.setReadResponseForErrors(false);
    cr.setDuplicateSupported(true);
    cr.setUrl(url);
    cr.downloadImageToFileSystem(fileName, onSuccess, onFail);
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Example 50 with URL

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

the class CodenameOneImplementation method downloadImageToStorage.

/**
 * Downloads an image to storage. This will *not* first check to see if the image is located in storage
 * already.  It will download and overwrite any existing image at the provided location.
 *
 * <p>Some platforms may override this method to use platform-level caching.  E.g. Javascript will use
 * the browser cache for downloading the image.</p>
 *
 * @param url The URL of the image to download.
 * @param fileName The storage key to be used to store the image.
 * @param onSuccess Callback on success.  Will be executed on EDT.
 * @param onFail Callback on failure.  Will be executed on EDT.
 */
public void downloadImageToStorage(String url, String fileName, SuccessCallback<Image> onSuccess, FailureCallback<Image> onFail) {
    ConnectionRequest cr = new ConnectionRequest();
    cr.setPost(false);
    cr.setFailSilently(true);
    cr.setReadResponseForErrors(false);
    cr.setDuplicateSupported(true);
    cr.setUrl(url);
    cr.downloadImageToStorage(fileName, onSuccess, onFail);
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest)

Aggregations

IOException (java.io.IOException)33 InputStream (java.io.InputStream)18 ConnectionRequest (com.codename1.io.ConnectionRequest)12 ActionEvent (com.codename1.ui.events.ActionEvent)12 ByteArrayInputStream (java.io.ByteArrayInputStream)12 File (java.io.File)11 Form (com.codename1.ui.Form)10 OutputStream (java.io.OutputStream)10 Image (com.codename1.ui.Image)9 Vector (java.util.Vector)9 EncodedImage (com.codename1.ui.EncodedImage)8 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 URISyntaxException (java.net.URISyntaxException)7 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)5 RemoteException (android.os.RemoteException)5 BufferedInputStream (com.codename1.io.BufferedInputStream)5 FileSystemStorage (com.codename1.io.FileSystemStorage)5 MediaException (com.codename1.media.AsyncMedia.MediaException)5 ActionListener (com.codename1.ui.events.ActionListener)5 FileInputStream (java.io.FileInputStream)5