A light Twitter API for Java.
-
Either build the JAR or download it from the latest release
-
Create a folder, "lib", in the directory of a new/existing project
-
Move or copy the JAR file into the lib folder
-
Add the JAR file to the build path
IDEA: From the Project window, select and right-click the lib folder, then click Add as Library...
Eclipse: From the Package Explorer, select and right-click the JAR file, then select Build and click Add to Build Path
- In a Java source file, import the API
import twitterapi.TwitterApi;
- Create an instance of the API using the consumer key and secret acquired from Twitter (pass both as strings)
TwitterApi twitterApi = new TwitterApi(consumerKey, consumerSecret);
- Invoke authentication
twitterApi.authenticate();
- Pass a query to the apiCall(String) method
String apiCallUrl = "https://api.twitter.com/1.1/trends/place.json?id=1";
twitterApi.apiCall(apiCallUrl);
- Parse top ten trends
twitterApi.parseTopTenTwitterTrends();
- Get results
String topTenTrends = "Trends: " + twitterApi.getTrendsStr();
import twitterapi.TwitterApi;
public class Main {
public static void main(String[] args) {
run();
}
public static void run() {
String apiCallUrl = "https://api.twitter.com/1.1/trends/place.json?id=1";
String consumerKey = "place-key-here";
String consumerSecret = "place-secret-here";
TwitterApi twitterApi = new TwitterApi(consumerKey, consumerSecret);
twitterApi.authenticate();
twitterApi.apiCall(apiCallUrl);
twitterApi.parseTopTenTwitterTrends();
System.out.println("Trends: " + twitterApi.getTrendsStr());
}
}
authenticate(String)
Connects to Twitter's web API service to authenticate credentials
apiCall(String)
Queries the Twitter web API based on the given URL and retrieves the results
parseTopTenTwitterTrends()
Parses the top ten trends from the retrieved results
getTrendsStr()
Returns the retrieved/parsed results in a string
Note: This API includes more methods; however, this covers the majority of its functionality at this time.
All pull requests are welcome.