A simple JTwitter example for Android

I tried many different ways and searched all over the internet to find a tutorial on using JTwitter with OAuth. Here is the next step I completed

Download Jtwitter and Signpost Add them as Jars in Java Builder

Created a simple button that launches

public class ShareGenerator extends Activity {

    private static final String JTWITTER_OAUTH_KEY = "this_is_populated";
    private static final String JTWITTER_OAUTH_SECRET = "this_is_populated";

    Button menupopButton;

     @Override
        public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  setContentView(R.layout.share);
  this.setContentView(R.layout.share);
  this.txShare = (TextView)this.findViewById(R.id.lblshare);
  this.menupopButton = (Button)this.findViewById(R.id.menupop);


  menupopButton.setOnClickListener(new View.OnClickListener()
  { 
      public void onClick(View v) 

      {
          TwitterSend();

      } 
 }); 

     }

and i have a class

public void TwitterSend () {

                 OAuthSignpostClient client = new OAuthSignpostClient(JTWITTER_OAUTH_KEY, JTWITTER_OAUTH_SECRET, "oob");
                    Twitter jtwit = new Twitter("bob", client);
                    // open the authorisation page in the user browser
                    client.authorizeDesktop();
                    // get the pin
                    String v = client.askUser("Please enter the verification PIN from Twitter");
                    client.setAuthorizationCode(v);
                    // Optional: store the authorisation token details
                    Object accessToken = client.getAccessToken();
                    // use the API!
                    jtwit.setStatus("Messing about in Java");

             }

However, I cannot even open the OAuth screen. He falls when he gets there. Can someone help me at least see the OAuth screen? I installed the import correctly.

+3
source share
3 answers

The problem is this line, which uses java.awt.Desktop:

// open the authorisation page in the user browser
client.authorizeDesktop();

This will work on a desktop PC, but not on Android.

url client.authorizeUrl(); . . - :

URI url = client.authorizeUrl();
Intent myIntent = new Intent(Intent.VIEW_ACTION);
myIntent.setData(url);
startActivity(myIntent);

Android! , oob. , - ...

+3

. Android:

    OAuthSignpostClient authClient = new OAuthSignpostClient('apiKey','apiSecret','callbackUrl');

                java.net.URI jUrl = authClient.authorizeUrl();
                Uri.Builder uriBuilder = new Uri.Builder();
                uriBuilder.encodedPath(jUrl.toString());

                Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(jUrl.toString()));
                startActivity(myIntent);

, 'apiKey' . , URL- OAuthSignpostClient.

. Java.net.URI, JTwitter, Android.net.Uri, .

, URL- - , API- Twitter.

+1

see this post [Check change history if you're interested]

-2
source

Source: https://habr.com/ru/post/1753636/


All Articles