suresh vanga
suresh vanga

Reputation: 71

How to post message to twitter using android app?

I developed a twitter application . i need After enter a text into editbox, i click the button then its post to twitter.

can any one post the code.

I tried but i get error at setStatus() method.

here is my code please help.

public void onCreate(Bundle savedInstanceState) {
        System.setProperty("http.keepAlive", "false");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_oauth);

        //check for saved log in details..
        checkForSavedLogin();

        //set consumer and provider on teh Application service
        getConsumerProvider();

        //Define login button and listener
        buttonLogin = (Button)findViewById(R.id.ButtonLogin);
        buttonLogin.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {
                askOAuth();
            }
        });

        postText = (EditText)findViewById(R.id.editText1);
        postbtn = (Button)findViewById(R.id.button1);

        postbtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                postTwitter();
            }
        });

    }

    protected void postTwitter() {
        String message = postText.getText().toString();
        System.out.println("Post Twitter.."+message);

        Thread t = new Thread() {
            public void run() {

                 twitter = new Twitter();

                 try
                 {
                 //Status to post in Twitter
                 **twitter.setStatus(postText.getText().toString());**
                 Toast.makeText(AuthActivity.this, "Article Posted to Twitter Successfully!!", Toast.LENGTH_SHORT).show();
                 }
                  catch(Exception e)
                 {
                 Toast.makeText(AuthActivity.this, "Network Host not responding",Toast.LENGTH_SHORT).show();
                 }
            }

        };
        t.start();
        }  

Upvotes: 4

Views: 8352

Answers (2)

Prachi
Prachi

Reputation: 2559

First you need to create an app on twitter

here is code to post message on twitter

 ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
            configurationBuilder.setOAuthConsumerKey(context.getResources().getString(R.string.twitter_consumer_key));
            configurationBuilder.setOAuthConsumerSecret(context.getResources().getString(R.string.twitter_consumer_secret));
            configurationBuilder.setOAuthAccessToken(LoginActivity.getAccessToken((context)));
            configurationBuilder.setOAuthAccessTokenSecret(LoginActivity.getAccessTokenSecret(context));
            Configuration configuration = configurationBuilder.build();
            final Twitter twitter = new TwitterFactory(configuration).getInstance();

            new Thread(new Runnable() {

                    private double x;

                    @Override
                    public void run() {
                            boolean success = true;
                            try {
                                    x = Math.random();
                                    twitter.updateStatus(message +" "+x);
                            } catch (TwitterException e) {
                                    e.printStackTrace();
                                    success = false;
                            }

                            final boolean finalSuccess = success;

                            callingActivity.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                            postResponse.onFinsihed(finalSuccess);
                                    }
                            });

                    }
            }).start(); 

check this tutorial for more details.

Upvotes: 3

user1391869
user1391869

Reputation:

Posting from Android into Twitter is one of the earliest stages of an Android developer. To keep full control over the posting process, we will use prefer primarily a pure OAuth post instead of dealing with Intents, so we can keep full control. So as a user, we could just think and conclude: the most typical way to authenticate is to pop up a window where we can identify with our user and password to give the application access to our account (not the full account though, just to post from the application!) and forget about the rest of the process. This might be a bit tricky process for newbies in Android. And of course, Twitter will eventually change their API or registration method, so we will found sometimes that our old implementation is not working anymore

We first need to register a Twitter App. For that purpose, we will visit the developer website of Twitter. After login in, we will add a new application. There are no special settings to be remembered, but the part of the callback URL has changed very oftenly since Twitter released their API. At the moment, if we are developing one application we only need to provide any random URL.(More for this article)

Here one of the sample Android project to show how to connect to Twitter, save it’s token and username on shared preferences so it can be used later to post status to Twitter. How to Post Twitter Status from Android

Others exaples how to send message suing androd API . Here some of the link for your help;

http://www.android10.org/index.php/articleslibraries/291-twitter-integration-in-your-android-application,

Build Android applications with Twitter

http://kenai.com/projects/twitterapime/pages/Home

Don't forget to see this also; http://abhinavasblog.blogspot.com/2010/09/using-twitter-with-oauth-on-andorid.html

Upvotes: 9

Related Questions