Android Post Status to Twitter using Jtwitter- Example

來源:互聯網
上載者:User

 

In android, we can able to post a status to twitter by using jtwitter. Its very simple to post a status in twitter. For that we need jtwitter.jar. you can download the jwitter.jar from here
Post Status in Twitter Example :-
Create a new android project,
Add the below code in your AndroidManifest.xml for internet permission

1 <uses-permission android:name="android.permission.INTERNET"/>

Now we need to add Jtwitter to our project. Download the jwitter and save it in local drive.
Click the Android Project –> Properties — > (In left Pane) Java Build Path — > Libraries
In Libraries Tab click the ‘Add External Jars’ Button and then add the Jtwitter jar file.

Edit your java file

01 package org.androidpeople.twitter;
02  
03 import winterwell.jtwitter.Twitter;
04 import winterwell.jtwitter.TwitterException;
05 import android.app.Activity;
06 import android.os.Bundle;
07 import android.widget.Toast;
08  
09 public class TwitterExample extends Activity {
10  
11     Twitter twitter;
12  
13     @Override
14     public void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.main);
17  
18         // Set your Twitter username / Password
19         twitter = new Twitter("username","password");
20  
21         try
22         {
23             //Status to post in Twitter
24             twitter.setStatus("This is my first Tweet from Android -  AndroidPeople.com");
25             Toast.makeText(TwitterExample.this, "Article Posted to Twitter Successfully!!", Toast.LENGTH_SHORT).show();
26         }
27         catch(TwitterException.E401 e)
28         {
29             // comes here when username or password is wrongs
30             Toast.makeText(TwitterExample.this, "Wrong Username or Password,Kindly Check your logins",Toast.LENGTH_SHORT).show();
31         }
32         catch(Exception e)
33         {
34             Toast.makeText(TwitterExample.this, "Network Host not responding",Toast.LENGTH_SHORT).show();
35         }
36     }
37 }

Now run the application.
The output will looks like

Download the full source code of Twitter Example from here

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.