Search in sources :

Example 1 with MultipartRequest

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

the class CloudStorage method uploadCloudFileImpl.

private String uploadCloudFileImpl(String mimeType, String file, InputStream data, int dataSize) throws CloudException, IOException {
    String token = CloudPersona.getCurrentPersona().getToken();
    if (token == null || token.length() == 0) {
        if (!CloudPersona.createAnonymous()) {
            throw new CloudException(RETURN_CODE_FAIL_SERVER_ERROR, "Error creating anonymous login");
        }
        token = CloudPersona.getCurrentPersona().getToken();
    }
    ConnectionRequest req = new ConnectionRequest();
    req.setPost(false);
    req.setUrl(SERVER_URL + "/fileStoreURLRequest");
    // req.addArgument("bb", Display.getInstance().getProperty("built_by_user", null));
    NetworkManager.getInstance().addToQueueAndWait(req);
    int rc = req.getResponseCode();
    if (rc != 200) {
        if (rc == 420) {
            throw new CloudException(RETURN_CODE_FAIL_QUOTA_EXCEEDED);
        }
        throw new CloudException(RETURN_CODE_FAIL_SERVER_ERROR);
    }
    String d = new String(req.getResponseData());
    MultipartRequest uploadReq = new MultipartRequest();
    uploadReq.setUrl(d);
    uploadReq.setManualRedirect(false);
    uploadReq.addArgument("bb", Display.getInstance().getProperty("built_by_user", null));
    uploadReq.addArgument("t", CloudPersona.getCurrentPersona().getToken());
    uploadReq.addArgument("pk", Display.getInstance().getProperty("package_name", null));
    if (data == null) {
        int pos = file.lastIndexOf('/');
        String shortName = file;
        if (pos > -1) {
            shortName = file.substring(pos);
        }
        uploadReq.addData(shortName, file, mimeType);
    } else {
        uploadReq.addData(file, data, dataSize, mimeType);
    }
    NetworkManager.getInstance().addToQueueAndWait(uploadReq);
    if (uploadReq.getResponseCode() != 200) {
        throw new CloudException(RETURN_CODE_FAIL_SERVER_ERROR);
    }
    String r = new String(uploadReq.getResponseData());
    if ("ERROR".equals(r)) {
        throw new CloudException(RETURN_CODE_FAIL_SERVER_ERROR);
    }
    return r;
}
Also used : ConnectionRequest(com.codename1.io.ConnectionRequest) MultipartRequest(com.codename1.io.MultipartRequest)

Example 2 with MultipartRequest

use of com.codename1.io.MultipartRequest 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

MultipartRequest (com.codename1.io.MultipartRequest)2 InfiniteProgress (com.codename1.components.InfiniteProgress)1 ConnectionRequest (com.codename1.io.ConnectionRequest)1 NetworkEvent (com.codename1.io.NetworkEvent)1 Dialog (com.codename1.ui.Dialog)1 ActionEvent (com.codename1.ui.events.ActionEvent)1 ActionListener (com.codename1.ui.events.ActionListener)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1