Example usage for twitter4j AsyncTwitter updateStatus

List of usage examples for twitter4j AsyncTwitter updateStatus

Introduction

In this page you can find the example usage for twitter4j AsyncTwitter updateStatus.

Prototype

void updateStatus(String status);

Source Link

Document

Updates the authenticating user's status.

Usage

From source file:com.marpies.ane.twitter.functions.UpdateStatusFunction.java

License:Apache License

@Override
public FREObject call(FREContext context, FREObject[] args) {
    super.call(context, args);

    String text = (args[0] == null) ? null : FREObjectUtils.getString(args[0]);
    mCallbackID = FREObjectUtils.getInt(args[1]);
    long inReplyToStatusID = (args[2] == null) ? -1 : Long.valueOf(FREObjectUtils.getString(args[2]));
    final List<MediaSource> mediaSources = (args[3] == null) ? null
            : FREObjectUtils.getListOfMediaSource((FREArray) args[3]);

    final AsyncTwitter twitter = TwitterAPI.getAsyncInstance(TwitterAPI.getAccessToken());
    twitter.addListener(this);

    final StatusUpdate status = new StatusUpdate(text);
    if (inReplyToStatusID >= 0) {
        status.setInReplyToStatusId(inReplyToStatusID);
    }//from w  ww.  ja  v  a  2  s  . c  o m
    /* If we have some media, system files must be created and uploaded to Twitter */
    if (mediaSources != null) {
        /* Save cache dir path before running the async task */
        AIR.setCacheDir(AIR.getContext().getActivity().getCacheDir().getAbsolutePath());
        /* Execute async task that creates files out of the given sources (URLs and Bitmaps) */
        createFilesFromSources(mediaSources, twitter, status);
        return null;
    }

    /* Posts status without media */
    twitter.updateStatus(status);

    return null;
}

From source file:com.marpies.ane.twitter.functions.UpdateStatusFunction.java

License:Apache License

private void uploadMedia(final AsyncTwitter twitter, final StatusUpdate status, final List<File> mediaList,
        final Integer callbackID) {
    /* Run the upload in separate thread */
    new Thread(new Runnable() {
        @Override/*from  www .  j a  v  a  2 s.c o  m*/
        public void run() {
            final long[] mediaIDs = new long[mediaList.size()];
            try {
                /* Synchronous twitter4j must be used in order to upload the media */
                Twitter syncTwitter = TwitterAPI.getInstance(TwitterAPI.getAccessToken());
                for (int i = 0; i < mediaIDs.length; i++) {
                    File mediaFile = mediaList.get(i);
                    AIR.log("Uploading media " + mediaFile.getAbsolutePath());
                    UploadedMedia uploadedMedia = syncTwitter.uploadMedia(mediaFile);
                    mediaIDs[i] = uploadedMedia.getMediaId();
                }
                // todo: remove cached media files
                status.setMediaIds(mediaIDs);
                /* Update status only if there is no exception during media upload */
                twitter.updateStatus(status);
            } catch (TwitterException e) {
                AIR.log("Error uploading media " + e.getMessage());
                /* Failed to upload media, let's not update status without the media */
                AIR.dispatchEvent(AIRTwitterEvent.STATUS_QUERY_ERROR, StringUtils.getEventErrorJSON(callbackID,
                        StringUtils.removeLineBreaks(e.getMessage())));
            }
        }
    }).start();
}

From source file:org.webcat.notifications.protocols.TwitterProtocol.java

License:Open Source License

@Override
public void sendMessage(SendMessageJob message, User user, IMessageSettings settings) throws Exception {
    final String username = settings.stringSettingForKey(USERNAME_SETTING, null);
    String password = settings.stringSettingForKey(PASSWORD_SETTING, null);

    if (username == null || password == null) {
        return;// www  .  ja v  a 2s.c om
    }

    String content = message.shortBody();

    if (content.length() > 140) {
        content = content.substring(0, 137) + "...";
    }

    AsyncTwitter twitter = twitterFactory.getInstance(username, password);
    twitter.updateStatus(content);
}

From source file:twitter4j.examples.async.AsyncUpdate.java

License:Apache License

public static void main(String[] args) throws InterruptedException {
    if (args.length < 1) {
        System.out.println("Usage: java twitter4j.examples.AsyncUpdate text");
        System.exit(-1);//  w  w  w . java 2 s .co  m
    }
    AsyncTwitterFactory factory = new AsyncTwitterFactory();
    AsyncTwitter twitter = factory.getInstance();
    twitter.addListener(new TwitterAdapter() {
        @Override
        public void updatedStatus(Status status) {
            System.out.println("Successfully updated the status to [" + status.getText() + "].");
            synchronized (LOCK) {
                LOCK.notify();
            }
        }

        @Override
        public void onException(TwitterException e, TwitterMethod method) {
            if (method == UPDATE_STATUS) {
                e.printStackTrace();
                synchronized (LOCK) {
                    LOCK.notify();
                }
            } else {
                synchronized (LOCK) {
                    LOCK.notify();
                }
                throw new AssertionError("Should not happen");
            }
        }
    });
    twitter.updateStatus(args[0]);
    synchronized (LOCK) {
        LOCK.wait();
    }
}