Skip to content

SocialPost

Ahmed Khalil edited this page Jun 25, 2017 · 2 revisions

This class is in charge of posting to social media.

SocialPost flowchart

Extensions & Implementations

This class has none.

Public Variables

This class has only one public variable.

    private static String message;

Constructor

Constructor to create an instance of SocialPost and pass to it the text message.

    SocialPost(String message){
        SocialPost.message = message;
    }

Methods

postToTwitter()

This method posts a picture and caption to Twitter.

    void postToTwitter(String filePath) {
        new AsyncTask<String, Void, Void>() {

            @Override
            protected Void doInBackground(String... params) {
                ConfigurationBuilder twitterConfigBuilder = new ConfigurationBuilder();
                twitterConfigBuilder.setDebugEnabled(true);
                twitterConfigBuilder.setOAuthConsumerKey("lxRCnjL6HaMUg7HjxAJC1k6IH");
                twitterConfigBuilder.setOAuthConsumerSecret(
                        "6E3oLs4kln9p4oMkBRi2LceOkXuDYKASlXIm53UEq1wDNC4FxI");
                twitterConfigBuilder.setOAuthAccessToken(
                        "854512192879820800-zcc88HtCEEcHyXO0JjgZJEFKmLP2HUi");
                twitterConfigBuilder.setOAuthAccessTokenSecret(
                        "bUPpgmB6ipYkVb2kQ0LgAOeUPQtzZ78qBRB2iSrHQdJAe");

                Twitter twitter = new TwitterFactory(twitterConfigBuilder.build()).getInstance();
                File file = new File(params[0]);

                StatusUpdate status = new StatusUpdate("This is my view from " + message + "!");
                status.setMedia(file); // set the image to be uploaded here.
                try {
                    twitter.updateStatus(status);
                } catch (TwitterException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute(filePath);
    }