Skip to content

Commit 03e31bb

Browse files
author
pocha
committed
updated instructions for twitter4j level
1 parent 8ba07bf commit 03e31bb

File tree

1 file changed

+80
-4
lines changed

1 file changed

+80
-4
lines changed

twitter-http-challenge/_real_twitter_with_twitter4j.html.md

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,85 @@ So far, the challenge involved the usage of the Codelearn Twitter API, which pro
66

77
Refer to the [Javadoc](http://twitter4j.org/en/javadoc.html) and [code examples](http://twitter4j.org/en/code-examples.html) provided by Twitter4J.
88

9-
###Tasks
10-
* Insert the relevant JAR files into your Eclipse project
11-
* Register at [dev.twitter.com](https://dev.twitter.com) and obtain consumer key and secret for your Twitter application.
12-
* Replace the existing HTTP network mechanisms for logging in, fetching tweets and submitting new tweets with the methods provided by Twitter4J.
9+
For using Twitter4J library, you need to register an app at [http://dev.twitter.com/apps](http://dev.twitter.com/apps). For your convenience, we have created an app & you can use the API key & secret (reproduced below). It is a good practice to create a separate class `TwitterConstants.java` and define these as static variables to be used in your app
1310

11+
public static String CONSUMER_KEY = "pC3D9eMxb8pXNAPhBHAQFYLoS";
12+
public static String CONSUMER_SECRET = "8qhOLjLHiqLQJLLQUnBNSlnUoFtbLHjWQlnaxS5W3n6NkkUeOi";
1413

14+
15+
### Tasks
16+
17+
1) Modify MainActivity.java to have only one 'Sign in with Twitter' button . Remove the LinearLayouts containing username & password fields. Hook up the button with Twitter4J so that the user gets redirected to Twitter for authentication.
18+
19+
2) Our tests will not be access the webView & fill in the data. Our tests will be using @DroidChallenge user oAuth token to test your app. We need to mandate that you should store the token details in your app in **codelearn_twitter** SharedPreference with key **key_acc_token** for the token & **key_acc_token_secret** for the secret. We are simply going to write @DroidChallenge token values to these keys in the SharedPreference to test your app on our server.
20+
21+
**P.S. - We will not get your twitter account token details this way & the app will not post as you when run on our server. Just pause & think for a minute if you do not get it.**
22+
23+
It is a good practice to store these names in TwitterConstants.java
24+
25+
`TwitterConstants.java`
26+
27+
public static final String SHARED_PREFERENCE = "codelearn_twitter";
28+
public static final String PREF_KEY_TOKEN = "key_acc_token";
29+
public static final String PREF_KEY_SECRET = "key_acc_token_secret";
30+
31+
& use it where it is required
32+
33+
`MainActivity.java`
34+
35+
prefs = getSharedPreferences(TwitterConstants.SHARED_PREFERENCE, MODE_PRIVATE);
36+
Editor editor = prefs.edit();
37+
editor.putString(TwitterConstants.PREF_KEY_TOKEN, accessToken.getToken());
38+
editor.putString(TwitterConstants.PREF_KEY_SECRET, accessToken.getTokenSecret());
39+
40+
2) Once the user gets authenticated with Twitter, he should automatically go to TweetListActivity where he can see his tweets. Modify the logic so that **only new tweets are fetched & appended to the tweet list**.
41+
42+
3) Create a new Activity **ComposeTweetActivity** to compose a tweet. Make sure to keep the name exactly the same, also the Activity should extend normal Activity & not ActionBarActivity. A corresponding layout file **activity_compose_tweet.xml** will get generated. Replace the content of the file with the content below
43+
44+
45+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
46+
xmlns:tools="http://schemas.android.com/tools"
47+
android:layout_width="match_parent"
48+
android:layout_height="match_parent"
49+
android:paddingBottom="@dimen/activity_vertical_margin"
50+
android:paddingLeft="@dimen/activity_horizontal_margin"
51+
android:paddingRight="@dimen/activity_horizontal_margin"
52+
android:paddingTop="@dimen/activity_vertical_margin" >
53+
54+
<EditText
55+
android:id="@+id/fld_compose"
56+
android:layout_width="match_parent"
57+
android:layout_height="wrap_content"
58+
android:layout_marginLeft="5dp"
59+
android:layout_marginRight="5dp"
60+
android:layout_marginTop="15dp"
61+
android:hint="@string/hint_new_tweet"
62+
android:lines="5" />
63+
64+
<TextView
65+
android:id="@+id/char_count"
66+
android:layout_width="wrap_content"
67+
android:layout_height="wrap_content"
68+
android:layout_alignLeft="@id/fld_compose"
69+
android:layout_alignRight="@id/fld_compose"
70+
android:layout_below="@id/fld_compose"
71+
android:gravity="right" />
72+
73+
<Button
74+
android:id="@+id/btn_submit"
75+
android:layout_width="290dp"
76+
android:layout_height="wrap_content"
77+
android:layout_below="@id/char_count"
78+
android:layout_centerHorizontal="true"
79+
android:layout_marginTop="15dp"
80+
android:gravity="center"
81+
android:text="@string/lbl_submit"
82+
android:textSize="13sp"
83+
android:textStyle="bold" />
84+
85+
</RelativeLayout>
86+
87+
88+
Add the logic to post the tweet on button click.
89+
90+
4) Add a menu item on TweetListActivity to take user to ComposeTweetActivity**. Overall, there should only be two menu items - the first for Refresh of tweets, the second one for compose tweet.

0 commit comments

Comments
 (0)