Skip to main content

Running Twitter4J on phoneMe Advanced

Posted by brunogh on November 22, 2010 at 8:46 AM PST

Today we are gonna talk about how to create a Java application for Digital TV that integrates with Twitter. Note that the demo will be based on the basic Java DTV stack, without any specific middleware definitions, so feel free to add UI later.

phoneMe Advanced is the basic stack (CDC/FP/PBP) for DTV middlewares. I started looking at a Twitter library in Java and found an interesting one called Twitter4J (http://twitter4j.org). Twitter4J has a target compilation for Java 1.4, which is needed, because phoneME Advanced is a subset of Java 1.4 (does not have some packages and methods) compatible with the language syntax. Most of the others libraries were only for 1.5.

  1. Download phoneME Advanced (http://phoneme.dev.java.net) and compile it (http://wiki.java.net/bin/view/Mobileandembedded/PhoneMEAdvanced).
  2. Create a new Java Project and add basis.jar and btclasses.zip in the classpath (it will be generated after compiling phoneMe Advanced). Also remove the default JRE at Java Build Path->Libraries, if you are using Eclipse.
  3. Download Twitter4J (http://twitter4j.org/en/index.html) and add it to the project classpath.

Create a TwitterXlet.java class like above. Xlet is an interface that is needed to be implement, so AMS (Application Management System) can run it in a sandbox.

import java.util.ArrayList;
import java.util.List;
import javax.microedition.xlet.Xlet;
import javax.microedition.xlet.XletContext;
import javax.microedition.xlet.XletStateChangeException;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import com.sun.xlet.XletContextImpl;

public class TwitterXlet implements Xlet {

    protected XletContextImpl context;

    public void initXlet(XletContext xletContext)
            throws XletStateChangeException {
        context = (XletContextImpl) xletContext;
    }

    public void destroyXlet(boolean unconditional)
            throws XletStateChangeException {
    }

    public void pauseXlet() {
    }

    public void startXlet() throws XletStateChangeException {
        List result = this.search("#java");
        for (int i = 0; i < result.size(); i++) {
            Tweet tweet = (Tweet) result.get(i);
            System.out.println(tweet.getFromUser() + " " + tweet.getText());
        }
    }

    public List search(String s) {
        List toReturn = new ArrayList();
        try {
            Twitter twitter = new TwitterFactory().getInstance();
            Query query = new Query(s);
            QueryResult result;
            result = twitter.search(query);
            toReturn = result.getTweets();
        } catch (TwitterException e) {
            e.printStackTrace();
        }
        return toReturn;
    }
}

Create a twitter.policy file that will be passed to override on execution for test purposes:
grant {
    permission java.security.AllPermission;
};

Now, note that you will need to add xerces.jar (http://xerces.apache.org/mirrors.cgi) into the execution classpath when invokes the XletRunner to run the TwitterXlet. This is because Twitter4J depends on org.xml.sax package and the stack does not provide implementation.

To run the Xlet application:
${PHONEME_PATH}/cdc/build/linux-x86-generic/bin/cvm -Djava.security.policy=${POLICY_PATH}/twitter.policy com.sun.xlet.XletRunner -name TwitterXlet -path
${APP_PATH}/bin:${APP_PATH}/lib/twitter4j-core-2.1.6.jar:${APP_PATH}/lib/xerces.jar

Output (a lot fo people from all around the world is quoting Java!):
@@XletRunner starting Xlet TwitterXlet
[Mon Nov 22 12:59:10 GMT-03:00 2010]Will use class twitter4j.internal.logging.StdOutLoggerFactory as logging factory.
[Mon Nov 22 12:59:10 GMT-03:00 2010]Will use twitter4j.internal.http.HttpClientImpl as HttpClient implementation.
darksage07 Alguem me pode explicar ond é que ta o erro neste sniippet k fiz em #java http://pastebin.com/bzP9pB8e ?
baselogic #java #javaee I can agree with everything except for "Python 2.5 is old". Its not old, its stable for product... http://bit.ly/bCKiS7 pt
baselogic #java #javaee Reconfiguring mac for heavy JBoss, J2EE, work. #mac #java# RT @bitsmonkey:  http://bit.ly/cX1ycS pt
baselogic #java #javaee Configuration Management Database - You can move forward even if you don't want a complete CMDB... http://bit.ly/bX98l9 pt
baselogic #java #javaee Forgot url for mobile dev with spring http://bit.ly/bxhyxb# RT @vdandre:  http://bit.ly/a4xR3V pt
baselogic #java #javaee FREE Red Hat Webinar - Insight into Performance Improvements JBoss Enterprise Platforms- Nov 23... http://bit.ly/dqbtOa pt
top_java_jobs Myspace Feed System Replica by creativefuse: I am looking for a feed system similar to the New Myspa... http://bit.ly/cNnDAo #Java #Jobs
ruglax pregunta de novato, en #java, para una aplicacion #JavaSE, cual es la forma correcta de ,anejar usuarios?
enterprisejava #java #javaee Will the "NoSQL" fad thrive or end?# RT @riyaaz:  http://bit.ly/a0qmOe pt /via @baselogic:  http://bit.ly/c0JxMI
enterprisejava #java #javaee JBoss Application Server 7 looks shockingly good (Asylum #14 podcast) - http://su.pr/1q47gS# RT...... http://bit.ly/asQQvS
baselogic #java Прошивка iOS 4.2 выйдет сегодня: iPhoneRoot.comСегодня компания Apple выпустила пресс-релиз, в котором ... http://bit.ly/9XUKB5 jb
baselogic #java Google datastore discussion: The Disco BlogGoogle’s datastore, known as Bigtable, is a column-oriented ... http://bit.ly/dgGgLD jb
baselogic #java Все возможности iOS 4.2 для iPhone/iPad в 15-минутном видео: iPhoneRoot.comПока мы все ждем выхода прош... http://bit.ly/bex7Ii jb
baselogic #java 2010년 11월 22일, 미투데이: 대한민국 대표 개발자 [짱가™]2010-11-22 16:00:21 좋군요. ^^ infinitest…  우와 ~ 괜춘하네~!! by ... http://bit.ly/bY953I jb
baselogic #java Agile Testing UK – plans for the future: Gojko AdzicAt the BDD and Agile Testing exchange conference on... http://bit.ly/cp0YeT jb


ps: Thanks to Yusuke Yamamoto for the support in the users list. Great project!


Have fun!
Bruno Ghisi


 

Related Topics >>