Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. package com.slk.TwitterSample;
  2.  
  3. import java.io.IOException;
  4.  
  5. import com.twitterapime.rest.Credential;
  6. import com.twitterapime.rest.TweetER;
  7. import com.twitterapime.rest.UserAccountManager;
  8. import com.twitterapime.search.LimitExceededException;
  9. import com.twitterapime.search.Tweet;
  10. import com.twitterapime.xauth.Token;
  11.  
  12. import android.app.Activity;
  13. import android.app.AlertDialog;
  14. import android.app.Dialog;
  15. import android.content.Context;
  16. import android.content.DialogInterface;
  17. import android.content.Intent;
  18. import android.content.SharedPreferences;
  19. import android.os.Bundle;
  20. import android.util.Log;
  21. import android.view.LayoutInflater;
  22. import android.view.View;
  23. import android.widget.EditText;
  24. import android.widget.Toast;
  25.  
  26. public class TwitterSample extends Activity {
  27. UserAccountManager m;
  28. Credential c;
  29. Context context;
  30. String userName="",pwd="";
  31. private String CONSUMER_KEY = "fPki8w1t1C7Hn92gZ43VkQ";
  32. private String CONSUMER_SECRET = "MUl2Etxbq1UVAJdUVNexZ00doYtZiZwhXVCFyw8EBc";
  33. public static String TWITTER_USERKEY = "userKey";
  34. public static String TWITTER_USERSECRET = "userSecret";
  35. public static String TWITTER_USERNAME = "username";
  36. public static String TWITTER_PASSWORD = "password";
  37. public static String SCREEN_NAME = "userName";
  38. public String key,secret;
  39. private static final int FPASS_DIALOG = 1;
  40. private static final int ERROR_IN_INPUT = 2;
  41. SharedPreferences TwitterPref;
  42. String userKey,userSecret;
  43. /** Called when the activity is first created. */
  44. @Override
  45. public void onCreate(Bundle savedInstanceState) {
  46. super.onCreate(savedInstanceState);
  47. setContentView(R.layout.main);
  48.  
  49. TwitterPref = this.getSharedPreferences("TwitterPref", MODE_WORLD_READABLE);
  50. key = TwitterPref.getString(TwitterSample.TWITTER_USERKEY,"");
  51. secret = TwitterPref.getString(TwitterSample.TWITTER_USERSECRET, "");
  52. userName = TwitterPref.getString(TwitterSample.TWITTER_USERNAME, "");
  53. pwd = TwitterPref.getString(TwitterSample.TWITTER_PASSWORD, "");
  54.  
  55. if(key.equals("") && key.length()<=0 || secret.equals("") && secret.length()<=0){
  56. showDialog(1);
  57.  
  58. c = new Credential(userName,pwd,CONSUMER_KEY,CONSUMER_SECRET);
  59. Log.d("Details",userName+":"+pwd);
  60. m = UserAccountManager.getInstance(c);
  61. try {
  62. if (m.verifyCredential()) {
  63.  
  64.  
  65.  
  66. Token t1=m.getAccessToken();
  67. userKey =t1.getToken();
  68. userSecret = t1.getSecret();
  69. Log.d("Keys",userKey+":"+userSecret);
  70. // SAVE IN PREFERENCE
  71. TwitterPref = TwitterSample.this.getSharedPreferences("TwitterPref", MODE_WORLD_READABLE);
  72. SharedPreferences.Editor TwitterEditor = TwitterPref.edit();
  73. TwitterEditor.putString(TwitterSample.TWITTER_USERKEY, userKey);
  74. TwitterEditor.putString(TwitterSample.TWITTER_USERSECRET, userSecret);
  75. TwitterEditor.putString(TwitterSample.TWITTER_USERNAME, userName);
  76. TwitterEditor.putString(TwitterSample.TWITTER_PASSWORD, pwd);
  77. TwitterEditor.commit();
  78.  
  79. }
  80. } catch (IOException e) {
  81. e.printStackTrace();
  82. } catch (LimitExceededException e) {
  83. e.printStackTrace();
  84. }
  85.  
  86. }
  87.  
  88.  
  89. c = new Credential(userName,pwd,CONSUMER_KEY,CONSUMER_SECRET);
  90. m = UserAccountManager.getInstance(c);
  91. try {
  92. if (m.verifyCredential()) {
  93.  
  94. Tweet t = new Tweet("RT @test "+"Hello how r u");
  95. TweetER ter = TweetER.getInstance(m);
  96. try {
  97. t = ter.post(t);
  98. } catch (IOException e) {
  99. // TODO Auto-generated catch block
  100. e.printStackTrace();
  101. } catch (LimitExceededException e) {
  102. // TODO Auto-generated catch block
  103. e.printStackTrace();
  104. }
  105.  
  106.  
  107. }
  108. } catch (IOException e) {
  109. e.printStackTrace();
  110. } catch (LimitExceededException e) {
  111. e.printStackTrace();
  112. }
  113. }
  114.  
  115. @Override
  116. protected Dialog onCreateDialog(int select) {
  117. switch(select) {
  118. case FPASS_DIALOG:
  119. {
  120. LayoutInflater factory = LayoutInflater.from(this);
  121.  
  122. final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
  123. final EditText username = (EditText)textEntryView.findViewById(R.id.username_edit);
  124. final EditText password = (EditText)textEntryView.findViewById(R.id.password_edit);
  125. return new AlertDialog.Builder(TwitterSample.this)
  126. .setIcon(R.drawable.alert_dialog_icon)
  127. .setTitle(R.string.alert_dialog_text_entry)
  128. .setView(textEntryView)
  129. .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
  130. public void onClick(DialogInterface dialog, int whichButton) {
  131. /* User clicked OK so do some stuff */
  132. userName= username.getText().toString();
  133. pwd = password.getText().toString();
  134. dialog.cancel();
  135. }
  136. })
  137. .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
  138. public void onClick(DialogInterface dialog, int whichButton) {
  139. dialog.cancel();
  140. /* User clicked cancel so do some stuff */
  141. }
  142. })
  143. .create();
  144. }
  145. case ERROR_IN_INPUT:
  146. {
  147. return new AlertDialog.Builder(TwitterSample.this)
  148. .setTitle("Error in input").setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  149. public void onClick(DialogInterface dialog, int whichButton)
  150. {
  151. showDialog(FPASS_DIALOG);
  152. }
  153. }).create();
  154. }
  155. }
  156. return null;
  157. }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement