Android Twitter Search API 1.1 App

This is the second article in this series. The first post, Android Twitter API 1.1 App, should be read first. As stated in the first article, you will need your own consumer key and secret. These are available from dev.twitter.com for free. The app described in the article will not work without the key and secret, so please don't write me saying that the app doesn't work if you didn't add your own key and secret. 

DON'T FORGET, YOU WILL NEED YOUR OWN CONSUMER KEY AND SECRET!

Understanding the Twitter Search API

Before we can begin using the API, we need to understand it. Developers often get confused by the results of their searches and assume that there is a bug in their implementation. The Search API is geared for relevance, not completeness. So searching it isn't like searching a text document, you won't get complete and exhaustive list of every tweet where your search term is found. 

Instead what you get is based on relevance. Twitter is a very fluid thing. As others send, favor, and re-tweet tweets, the relevance of tweets change. The assumption of the Search API is that you are more interested in more relevant tweets. If this is not the case, there is the Streaming API, which is based on completeness.

Using the Twitter Search API

The focus of this article is the Twitter Search API, so there isn't much of a UI here. It is the same stripped down interface that there was in the last Twitter post. But this post should make a great start for your own Twitter app whose UI you can make as elaborate as you'd like. The search term is held in the variable, SearchTerm. It can be anything that is acceptable to Twitter. The search term will be URL encoded before it is sent so don't trip over spaces and other characters. 

I moved the consumer key and secret out of the code and into the manifest file which seems more appropriate. To handle it I added a method, getStringFromManifest. Both are read in during the onCreate method and kept 

Most of the heavy lifting of the app is actually done by Google's Gson library. It converts the JSON data return by Twitter into Java objects. It is remarkably easy to use. The hardest thing here is just getting all of the types correct. Some of that information comes from the API documentation but for me it is easier to actually make the call then copy the returned data to one of my favorite websites, JSON Editor Online. From there I create all of the classes and types need to convert the JSON search data into usable objects.

Summary

While the code does work, you still got a lot of work to do if you are hoping to create a HootSuite clone. I would probably start by making the search term part of the UI and cleaning up the tweet UI.  Again please remember to enter your own consumer key and secret into the manifest.

Source Code


References:






Popular Posts